-
Notifications
You must be signed in to change notification settings - Fork 0
/
fb.py
193 lines (158 loc) · 4.76 KB
/
fb.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
import json
import networkx as nx
import os
import time
import urllib2
from datetime import datetime
# QUERY
# Access token
token = '''
CAACEdEose0cBAEUNc23xgXydIGnZAKYpmmJrCrqDKwj3dfSaWM56e9XyLcfhPJFosja1GGt6EyQrDNTxlO1HujnXwc50zPHJLUaBZAcBZCOOFj3CrLdLsR467mVevtQmCtaRh5vFQyOqqL7wDA53ZAzSPm7F8bSRpNOHjRCHvCOfm57jR0myLn3aqfdzPq0kTMQIWTPZCRAZDZD
'''
token = token.strip()
# Api
api = 'https://graph.facebook.com'
# Limit
limit = 100
# Build query
def q(action):
query = api + action
if '?' in query:
query += '&'
else:
query += '?'
query += 'limit=' + str(limit)
query += '&access_token=' + token
return query
# GLOBAL VARS
global_var = {
'post_counter': 0,
'last_date': '',
'last_year': 0,
'year': 2014,
}
POSTS = 0
COMMENTS = 1
# FUNCTIONS
# Time functions
def date_to_timestamp(date_str):
date = datetime.strptime(date_str, "%Y-%m-%d")
ts = int(time.mktime(date.timetuple()))
return ts
def get_date(ts_str):
return ts_str.split('T')[0]
def get_year(date_str):
return int(date_str.split('-')[0])
# Graph functions
def process_user(user_id, user_name):
if not toh.has_node(user_id):
user_info = {
'name': user_name,
'contributions': 1,
}
toh.add_node(user_id, user_info)
else:
toh.node[user_id]['contributions'] += 1
def process_relation(user_id_1, user_id_2):
if user_id_1 != user_id_2:
if not toh.has_edge(user_id_1, user_id_2):
toh.add_edge(user_id_1, user_id_2, grade=1)
else:
toh[user_id_1][user_id_2]['grade'] += 1
def process_comments(comments, **extra):
post_user_id = extra['user']
for comment in comments:
user_id = comment['from']['id']
user_name = comment['from']['name']
process_user(user_id, user_name)
process_relation(user_id, post_user_id)
def process_posts(posts):
for post in posts:
user_id = post['from']['id']
user_name = post['from']['name']
process_user(user_id, user_name)
process_relation(user_id, toh_id)
post_id = post['id']
post_comments(post_id, user_id)
created_time = post['created_time']
last_date = get_date(created_time)
global_var['last_date'] = last_date
global_var['last_year'] = get_year(last_date)
global_var['post_counter'] += len(posts)
print global_var['post_counter'], 'posts processed...'
print 'Last date:', global_var['last_date']
print 'Users:', len(toh.nodes())
print 'Relations:', len(toh.edges())
if global_var['post_counter'] % 1000 == 0:
nx.write_gexf(toh, 'toh.gexf')
f = open('last_date.txt', 'w')
f.write(global_var['last_date'])
f.close()
# Data processing functions
def process_data(data, result_type, **extra):
end = False
if 'data' in data:
if result_type == POSTS:
process_posts(data['data'])
if global_var['year'] != 0:
end = global_var['last_year'] < global_var['year']
else: # result_type == COMMENTS:
process_comments(data['data'], **extra)
if not end and 'paging' in data and 'next' in data['paging']:
process_query(data['paging']['next'], result_type, **extra)
def get_data(url):
response = urllib2.urlopen(url)
content = response.read()
response.close()
data = json.loads(content)
return data
def process_query(url, result_type, **extra):
data = get_data(url)
process_data(data, result_type, **extra)
# Main functions
def post_comments(post_id, user_id):
action = '/' + post_id + '/comments'
url = q(action)
process_query(url, COMMENTS, post=post_id, user=user_id)
def toh_posts():
action = '/tasteofhome/feed'
url = q(action)
process_query(url, POSTS, year=2014)
def toh_posts_ts(ts):
action = '/tasteofhome/feed?until=' + str(ts)
url = q(action)
process_query(url, POSTS, year=2014)
# NETWORK
if os.path.isfile('toh.gexf') and os.path.isfile('last_date.txt'):
# Recover graph
toh = nx.read_gexf('toh.gexf')
# Recover toh id
action = '/tasteofhome?fields=name'
url = q(action)
data = get_data(url)
toh_id = data['id']
# Keep processing toh relations
f = open('last_date.txt')
last_date = f.read()
f.close()
ts = date_to_timestamp(last_date)
toh_posts_ts(ts)
else:
# Graph
toh = nx.Graph()
# Add toh node
action = '/tasteofhome?fields=name'
url = q(action)
data = get_data(url)
toh_id = data['id']
toh_info = {
'name': data['name'],
'contributions': 1,
}
toh.add_node(toh_id, toh_info)
# Process toh relations
toh_posts()
nx.write_gexf(toh, 'toh.gexf')
f = open('last_date.txt', 'w')
f.write(global_var['last_date'])
f.close()