-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCollectTweetsFromFollowerList.py
52 lines (42 loc) · 1.56 KB
/
CollectTweetsFromFollowerList.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
@author: Sayali
"""
import tweepy
import json
import time
consumer_key='FJkeW0VV0D6HGPYlF5UfklTK5'
consumer_secret='IM8zRQFIq4wbKBgikZKNLqiEkHH6ePSg20Ag6bE1QLY6dIQPGM'
access_token_key='4921031892-twRpm76J6kgd3cWp2d4dIMkp674ocaggbQiUgCX'
access_token_secret='nTPkL7TXTD4winCFu8INTzdE6ALAIYNk9Tb39d4R0DgYS'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
myApi = tweepy.API(auth)
outputFile = open("FollowersTweets_NYCAllergyDr.txt", "a+")
statsFile = open("Stats.txt", "a+")
def GetTweets(user_id):
try:
alltweets = []
#Collect most recent 200 tweets
new_tweets = myApi.user_timeline(id=user_id,count=200)
alltweets.extend(new_tweets)
#save the id of the lastest tweet collected
MAX_ID = alltweets[-1].id - 1
#Collect data till last tweets
while len(new_tweets) > 0:
new_tweets = myApi.user_timeline(id=user_id, count=200, max_id=MAX_ID)
alltweets.extend(new_tweets)
MAX_ID = alltweets[-1].id - 1
for tweet in alltweets:
outputFile.write(json.dumps(tweet._json) + "\n")
#pass user id if user has protedted tweets.
except:
pass
if __name__ == '__main__':
inputFile = open("ListOfFollowers_NYCAllergyDr.txt")
startTime = time.time()
for line in iter(inputFile):
GetTweets(line)
inputFile.close()
endTime = time.time()
print endTime - startTime