Home > Experiments, python > Python Script to backup tweets

Python Script to backup tweets

It was some conversations in Twitter with @aashiks and @geohacker that made me write this script. The conversation goes like this:

geohacker : What is the best way to archive my twitter timeline?
            I think I'm loosing several precious tweets! #help #please
ershus    : Copy-paste ? ;)
aashiks   : @geohacker ask @ershus to write a python script and
            retrieve data using the twitter api
ershus    : @aashiks My god! ഞാന്‍ ഒന്നും പറഞ്ഞില്ല ! ദയവായ് ക്ഷമിക്കൂ.....
            PS: വിടാന്‍ ഉദ്ദേശമില്ല, ശ്രമിക്കാം :) ( @geohacker )
aashiks   : @geohacker - kandallo ? thats how you do it :D
            @ershus vegam V0.1 erakku :D

Well, this little script is born! Thanks to them, else I would not have written anything like this.

This is my first ‘working’ python script and I’m very happy to share with you. I’ve just started using python,  you may be able to suggest a lot of improvements in the code. Please help me by pointing out the mistakes and other suggestions.

This script uses python-twitter library,  please make sure it’s installed before running the code.

Thanks to Rajeesh ettan who helped me with unicode issues and time.sleep() 🙂

Here’s the code:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

# pyTweetBackUp.py
# Version 1.0
# Copyright (C) 2010 - Ershad K   ershad92@gmail.com
# Licensed under GPL Version 3

import os, sys, codecs
import time
import twitter

# Change the following values
username = 'USERNAME'
password = 'PASSWORD'
backup_file = 'twitterbackfile' #File name or Path + File name
sleep_time = 5 #seconds

fout = codecs.open(backup_file, 'a') #to create such a file
api = twitter.Api(username, password)

while (True):
	timeline = api.GetFriendsTimeline(username)
	for s in timeline:
		#print "%s\t%s" % (s.user.name, s.text)
		tweet = s.user.name + "\t" + s.text
		fin = open(backup_file, 'r')
		x = fin.read()
		y = x.find(tweet.encode("utf-8"))

		if y < 0:
			fout = codecs.open(backup_file, 'a')
			text = s.user.name + "\t" + s.text + "\n"

Thank you 🙂

  1. June 6, 2010 at 10:46 am

    Woohoo! Good work!

  2. June 7, 2010 at 12:22 pm

    Nice script, I will try and install and see how it goes.
    Thanks for sharing.

    • June 7, 2010 at 6:35 pm

      Thank you for the comment 🙂

  3. June 8, 2010 at 1:23 am

    For comparison, there is also http://code.activestate.com/recipes/576594-backupdownload-your-tweets-or-anyones-tweets/

    It would be cool to see your recipe up there too. Your use of a real twitter API client to parsing tag soup will be much more reliable. 🙂

  4. June 8, 2010 at 1:29 pm

    You are always like this. Everything happens all of a sudden. Happy to see this script from a ‘beginner in python’ like you. Go ahead. I will try this script.

    • June 8, 2010 at 6:36 pm

      Thank you, arju 🙂

  5. Selvin Josy Bai
    June 9, 2010 at 3:58 pm

    Wonderful work!! All the best for your future projects.

    • June 9, 2010 at 4:16 pm

      Thank you very much, Sir 🙂

  6. June 10, 2010 at 9:17 pm

    You, a beginner in python? , no i wont accept this! this script would not be from a beginner! – Rinaldo

    • June 10, 2010 at 9:26 pm

      Thank you, Rinaldo 🙂

  7. John Peter
    June 15, 2010 at 11:20 am

    is there anyway to back all my tweets? this program only gets the latest tweets. How do i do that?

  8. January 4, 2011 at 8:02 am

    Seems not to work anymore, I get: twitter.TwitterError: Twitter requires oAuth Access Token for all API access

    Did anyone solve this?

    • January 4, 2011 at 6:42 pm

      You get this error because twitter now supports only OAuth. The issue is solved by using tweepy module, which supports OAuth. Need to search for the modified code, would post it soon 🙂

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: