When the script is run, Lines 35-36 open that file and read the number in it. This is handled by storing the ID number of the last downloaded tweet in a file named lastID.txt, kept in the same folder as the archive file itself. The local file parameters define where the tweet archive is, and the date/time parameters define how the dates will be formatted in the archive.Ī key feature of the script is knowing how far back in my user timeline to look for new tweets. It’s not a big deal I explained the steps in an earlier post. If you want to run your own version of this program, you’ll have to get your own keys and secrets from Twitter’s developer site. The keys and secrets shown are obviously not the ones I really use. The Twitter parameters are my user name and the set of keys and secrets needed to interact with the Twitter API. The first few sections define the parameters that control the way the script works. Here’s the script, called archive-tweets.py: python:ġ6: tweetdir = os.environ '/Dropbox/twitter/'Ģ5: # This function pretty much taken directly from a tweepy example.Ģ7: auth = tweepy.OAuthHandler(consumerKey, consumerKeySecret)Ģ8: t_access_token(accessToken, accessTokenSecret)ģ4: # Get the ID of the last downloaded tweet.ģ8: # Collect all the tweets since the last one.ģ9: tweets = api.user_timeline(me, since_id=lastID)Ĥ1: # Write them out to the twitter.txt file.Ĥ4: ts = utc.localize(t.created_at).astimezone(homeTZ)ĥ1: f.write('\n'.join(lines).encode('utf8'))ĥ4: # Update the ID of the last downloaded tweet. Tweepy is nice in that it automatically handles conversions between the pure text that the Twitter API deals in and the objects-integers and datetime objects, in particular-that Python understands and can manipulate directly. I used tweepy a while ago in a script that posts tweets with images. I wrote my own because I wanted one written in a language (Python) and using a Twitter library ( tweepy) with which I’m familiar. Plenty of people have written scripts to do this. Since I have no control over IFTTT, my preference would be to use my own archiving script sitting on my own hard disk. The way I left it, I was using the IFTTT web service to append new tweets to a text file in my Dropbox folder. Here’s a final bit of follow-up on the flurry of posts about Twitter and archiving tweets that I wrote early in the month. Next post Previous post Archiving tweets without IFTTT
0 Comments
Leave a Reply. |