forked from abhishek9sharma/TwitterAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetdata.py
204 lines (166 loc) · 9.86 KB
/
getdata.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import tweepy
import json
import time
import math
import os
import random
class TweetExtractor:
def __init__(self, minlikes = 2, mincharlen = 16 , tweetstofind = 250, configfilepath = 'config.json', datafolder = 'Data/', idsprocessedfile = "idsprocessedalready.txt", writeheader = False):
try:
self.minlikes = minlikes
self.tweetstofind = tweetstofind
self.mincharlen = mincharlen
self.config = self.ReadConfig(configfilepath)
self.datafolder = datafolder
self.apiDict = self.InitiateAPIHAndlers()
self.rangeoflikes = {'min': {'val': math.inf, 'id':''}, 'max': {'val': -math.inf, 'id':''}}
self.idsprocessedfile = idsprocessedfile
if os.path.exists(self.idsprocessedfile):
idfileobj = open(self.idsprocessedfile,'r')
self.tweetidsprocessed = [str(id.strip()) for id in idfileobj.readlines()]
idfileobj.close()
else:
open(self.idsprocessedfile,'a').close()
self.tweetidsprocessed =[]
#self.maxid = max(self.tweetidsprocessed)
self.headerwritten = writeheader
except:
print ("some error occured while initializing tweet extrction module")
def InitiateAPI(self, auth):
return tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
def InitiateAPIHAndlers(self):
cnt =0
apiDict ={}
for configinfo in self.config:
auth = tweepy.OAuthHandler(configinfo['CONSUMER_KEY'],configinfo['CONSUMER_SECRET'])
auth.set_access_token(configinfo['ACCESS_TOKEN'],configinfo['ACCESS_SECRET'])
api=self.InitiateAPI(auth)
apiDict[cnt]=api
cnt+=1
return apiDict
def ReadConfig(self, configpath):
with open(configpath, 'r') as f:
config = json.load(f)
return config
def IsValidTweet(self, tweetjson):
favorite_filter = int(tweetjson['favorite_count'])>=self.minlikes
char_filter = len(tweetjson['full_text'])>=self.mincharlen
#isRT = 'retweeted_status' in tweetjson
return favorite_filter & char_filter #& not(isRT)
def UpdateRange(self,likes,id):
if int(likes)<self.rangeoflikes['min']['val']:
self.rangeoflikes['min']['val'] = likes
self.rangeoflikes['min']['id'] =id
elif int(likes)>self.rangeoflikes['max']['val']:
self.rangeoflikes['max']['val'] = likes
self.rangeoflikes['max']['id'] =id
else:
pass
def WriteToJSONFile(self,data,filename, foldername):
with open(self.datafolder + foldername +filename+'.json','a') as fo:
json.dump(data,fo)
def ExtractDataToWrite(self,tweetjson):
tweetid = tweetjson['id_str']
tweetcontent = tweetjson['full_text'].replace(',','')
likes = str(tweetjson['favorite_count'])
langauge = tweetjson['lang']
retweets = str(tweetjson['retweet_count'])
charlen = str(len(tweetjson['full_text']))
textline = ",".join([tweetid, tweetcontent, likes, langauge, charlen, retweets])
return textline
def WriteToTextFile(self,data,filename):
with open(filename,'a') as fo:
fo.write(data.replace('\n','').strip() +'\n')
def CountAlreadyProcessed(self, searchkeyword):
filesprocessedalready = os.listdir(self.datafolder+'/JSON/')
alreadyprocessed = [int(f.split('_')[1].split('.')[0]) for f in filesprocessedalready if searchkeyword in f]
if len(alreadyprocessed)>0:
max_id_curr_search = min(alreadyprocessed)
else:
max_id_curr_search = NotImplementedError
countalreadyprocessed = len(alreadyprocessed)
return countalreadyprocessed, max_id_curr_search
def ExtractTweets(self, searchkeyword):
#self.tweetidsprocessed = []
count_processed_before, max_id_curr_search = self.CountAlreadyProcessed(searchkeyword)
#override already processed jsons delete later
# filesprocessedalready = os.listdir(self.datafolder+'/JSON/')
# self.tweetidsprocessed = [str(f.split('_')[1].split('.')[0]) for f in filesprocessedalready]
tweetlimit = self.tweetstofind - count_processed_before
if tweetlimit ==0 :
print( " Have already found " + str(self.tweetstofind) + " for hashtag " + searchkeyword)
return
#tweetlimit = 5
twtcntvalid = 0
twtcounttotal =0
last_proceesed_id = None
while(twtcntvalid<tweetlimit):
try:
#for idx, api in self.apiDict.items():
apikeys = list(self.apiDict.keys())
random.shuffle(apikeys)
for idx in apikeys:
api = self.apiDict[idx]
try:
#maxid is buggy check later if fixed can speed up data collection
for page in tweepy.Cursor(api.search, q = searchkeyword, count=100, tweet_mode='extended', max_id = None).pages():
try:
#print(str(len(page)) + " tweets found in page " )
for tweetstatus in page:
tweetjson = tweetstatus._json
tweetid = tweetjson['id_str']
if tweetid==str(max_id_curr_search):
print("Cannot find tweets behhin the current max id i.e ", + tweetid)
return twtcntvalid
else:
max_id_curr_search = int(tweetid)
if tweetid not in self.tweetidsprocessed:
self.tweetidsprocessed.append(tweetid)
if self.IsValidTweet(tweetjson):
self.UpdateRange(tweetjson['favorite_count'],tweetid)
self.WriteToJSONFile(tweetjson, searchkeyword +'_'+ tweetid, '/JSON/')
if not(self.headerwritten):
#has bugrewrites header when restarting job
self.WriteToTextFile('tweetid,content,likes,language,retweets,charlen,HT', self.datafolder + 'alltweets.txt')
self.headerwritten = True
self.WriteToTextFile(self.ExtractDataToWrite(tweetjson) +","+ searchkeyword, self.datafolder + 'alltweets.txt')
twtcntvalid+=1
twtcounttotal+=1
if twtcntvalid==tweetlimit:
print(" Finally Found ", twtcntvalid, " tweets in ", twtcounttotal, "tweets that were checked for HT", searchkeyword)
return twtcntvalid
else:
self.WriteToJSONFile(tweetjson, searchkeyword +'_'+ tweetid, '/INVALID/')
self.tweetidsprocessed.append(tweetid)
twtcounttotal+=1
self.WriteToTextFile(tweetid, self.idsprocessedfile)
if twtcounttotal%500==0 and twtcounttotal>0:
print( " proceesed ", twtcounttotal, " tweets for ", searchkeyword, " and have found ", twtcntvalid, " valid tweets")
if twtcntvalid%60==0 and twtcntvalid>0:
print( " Found ", twtcntvalid, " valid tweets for ", searchkeyword, "among total tweets ", twtcounttotal)
if twtcounttotal%200==0 and twtcounttotal>0:
print( " proceesed ", twtcounttotal, " tweets for ", searchkeyword, " and have found ", twtcntvalid, " valid tweets")
time.sleep(10)
except Exception as e:
print("Some error occured while processing tweet with id " + tweetid + " :: " + str(e))
except tweepy.TweepError as e:
print("Error")
print( " Error occured, moving to next API Interface " + str(e.message))
pass
except:
print(" error occured while extracting tweets using ", idx, api)
return 0
# #Collect Data
# #htlist = ['Service', 'price', 'cost', 'quality', 'ambiente', 'reservation']
# htlist = ['cost']
# data_collector = TweetExtractor(minlikes = 2, mincharlen = 16, tweetstofind = 300, configfilepath= 'config.json', datafolder= 'Data/', idsprocessedfile ='idsprocessedalready.txt', writeheader = False)
# data_collector.InitiateAPIHAndlers()
# for ht in htlist:
# print( "Collecting Tweets for hashtag #" + ht)
# print(str(data_collector.ExtractTweets('#'+ht)) + " tweets extracted for hashtag #" + ht)
# print()
# #Print Range
# minlikes = data_collector.rangeoflikes['min']
# maxlikes = data_collector.rangeoflikes['max']
# print (' Minimum Likes found are ', minlikes['val'], " for tweet id ", minlikes)
# print (' Maximum Likes found are ', maxlikes['val'], " for tweet id ", maxlikes)