#-*-coding:utf8 -*-
import tweepy
import sys
import time
import os
def SaveTweets(users=[]):
creds={}
creds[\'CONSUMER_KEY\']=\'\'
creds[\'CONSUMER_SECRET\']=\'\'
creds[\'ACCESS_KEY\']=\'\'
creds[\'ACCESS_SECRET\']=\'\'
creds[\'screen_name\']=\'\'
auth = tweepy.OAuthHandler(creds[\'CONSUMER_KEY\'], creds[\'CONSUMER_SECRET\'])
auth.set_access_token(creds[\'ACCESS_KEY\'], creds[\'ACCESS_SECRET\'])
api=tweepy.API(auth)
if not users:
me = api.get_user(creds[\'screen_name\'])
users = api.friends_ids(creds[\'screen_name\'])
wdir = \'Tweets\'
id_file = \'users.last_ids\'
timeline_file = \'tweets.timeline\'
since_id=1
since={}
if os.path.exists(wdir + \'/\' + id_file):
f = open(wdir + \'/\' + id_file, \'r\')
for l in f.readlines():
user = l.split()[0].strip()
last_id = l.split()[1].strip()
since[user]=last_id
f.close()
else:
me = api.get_user(creds[\'screen_name\'])
friends = api.friends_ids(creds[\'screen_name\'])
for friend in friends:
since[friend]=1
f = open(wdir + \'/\' + timeline_file, \'a+\')
last_id={}
for user in users:
if str(user) in since.keys():
tweets = api.user_timeline(user, since_id=since[str(user)])
else:
tweets=api.user_timeline(user)
if len(tweets) > 0:
last_id[user] = int(str(tweets[0].id))
tweets.reverse()
for tweet in tweets:
output = str(tweet.created_at) + \'\\t\' +str(user)+\'\\t\'+ tweet.text.replace(\'\\n\', \' \').encode(\'utf-8\') + \'\\n\'
f.write(output)
print output
f.close()
# write last id to file
f = open(wdir + \'/\' + id_file, \'w\')
for user in users:
if user in last_id:
f.write("%s %s\\n" % (user, last_id[user]))
SaveTweets()