-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraph_construction.py
163 lines (135 loc) · 6.74 KB
/
Graph_construction.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
import time
import tweepy
import os
import networkx as nx
import matplotlib.pyplot as plt
import json
consumer_key=[]
consumer_secret=[]
access_key=[]
access_secret=[]
# set the API logins for several applications
#consumer_key.append("--")
#consumer_secret.append("--")
#access_key.append("---")
#access_secret.append("---")
auth = []
for i in range(len(consumer_key)):
auth.append(tweepy.OAuthHandler(consumer_key[i], consumer_secret[i]))
auth[i].set_access_token(access_key[i], access_secret[i])
api = tweepy.API(auth[0])
#loading an existante graph
try:
print("Loading graph ...")
G = nx.read_gexf("Graph.gexf")
print("Loading graph finished ...")
except:
G=nx.DiGraph()
#load a list of locations that you what extract the graph around
with open("Location.txt", "r") as fp:
Locations = json.load(fp)
print ("Number Of nodes collected so far:", G.number_of_nodes())
print ("Number Of nodes collected so far:", G.number_of_edges())
Nodeslist=[]
# if the graph is empty we need to initiate the graph with the first user
if G.number_of_nodes() ==0:
G.add_nodes_from([('2229718597', {'id':'2229718597',
'name':'',
'screen_name': '',
'followers_count':'',
'friends_count': '',
'checked' :0 ,
'location' :''
}
)
]
)
Nodeslist = [v for v in G.nodes()]
# set the waiting time
Time=0
Limited_number_of_followers=4000
Limited_number_of_friends=4000
for v in Nodeslist:
try:
print(" node: ",G.nodes[v]['screen_name']," is it checked : ",G.nodes[v]['checked'])
# check if the node has not been check it and belong to the disired location
if G.nodes[v]['checked']==0 and G.nodes[v]['location'] in Locations:
print("Collecting data for",G.nodes[v]['screen_name'],v,G.nodes[v]['location'])
print("The number of followers of the user are : " + str(G.nodes[v]['followers_count']))
print("The number of followers of the user are : " + str(G.nodes[v]['friends_count']))
if G.nodes[v]['followers_count']<Limited_number_of_followers:
# get the follower the the user v
followers = []
for page in tweepy.Cursor(api.followers, screen_name=G.nodes[v]['screen_name'], wait_on_rate_limit=True,count=300).pages():
try:
followers.extend(page)
except tweepy.TweepError as e:
print("Going to sleep:", e)
time.sleep(60)
G.nodes[v]['checked']=1
for user in followers:
user.screen_name
Time=0
if str(user.id) not in G:
G.add_nodes_from([(str(user.id), {'id':str(user.id),
'name': user.name,
'screen_name': user.screen_name,
'followers_count':user.followers_count,
'friends_count':user.friends_count,
'checked' :0 ,
'location' :user.location
}
)
]
)
G.add_edge(str(user.id),v)
print ("\t\tNumber Of nodes collected so far followers:", G.number_of_nodes())
print ("\t\tNumber Of edges collected so far followers:", G.number_of_edges())
else:
G.nodes[v]['checked']=2
if G.nodes[v]['friends_count']<Limited_number_of_friends:
# collect the list of the user v friends
Friends = []
for page in tweepy.Cursor(api.friends, screen_name=G.nodes[v]['screen_name'], wait_on_rate_limit=True,count=200).pages():
try:
Friends.extend(page)
except tweepy.TweepError as e:
print("Going to sleep:", e)
time.sleep(60)
for user in Friends:
user.screen_name
Time=0
G.nodes[v]['checked']=1
if str(user.id) not in G:
G.add_nodes_from([(str(user.id), {'id':str(user.id),
'name': user.name,
'screen_name': user.screen_name,
'followers_count':user.followers_count,
'friends_count':user.friends_count,
'checked' :0 ,
'location' :user.location
}
)
]
)
G.add_edge(v,str(user.id))
print ("\t\tNumber Of nodes collected so far followers: ", G.number_of_nodes())
print ("\t\tNumber Of edge collected so far followers: ", G.number_of_edges())
else:
G.nodes[v]['checked']=2
nx.write_gexf(G, "Graph.gexf")
print ("\tNumber Of nodes collected so far ", G.number_of_nodes())
print ("\tNumber Of edges collected so far", G.number_of_edges())
except tweepy.TweepError as ex:
if ex.reason == "Not authorized.":
print("exep ", ex)
G.nodes[v]['checked']=2
else:
os.system('clear')
print(ex)
print("waiting time so far : ", Time)
Time+=1
print ("Number Of nodes collected so far:", G.number_of_nodes())
print ("Number Of edges collected so far:", G.number_of_edges())
nx.write_gexf(G, "Graph.gexf")
time.sleep(60)