From 2347cfeb381a49b5edee5151cb74cc3dae679040 Mon Sep 17 00:00:00 2001
From: Zhiming Wang <zmwangx@gmail.com>
Date: Tue, 19 May 2015 18:51:24 -0700
Subject: pyblog: smarter auto touch

Now reading date directly from post.
---
 pyblog | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

(limited to 'pyblog')

diff --git a/pyblog b/pyblog
index 94547001..cee67bee 100755
--- a/pyblog
+++ b/pyblog
@@ -775,7 +775,7 @@ def deploy(args):
 
 def gen_deploy(args):
     """Regenerate and deploy."""
-    # pylint: disable=unused-argument
+    # pylint: disable=unused-argument,too-many-branches
 
     # try to smartly determine the latest post, and prompt to touch it
     current_time = time.time()
@@ -786,26 +786,27 @@ def gen_deploy(args):
         if not matchobj:
             continue
         fullpath = os.path.join(POSTSDIR, name)
-        ctime = os.path.getctime(fullpath)
         mtime = os.path.getmtime(fullpath)
-        # get date registered in the filename, which has the format
-        # xxxx-xx-xx-blah-blah.md
-        cdate = datetime.datetime(year=int(matchobj.group(1)),
-                                  month=int(matchobj.group(2)),
-                                  day=int(matchobj.group(3)),
-                                  hour=23, minute=59, second=59,
-                                  tzinfo=dateutil.tz.tzlocal()).timestamp()
-        # skip the post if its source file was created more than three
-        # days ago, or the date registered in its filename is more than
-        # three days ago
-        if max(current_time - ctime, current_time - cdate) > 3 * 24 * 3600:
+        # get post date from the date metadata field of the post
+        postdate = datetime.datetime.fromtimestamp(0)
+        with open(fullpath) as postobj:
+            for line in postobj:
+                dateregex = r"^date: (\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}-\d{2}:\d{2})"
+                datematch = re.match(dateregex, line.rstrip())
+                if datematch:
+                    postdate = dateutil.parser.parse(datematch.group(1))
+                    break
+        # skip the post if it is dated more than three days ago
+        if current_time - posttime > 3 * 24 * 3600:
             continue
         if mtime > latest_mtime:
             latest_post = name
             latest_mtime = mtime
     # prompt for touching if the latest post determined above was
-    # modified within an hour
-    if latest_post is not None and current_time - latest_mtime < 3600:
+    # modified within the last hour but the date registered in the post
+    # isn't within the last ten minutes
+    if ((latest_post is not None and current_time - latest_mtime < 3600 and
+         current_time - postdate > 60)):
         sys.stderr.write("%sIt appears that %s might be a new post.\n"
                          "Do you want to touch its timestamp?%s\n" %
                          (GREEN, latest_post, RESET))
-- 
cgit v1.2.1