-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_tweets.py
363 lines (279 loc) · 14.1 KB
/
get_tweets.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from logging import raiseExceptions
import sys
import csv
import pandas as pd
import numpy as np
#http://www.tweepy.org/
import tweepy as tw
# The fields you can select on tweets are found here :
# https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet
# The fields you can select on users are found here :
# https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/user
#Get your Twitter API credentials and enter them here
api_key = json.load(open('credentials.json'))["second_key"]
#method to get a user's last tweets
def get_tweets(username, number_of_tweets):
# identification
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth) #If you wish to have the application sleep when
#it hits a rate limityou should instantiate the API with sleep_on_rate_limit=True
#set count to however many tweets you want
number_of_tweets = number_of_tweets
#get tweets
tweets_for_csv = []
for tweet in tw.Cursor(api.user_timeline, screen_name = username).items(number_of_tweets):
#create array of tweet information: username, tweet id, date/time, text
tweets_for_csv.append([username, tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")])
#write to a new csv file from the array of tweets
outfile = username + "_tweets.csv"
print ("writing to " + outfile)
with open(outfile, 'w+') as file:
writer = csv.writer(file, delimiter=',')
writer.writerows(tweets_for_csv)
def get_tweets_per_topic(topic,year,month,day,number_of_tweets):
"""
args : topic, year, month , day are Strings, number_of_tweets is an int
year is like '2020', 'month' is among ['01',...'12'], same for days
"""
#Giving nb of rows to pre-allocate and be memory efficient
df = pd.DataFrame(index=np.arange(0, number_of_tweets), columns=['id', 'date', 'text'])
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth)
#geo_code can be a parameter
tweets = tw.Cursor(api.search_tweets,
q=topic,
lang="en",
since=year+'-'+month+'-'+day, ).items(number_of_tweets)
tweets_topics_csv = []
for i, tweet in enumerate(tweets):
#tweets_topics_csv.append([tweet.id_str, tweet.created_at, tweet.text.encode("utf-8")])
df.loc[i] = [tweet.id_str, tweet.created_at, tweet.text]
return df
def get_tweets_from_people(usernames,number_of_tweets):
"""
"""
#Giving nb of rows to pre-allocate and be memory efficient
df = pd.DataFrame(index=np.arange(0, len(usernames) * number_of_tweets), columns=['id', 'date', 'text','retweet_count'])
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth,wait_on_rate_limit=True,
)
#geo_code can be a parameter
for k, username in enumerate(usernames):
try:
tweets = tw.Cursor(api.user_timeline, screen_name = username).items(number_of_tweets)
for i, tweet in enumerate(tweets):
df.loc[k*number_of_tweets + i] = [tweet.user.name, tweet.created_at, tweet.text, tweet.retweet_count]
except:
print("\n....There has beenproblem with the name of one twittos in the list...")
pass
return df
def get_tweets_with_comments(usernames,number_of_tweets):
df = pd.DataFrame(index=np.arange(0, len(usernames) * number_of_tweets), columns=['id','name', 'date', 'text','comments','retweet_count'])
#comments is a list
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth,wait_on_rate_limit=True,
)
for k, username in enumerate(usernames):
try:
tweets = tw.Cursor(api.user_timeline, screen_name = username).items(number_of_tweets)
for i, tweet in enumerate(tweets):
name = str(tweet.user.name)
tweet_id = tweet.id
replies=[]
if tweet.is_quote_status:
print("is quote")
for comment in tw.Cursor(api.search_tweets,q=name, result_type='recent').items(1000):
if hasattr(comment, 'in_reply_to_status_id'):
if (comment.in_reply_to_status_id==tweet_id):
print("on a trouvé")
replies.append([str(comment.text), str(comment.user.name)])
df.loc[k*number_of_tweets + i] = [tweet.id,tweet.user.name, tweet.created_at, tweet.text, replies, tweet.retweet_count]
else:
df.loc[k*number_of_tweets + i] = [tweet.id,tweet.user.name, tweet.created_at, tweet.text, replies, tweet.retweet_count]
except:
print("\n....There has beenproblem with the name of one twittos in the list...")
pass
return df
def get_tweets_dataframe(topics,year,month,day,number_of_tweets):
"""
args : topics, year, month , day are Strings, number_of_tweets is an int
year is like '2020', 'month' is among ['01',...'12'], same for days
"""
#Giving nb of rows to pre-allocate and be memory efficient
df = pd.DataFrame(index=np.arange(0, len(topics) * number_of_tweets), columns=['id', 'date', 'text'])
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth)
#geo_code can be a parameter
for k, topic in enumerate(topics):
tweets = tw.Cursor(api.search_tweets,
q=topic,
lang="en",
since=year+'-'+month+'-'+day, ).items(number_of_tweets)
for i, tweet in enumerate(tweets):
df.loc[k*number_of_tweets + i] = [tweet.id_str, tweet.created_at, tweet.text]
return df
def test_query(username, number_of_tweets):
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth,wait_on_rate_limit=True,
)
# tweets = tw.Cursor(api.user_timeline, screen_name = username).items(number_of_tweets)
name = str("NYU Stern")
tweet_id = 1442487073731190786
replies=[]
for comment in tw.Cursor(api.search_tweets,q="to:"+name, result_type='recent', ).items(100):
if hasattr(comment, 'in_reply_to_status_id'):
print("on a trouvé")
if comment.in_reply_to_status_id!=None:
print(str(comment.in_reply_to_status_id)+" vs "+ str(tweet_id))
if (comment.in_reply_to_status_id==tweet_id):
print("les tweets collent")
replies.append([str(comment.text), str(comment.user.name)])
print(replies)
def get_tweet_comments(screen_name="anne_sinclair", number_of_tweets= 5):
#accessing to the number of replies is impossible
#we assume the number of retweets is close to the number of replies
#because generaly there differ from a factor of 1 to 9
"""
args:
screen_name : string, its the twitter unique tag of the account
number_of_tweets: integer, the number of most recent tweets we need
outputs:
tuple: (dict of tweets, dict of comments per tweet)
"""
auth = tw.OAuthHandler(api_key['consumer_key'], api_key['consumer_secret'])
auth.set_access_token(api_key['access_key'], api_key['access_secret'])
api = tw.API(auth,wait_on_rate_limit=True,
)
tweets_dict = {}
replies = {}
good_replies = {}
#faire le edge case du tweet si c'est un retweet
for tweet in tw.Cursor(api.user_timeline, screen_name= screen_name,).items(number_of_tweets):
try :
if not (tweet.retweeted):
id = tweet.id
retweet_count = tweet.retweet_count
tweets_dict[str(id)] = {
"isComment": False,
"tweet_id": str(tweet.id),
"text": tweet.text,
"date": str(tweet.created_at),
"username": tweet.user.screen_name,
"retweeted_status": retweet_count!=0,
"retweet_count": retweet_count,
"likes": tweet.favorite_count,
# "entities": tweet.entities,
}
tweet_comments_dict = {}
comments_ids = []
#it works better with "to"
for comment in tw.Cursor(api.search_tweets,q="to:"+screen_name, result_type='recent', ).items(retweet_count):
if hasattr(comment, 'in_reply_to_status_id'):
if comment.in_reply_to_status_id!=None:
if (comment.in_reply_to_status_id==id):
comment_count = comment.retweet_count
comments_ids.append(str(comment.id))
reply = {
"isComment": True,
"repliesTo": screen_name,
"tweet_id": str(id),
"text": comment.text,
"date": str(comment.created_at),
"username": comment.user.screen_name,
"retweeted_status": comment_count!=0,
"retweet_count": comment_count,
"likes": comment.favorite_count,
# "entities":comment.entities,
}
tweet_comments_dict[str(comment.id)] = reply
good_replies[str(comment.id)] = reply
tweet_comments_dict["number of replies found"] = len(tweet_comments_dict.keys() )
replies[str(id)] = tweet_comments_dict
tweets_dict[str(id)]["comments"] = replies[str(id)]
tweets_dict[str(id)]["comments_ids"]= comments_ids
except:
pass
return tweets_dict, replies, good_replies
def scrap_topic(topic="world_leaders"):
scrapped_tweets = get_tweet_comments(number_of_tweets=1)
comments_df = pd.DataFrame.from_dict(scrapped_tweets[1])
tweets_df = pd.DataFrame.from_dict(scrapped_tweets[0])
comments_df.to_excel('dataframes_for_recommenders_training/comments_df1.xlsx')
tweets_df.to_excel('dataframes_for_recommenders_training/tweets_df1.xlsx')
return comments_df, tweets_df
def get_tweets_from(users = ["anne_sinclair"], name_of_the_list ="anne_sinclair", isDf= True, number_of_tweets=1):
"""
The users list has to be maximally between 20 and 5 users, depending on the average number of retweets
Complexity is O(n^3) and is really prevented by Twitter API.
Each 15min you can make maximum 1000 queries.
"""
path = "df_for_training_recommenders/"
if isDf:
dfs_tweets = []
dfs_good_comments = []
dfs_fulls = []
for user in users:
tweets_dict, comments_dict, good_comments = get_tweet_comments(user, number_of_tweets=number_of_tweets)
df_tweets = pd.DataFrame.from_dict(tweets_dict)
df_good_comments = pd.DataFrame.from_dict(good_comments)
df_full = pd.concat([df_tweets,df_good_comments],axis=1)
dfs_tweets.append(df_tweets)
dfs_good_comments.append(df_good_comments)
dfs_fulls.append(df_full)
# taking the transpose of the dataframes
dfs_tweets = pd.concat(dfs_tweets, axis=1).T
dfs_good_comments = pd.concat(dfs_good_comments, axis=1).T
dfs_fulls = pd.concat(dfs_fulls, axis=1).T
# saving dfs to csv
dfs_tweets.to_csv(path+"df_tweets"+"_"+name_of_the_list+'.csv')
dfs_good_comments.to_csv(path+"df_good_comments"+"_"+name_of_the_list+'.csv')
dfs_fulls.to_csv(path+"df_all"+"_"+name_of_the_list+'.csv')
else :
with open('tweets_and_comments'+name_of_the_list+'.json', 'w') as fp:
big_dict = {
}
for user in users:
tweets_dict, comments_dict, good_comments_dict = get_tweet_comments(user, number_of_tweets=1)
big_dict[user] = {
"tweets_dict": tweets_dict,
"comments_dict": comments_dict,
"good_comments_dict": good_comments_dict
}
json.dump(big_dict, fp)
#if we're running this as a script
if __name__ == '__main__':
number_of_tweets = 40
file = open("dict.json", "r")
data = json.loads(file.read())
topics = list(data.keys())
# print(get_tweet_comments(number_of_tweets=1)[1])
# get_tweets_from(data["entrepreneurs"],"entrepreneurs")
get_tweets_from(["anne_sinclair","leadlagreport"], "try", number_of_tweets=10)
# get_tweets_df()
# new_df = get_tweets_with_comments(data[topics[-2]],10)
# new_df.to_csv("good_df"+str(topics[-2])+'.csv', index=False)
# test_query("NYU Stern",100)
# for topic in topics:
# print("The topic scrapped is : "+ topic)
"""
to scrap people on precised fields
"""
# full_df = get_tweets_from_people(data["entrepreneurs"],number_of_tweets=number_of_tweets)
# full_df.to_csv("df_on_"+str("entrepreneurs")+'.csv', index=False)
"""
"""
# file.close()
# got = get_tweet_comments(number_of_tweets=1)
# my_dict = got[0]
# my_comments = got[1]
# print(my_comments)
# print(list(my_dict.values()))