Created
July 8, 2012 05:29
-
-
Save andrewheiss/3069537 to your computer and use it in GitHub Desktop.
Backupify Tweets to Markdown
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os | |
import glob | |
import json | |
from datetime import datetime | |
import dateutil.parser | |
# Handle Unicode | |
import sys | |
reload(sys) | |
sys.setdefaultencoding('utf-8') | |
# Archive format. | |
single = "%s\n\n[%s](http://twitter.com/andrewheiss/status/%s)\n" | |
path = 'tweets/' | |
tweets = [] | |
for tweet_raw in glob.glob( os.path.join(path, '*.json.json') ): | |
# tweet_data_utf8 = open(tweet_raw).read().decode('utf-8') | |
# tweet = json.loads(tweet_data_utf8) | |
tweet_data = open(tweet_raw) | |
tweet = json.load(tweet_data) | |
tweets.append([dateutil.parser.parse(tweet['created']), tweet['twitter_id'], tweet['text']]) | |
tweets.sort() | |
tweets_formatted = [ single % \ | |
(x[2], x[0].strftime("%B %d, %Y at %I:%M%p"), x[1]) \ | |
for x in tweets ] | |
output = '\n---\n\n'.join(tweets_formatted) | |
output += '\n---' | |
out_file = open('output.md', 'w+') | |
out_file.write(output) | |
out_file.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment