Skip to content

Instantly share code, notes, and snippets.

@ngsankha
Created June 2, 2012 09:08
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ngsankha/2857462 to your computer and use it in GitHub Desktop.
Save ngsankha/2857462 to your computer and use it in GitHub Desktop.
Creates an archive of tweets as a git repo

This Python script creates a git repo with your tweets. Obviously its limited by Twitter API limitations as you cannot retrieve more than 3200 tweets at a time.

Given the fact that Twitter doesn't give a fuck about our data, you may not find your old tweets ever again. So nothing is better than keeping an archive of tweets as a Git repo.

This is inspired by @holman's tweets repo. But this doesn't have a dependency on Madrox. Just plain Python and Git.

Usage

To create an archive of tweets initiate a git repo in a directory with git init. Then put the script in the directory and execute:

python mytweets.py username

This will update your archive. To view your tweets anytime use git log.

# mytweets.py - Creates an archive of your tweets as a Git repo
# Sankha Narayan Guria <sankha93@gmail.com>
import sys, httplib, os.path, json, subprocess
con = httplib.HTTPConnection("api.twitter.com")
def getTweets(num):
request = "";
if(num == -1):
request = "/1/statuses/user_timeline.json?screen_name=" + sys.argv[1] + "&count=200&include_rts=1&trim_user=1"
else:
request = "/1/statuses/user_timeline.json?screen_name=" + sys.argv[1] + "&count=200&include_rts=1&trim_user=1&since_id=" + num
return doRequest(request)
def doRequest(request):
con.request("GET", request)
r = con.getresponse()
return json.load(r)
def processTweets(obj):
if(len(obj) == 200):
maxid = (obj[-1])['id'] - 1
if(tweet_id == -1):
request = "/1/statuses/user_timeline.json?screen_name=" + sys.argv[1] + "&count=200&include_rts=1&trim_user=1&max_id=" + str(maxid)
else:
request = "/1/statuses/user_timeline.json?screen_name=" + sys.argv[1] + "&count=200&include_rts=1&trim_user=1&max_id=" + str(maxid) + "&since_id=" + tweet_id
processTweets(doRequest(request))
for tweet in reversed(obj):
f = open("tweet_id",'w')
f.write(tweet['id_str'])
f.close()
subprocess.call(["git", "add", "."])
subprocess.call(["git", "commit", "--date", tweet['created_at'], "-m", tweet['text']])
if len(sys.argv) > 1:
if(os.path.exists("tweet_id")):
tweet_id = open("tweet_id", 'r').read()
else:
tweet_id = -1
processTweets(getTweets(tweet_id))
else:
print("Usage: python mytweets.py username")
@perry
Copy link

perry commented Jan 22, 2013

WARNING

This will notify everyone whose twitter handle is the same as their github handle. Annoyed a lot of people...

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