-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorganic_tweets.py
179 lines (163 loc) · 6.47 KB
/
organic_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
import os
import linecache
import random
import twitter
import time
import pickle
import gc
import boto3
import time
import pandas as pd
from tqdm import tqdm
from datetime import datetime
from dotenv import load_dotenv, find_dotenv
load_dotenv(find_dotenv())
# Globals
SAMPLE_SIZE = 100000
# Date / Time Utils
fmt = '%Y-%m-%d %H:%M:%S'
START_TIMESTAMP = time.strptime("2014-07-14 18:04:55", fmt)
END_TIMESTAMP = time.strptime("2017-09-26 09:05:32", fmt)
in_range = lambda ts: ts >= START_TIMESTAMP and ts <= END_TIMESTAMP
to_iso = lambda ts: time.strptime(ts,'%a %b %d %H:%M:%S +0000 %Y')
to_str = lambda ts: time.strftime('%Y-%m-%d %H:%M:%S', ts)
# Get a set of tweet ids
def get_tweet_id_samples(sample_size=SAMPLE_SIZE):
try:
tweet_ids = pickle.load(open(f'data/tweet_ids', "rb"))
# If tweets, not picked, create pickle
except (OSError, IOError) as e:
TOTAL_LINES = 251376589
sampled, sampled_lines = set(), {i: [] for i in range(1, 7)}
# Get a sample of file lines associated with tweet ids data
for _ in range(sample_size):
line_number = random.randrange(1, TOTAL_LINES + 1)
while line_number in sampled:
line_number = random.randrange(1, TOTAL_LINES + 1)
sampled.add(line_number)
file_number = line_number // 50000000 + 1
line_number = line_number - (50000000 * (file_number - 1))
sampled_lines[file_number].append(line_number)
sampled_lines = {i: sorted(sampled_lines[i]) for i in sampled_lines}
tweet_ids = set()
# Get tweet ids from file lines
for i in tqdm(range(1, 7)):
f = open(f'data/election-filter{i}.txt', 'r')
lines = f.readlines()
for l in sampled_lines[i]:
tweet_ids.add(lines[l-1].strip())
f.close()
del lines
del f
gc.collect()
pickle.dump(tweet_ids, open(f'data/tweet_ids', "wb"))
return tweet_ids
tweet_ids = get_tweet_id_samples()
twitter_api = twitter.Api(consumer_key=os.getenv('TW_CONSUMER_KEY'),
consumer_secret=os.getenv('TW_CONSUMER_SECRET'),
access_token_key=os.getenv('TW_ACCESS_TOKEN_KEY'),
access_token_secret=os.getenv('TW_ACCESS_TOKEN_SECRET'),
sleep_on_rate_limit=True)
def get_user_id(tweet_id):
try:
# get user from tweet
tweet = twitter_api.GetStatus(tweet_id)
user_id = tweet.user.id
return user_id
except twitter.error.TwitterError:
pass
def get_tweets_data(user_id):
try:
# exhaust twitter api for full user timeline
user_timeline = []
tl = twitter_api.GetUserTimeline(user_id, count=200)
user_timeline.extend(tl)
while len(tl) > 0:
oldest = user_timeline[-1].id - 1
tl = twitter_api.GetUserTimeline(user_id, count=200, max_id=oldest)
user_timeline.extend(tl)
# filter user timeline
user_timeline = [tw for tw in user_timeline if
in_range(to_iso(tw.created_at))]
return user_timeline
except twitter.error.TwitterError:
pass
def get_user_data(user_id):
try:
u = twitter_api.GetUser(user_id)
return u
except twitter.error.TwitterError as e:
pass
# dynamodb = boto3.resource('dynamodb', region_name='us-east-1',
# aws_access_key_id=os.getenv('AWS_ACCESS_KEY'),
# aws_secret_access_key=os.getenv('AWS_SECRET_KEY'))
# tweets_tb = dynamodb.Table('election_tweets_tb')
tweets_df = pd.read_csv('data/organic_tweets.csv') # pd.DataFrame(data=users_data)
users_df = pd.read_csv('data/organic_users.csv') # pd.DataFrame(data=users_data)
users = set(users_df['id'].unique())
CAP = 2000
users_data = []
tweets_data = []
def data_to_csv():
last_user = users_data[-1]
users_new = pd.DataFrame(data=users_data[:-1])
tweets_new = pd.DataFrame(data=tweets_data)
users_new['id'] = users_new['id'].astype(str)
tweets_new['user_id'] = tweets_new['user_id'].astype(str)
tweets_new['tweet_id'] = tweets_new['tweet_id'].astype(str)
tweets_new = tweets_new[tweets_new['user_id'] != str(last_user)]
t = pd.concat([tweets_df, tweets_new], ignore_index=True)
u = pd.concat([users_df, users_new], ignore_index=True)
t.to_csv('data/organic_tweets.csv', index=False)
u.to_csv('data/organic_users.csv', index=False)
try:
for tweet_id in tqdm(list(tweet_ids)[1500:CAP]):
user_id = get_user_id(tweet_id)
if user_id in users:
continue
users.add(user_id)
u = get_user_data(user_id)
if not u:
continue
u_tweets_data = get_tweets_data(user_id)
if not u_tweets_data or len(u_tweets_data) == 0:
continue
user = {
'id': str(u.id),
'location': u.location,
'name': u.name,
'followers_count': u.followers_count,
'statuses_count': u.statuses_count,
'time_zone': u.time_zone,
'verified': u.verified,
'lang': u.lang,
'screen_name': u.screen_name,
'description': u.description,
'created_at': u.created_at,
'favourites_count': u.favourites_count,
'friends_count': u.friends_count,
'listed_count': u.listed_count
}
users_data.append(user)
# for every tweet a user has sent
for t in u_tweets_data:
# write tweet to dynamodb database
tweet = {
'tweet_id': str(t.id),
'user_id': str(t.user.id),
'user_key': t.user.screen_name,
'created_at': None,
'created_str': to_str(to_iso(t.created_at)),
'retweet_count': t.retweet_count,
'retweeted': t.retweeted,
'favorite_count': t.favorite_count,
'text': t.text,
'hashtags': str([h.text for h in t.hashtags]),
'mentions': str([u.screen_name for u in t.user_mentions]),
'in_reply_to_status_id': str(t.in_reply_to_status_id)
}
tweets_data.append(tweet)
except KeyboardInterrupt:
data_to_csv()
exit()
data_to_csv()