/
user_profile_collection.py
217 lines (143 loc) · 8.47 KB
/
user_profile_collection.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
import json
import logging
import os
from pathlib import Path
from twython import TwythonError, TwythonRateLimitError
from util.Constants import GET_USER, GET_USER_TWEETS, USER_ID, FOLLOWERS, GET_FRIENDS_ID, FOLLOWING
from util.TwythonConnector import TwythonConnector
from util.util import Config, is_folder_exists, create_dir, multiprocess_data_collection
from util.util import DataCollector
from util.Constants import GET_FOLLOWERS_ID
def get_user_ids_in_folder(samples_folder):
user_ids = set()
for news_id in os.listdir(samples_folder):
news_dir = "{}/{}".format(samples_folder, news_id)
tweets_dir = "{}/{}/tweets".format(samples_folder, news_id)
if is_folder_exists(news_dir) and is_folder_exists(tweets_dir):
for tweet_file in os.listdir(tweets_dir):
tweet_object = json.load(open("{}/{}".format(tweets_dir, tweet_file)))
user_ids.add(tweet_object["user"]["id"])
return user_ids
def dump_user_profile_job(user_id, save_location, twython_connector: TwythonConnector):
profile_info = None
# Fetch and save user information if the file is not already present
if not Path("{}/{}.json".format(save_location, user_id)).is_file():
try:
profile_info = twython_connector.get_twython_connection(GET_USER).show_user(user_id=user_id)
except TwythonRateLimitError as ex:
logging.exception("Twython API rate limit exception")
finally:
if profile_info:
json.dump(profile_info, open("{}/{}.json".format(save_location, user_id), "w"))
def dump_user_recent_tweets_job(user_id, save_location, twython_connector: TwythonConnector):
profile_info = None
# Fetch and save user information if the file is not already present
if not Path("{}/{}.json".format(save_location, user_id)).is_file():
try:
profile_info = twython_connector.get_twython_connection(GET_USER_TWEETS).get_user_timeline(user_id=user_id,
count=200)
except TwythonRateLimitError as ex:
logging.exception("Twython API rate limit exception")
finally:
if profile_info:
json.dump(profile_info, open("{}/{}.json".format(save_location, user_id), "w"))
def fetch_user_follower_ids(user_id, twython_connection):
user_followers = []
try:
user_followers = twython_connection.get_followers_ids(user_id=user_id)
user_followers = user_followers["ids"]
except:
logging.exception("Exception in follower_ids for user : {}".format(user_id))
return user_followers
def fetch_user_friends_ids(user_id, twython_connection):
user_friends = []
try:
user_friends = twython_connection.get_friends_ids(user_id=user_id)
user_friends = user_friends["ids"]
except:
logging.exception("Exception in follower_ids for user : {}".format(user_id))
return user_friends
def dump_user_followers(user_id, save_location, twython_connector: TwythonConnector):
# Fetch and save user information if the file is not already present
if not Path("{}/{}.json".format(save_location, user_id)).is_file():
try:
user_followers = fetch_user_follower_ids(user_id, twython_connector.get_twython_connection(GET_FOLLOWERS_ID))
user_followers_info = {USER_ID: user_id, FOLLOWERS: user_followers}
json.dump(user_followers_info, open("{}/{}.json".format(save_location, user_id), "w"))
except:
logging.exception("Exception in getting follower_ids for user : {}".format(user_id))
def dump_user_following(user_id, save_location, twython_connector: TwythonConnector):
# Fetch and save user information if the file is not already present
if not Path("{}/{}.json".format(save_location, user_id)).is_file():
try:
user_following = fetch_user_friends_ids(user_id, twython_connector.get_twython_connection(GET_FRIENDS_ID))
user_following_info = {USER_ID: user_id,FOLLOWING : user_following}
json.dump(user_following_info, open("{}/{}.json".format(save_location, user_id), "w"))
except:
logging.exception("Exception in getting follower_ids for user : {}".format(user_id))
def collect_user_profiles(config: Config, twython_connector: TwythonConnector):
dump_location = config.dump_location
all_user_ids = set()
all_user_ids.update(get_user_ids_in_folder("{}/politifact/fake".format(dump_location)))
all_user_ids.update(get_user_ids_in_folder("{}/politifact/real".format(dump_location)))
all_user_ids.update(get_user_ids_in_folder("{}/gossipcop/fake".format(dump_location)))
all_user_ids.update(get_user_ids_in_folder("{}/gossipcop/real".format(dump_location)))
user_profiles_folder = "{}/{}".format(dump_location, "user_profiles")
user_timeline_tweets_folder = "{}/{}".format(dump_location, "user_timeline_tweets")
create_dir(user_profiles_folder)
create_dir(user_timeline_tweets_folder)
multiprocess_data_collection(dump_user_profile_job, all_user_ids, (user_profiles_folder, twython_connector), config)
multiprocess_data_collection(dump_user_recent_tweets_job, all_user_ids, (user_timeline_tweets_folder,
twython_connector), config)
class UserProfileCollector(DataCollector):
def __init__(self, config):
super(UserProfileCollector, self).__init__(config)
def collect_data(self, choices):
all_user_ids = set()
for choice in choices:
all_user_ids.update(get_user_ids_in_folder(
"{}/{}/{}".format(self.config.dump_location, choice["news_source"], choice["label"])))
user_profiles_folder = "{}/{}".format(self.config.dump_location, "user_profiles")
create_dir(user_profiles_folder)
multiprocess_data_collection(dump_user_profile_job, list(all_user_ids),
(user_profiles_folder, self.config.twython_connector),
self.config)
class UserTimelineTweetsCollector(DataCollector):
def __init__(self, config):
super(UserTimelineTweetsCollector, self).__init__(config)
def collect_data(self, choices):
all_user_ids = set()
for choice in choices:
all_user_ids.update(get_user_ids_in_folder(
"{}/{}/{}".format(self.config.dump_location, choice["news_source"], choice["label"])))
user_timeline_tweets_folder = "{}/{}".format(self.config.dump_location, "user_timeline_tweets")
create_dir(user_timeline_tweets_folder)
multiprocess_data_collection(dump_user_recent_tweets_job, list(all_user_ids), (user_timeline_tweets_folder,
self.config.twython_connector),
self.config)
class UserFollowersCollector(DataCollector):
def __init__(self, config):
super(UserFollowersCollector, self).__init__(config)
def collect_data(self, choices):
all_user_ids = set()
for choice in choices:
all_user_ids.update(get_user_ids_in_folder(
"{}/{}/{}".format(self.config.dump_location, choice["news_source"], choice["label"])))
user_followers_folder = "{}/{}".format(self.config.dump_location, "user_followers")
create_dir(user_followers_folder)
multiprocess_data_collection(dump_user_followers, list(all_user_ids), (user_followers_folder,
self.config.twython_connector),
self.config)
class UserFollowingCollector(DataCollector):
def __init__(self, config):
super(UserFollowingCollector, self).__init__(config)
def collect_data(self, choices):
all_user_ids = set()
for choice in choices:
all_user_ids.update(get_user_ids_in_folder(
"{}/{}/{}".format(self.config.dump_location, choice["news_source"], choice["label"])))
user_friends_folder = "{}/{}".format(self.config.dump_location, "user_following")
create_dir(user_friends_folder)
multiprocess_data_collection(dump_user_following, list(all_user_ids), (user_friends_folder,
self.config.twython_connector),
self.config)