-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tweets2Graph.py
160 lines (148 loc) · 7.07 KB
/
Tweets2Graph.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
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import debug_errors
import glob
import pandas as pd
import dask.dataframe as dd
from dask.diagnostics import ProgressBar
import networkx as nx
import json
import os
import pymongo
from tqdm import tqdm
import tweepy
import time
import numpy
class Tweets2Graph():
def __init__(self,interactions,username,npartitions=2):
self.data = pd.DataFrame(columns=["user","retweet","quote","reply"])
self.ddf = dd.from_pandas(pd.DataFrame(), npartitions=npartitions)
self.graph = None
self.interactions = interactions
self.username = username
self.mapping = self.get_mapping()
def get_mapping(self):
"""
From friendly naming of the activity on Twitter,
map the related fields in json format.
:return:
"""
return {
"retweet" : "retweeted_status",
"quote" : "quoted_status",
"reply" : "in_reply_to_screen_name",
"mentions" : "mentions"
}
def from_file(self, path_2_file):
"""
From a .csv or a nested .json, load data
:param path_2_file:
:return:
"""
#load csv
if path_2_file.endswith(".csv"):
self.data = pd.read_csv(path_2_file)
self.log.info("CSV loaded correctly")
#load json
elif path_2_file.endswith(".json"):
with open(path_2_file+".json","r") as file:
tmp = json.loads(file)
for key in tqdm(tmp,desc="Loading from file"):
tmp_data = {"user": tmp[key]["user"][self.username]}
for activity in self.interactions:
if (self.mapping[activity] in tmp[key].keys()):
if activity == "reply":
source_user = tmp[key][self.mapping[activity]]
if activity == "retweet" or activity == "quote":
source_user = tmp[key][self.mapping[activity]]['user'][self.username]
else:
source_user = None
tmp_data[activity] = source_user
self.data = self.data.append(pd.Series(tmp_data), ignore_index=True, sort=True)
del tmp, key
else:
raise debug_errors.UnsupportedFormat(format(path_2_file[:-3]))
def from_folder(self,path_2_files, limit = 100):
if os.listdir(path_2_files)[0].endswith("json"):
pattern = glob.glob(path_2_files+'/*.json', recursive=True)
for json_path in tqdm(pattern,desc="Loading from folder"):
json_file = json.load(open(json_path))
tmp_data = {"user": json_file["user"][self.username]}
for activity in self.interactions:
if (self.mapping[activity] in json_file.keys()):
if activity == "reply":
source_user = json_file[self.mapping[activity]]
if activity == "retweet" or activity == "quote":
source_user = json_file[self.mapping[activity]]['user'][self.username]
else:
source_user = None
tmp_data[activity] = source_user
self.data = self.data.append(pd.Series(tmp_data),ignore_index=True,sort=True)
self.log.info("JSONs loaded correctly")
elif os.listdir(path_2_files)[0].endswith("csv"):
pattern = glob.glob(path_2_files + '/*.csv', recursive=True)[:limit]
for csv_path in tqdm(pattern,desc="Loading from folder"):
csv_file = pd.read_csv(csv_path)
csv_file = csv_file[["user"]+self.interactions]
self.data = self.data.append(csv_file,sort=True)
else:
raise debug_errors.UnsupportedFormat(format(path_2_files[:-3]))
def from_dataframe(self, dataframe):
self.data = dataframe[[self.interactions]]
def from_mongo(self,connection_string,database,collection, query={}):
cluster = pymongo.MongoClient(connection_string)
db = cluster[database][collection]
cursor = db.find(query)
for document in tqdm(cursor, desc="Loading from mongoDB"):
tmp_data = {"user": document["user"][self.username]}
for activity in self.interactions:
if (self.mapping[activity] in document.keys()):
if activity == "reply":
source_user = document[self.mapping[activity]]
if activity == "retweet" or activity == "quote":
source_user = document[self.mapping[activity]]['user'][self.username]
else:
source_user = None
tmp_data[activity] = source_user
self.data = self.data.append(pd.Series(tmp_data), ignore_index=True, sort=True)
def from_user(self, consumer_key, consumer_secret, access_token, access_token_secret, users=[]):
'''
Retrieve followers based on users. Requires valid Twitter API. It might take a while.
:param username:
:return:
'''
self.interactions = ["follow"]
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth,wait_on_rate_limit=True, wait_on_rate_limit_notify=True, compression=True)
target_users = pd.DataFrame(columns=["user","follow"])
for user in tqdm(users,desc="Getting user"):
c = tweepy.Cursor(api.followers, user)
for follower in c.items():
target_users = target_users.append(pd.DataFrame([(user,follower.screen_name)],columns=["user","follow"]))
time.sleep(numpy.random.uniform(0,1))
self.data = target_users
del target_users
def fit(self,):
for activity in self.interactions:
rt = self.data.loc[pd.isna(self.data[activity]) == False][["user", activity]]
rt.columns = ["source_user", "target_user"]
self.ddf = self.ddf.append(dd.from_pandas(rt, npartitions=2, sort=True))
def transform(self,connected_component = False):
df_topic = self.ddf.groupby(["source_user", "target_user"]).size().reset_index()
with ProgressBar():
df_topic = df_topic.compute()
df_topic.columns = ["source_user", "target_user", "weight"]
topic_graph = nx.from_pandas_edgelist(df=df_topic,
source="source_user",
target="target_user",
edge_attr="weight",
create_using=nx.Graph)
if connected_component:
return nx.subgraph(topic_graph, max(nx.connected_components(topic_graph), key=len))
return topic_graph
def fit_transform(self):
self.fit()
graph = self.transform()
self.log.info("Graph loaded and created correctly")
return graph