Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jgmarcel
Created July 5, 2012 21:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgmarcel/3056557 to your computer and use it in GitHub Desktop.
Save jgmarcel/3056557 to your computer and use it in GitHub Desktop.
Modified version of Dr. Drang's ThinkUp db -> text file script. Adds timestamp conversion from UTC to local time.
#!/usr/bin/python
import csv
import os
import time
import calendar
import sys
# Put your Twitter username here.
me = "drdrang"
# Archive format.
single = "%s\n%s\nhttp://twitter.com/" + me + "/status/%s"
# Open the CSV file specified on the command line and read the field names.
tfile = open(sys.argv[1])
treader = csv.reader(tfile)
fields = treader.next()
# Fill a list with the tweets, with each tweet a dictionary.
allInfo = []
for row in treader:
allInfo.append(dict(zip(fields,row)))
# Collect only the info we need in a list of lists. Convert the date string
# into a datetime object.
tweets = [ [time.localtime(calendar.timegm(time.strptime(x['pub_date'], "%Y-%m-%d %H:%M:%S"))), \
x['post_id'], x['post_text']] \
for x in allInfo ]
# We put the date first so we can sort by date easily.
tweets.sort()
# Construct a new list of tweets formatted the way the IFTTT recipe does.
out = [ single % \
(x[2], time.strftime("%B %d, %Y at %I:%M%p", x[0]), x[1]) \
for x in tweets ]
print '\n- - - - -\n\n'.join(out)
print '\n- - - - -'
@jgmarcel
Copy link
Author

jgmarcel commented Jul 5, 2012

Diff output:

--- old/tu2ifttt.py 2012-07-05 18:00:00.000000000 -0300
+++ new/tu2ifttt.py 2012-07-05 18:00:00.000000000 -0300
@@ -2,7 +2,8 @@

 import csv
 import os
-from datetime import datetime
+import time
+import calendar
 import sys

 # Put your Twitter username here.
@@ -23,7 +24,7 @@

 # Collect only the info we need in a list of lists. Convert the date string
 # into a datetime object.
-tweets = [ [datetime.strptime(x['pub_date'], "%Y-%m-%d %H:%M:%S"), \
+tweets = [ [time.localtime(calendar.timegm(time.strptime(x['pub_date'], "%Y-%m-%d %H:%M:%S"))), \
             x['post_id'], x['post_text']] \
             for x in allInfo ]

@@ -32,7 +33,7 @@

 # Construct a new list of tweets formatted the way the IFTTT recipe does.
 out = [ single % \
-        (x[2], x[0].strftime("%B %d, %Y at %I:%M%p"), x[1]) \
+        (x[2], time.strftime("%B %d, %Y at %I:%M%p", x[0]), x[1]) \
         for x in tweets ]

 print '\n- - - - -\n\n'.join(out)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment