forked from devty1023/fboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfboard.py
407 lines (327 loc) · 12.7 KB
/
fboard.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
"""
fboard
~~~
enhancing fb group.
sort posts by 'hotness', 'most liked', 'most commented', etc.
sort users by score (point per likes on posts he/she authored)
heavily influneced by Armin Ronacher's flask apps
:copyright: (c) Copyright 2014 by devty
:license: BSD, see LICENSE for detail
"""
from flask import Flask, render_template
from flask.ext.sqlalchemy import SQLAlchemy
import requests
import json
import time, calendar
import math
import redis
from celery import Celery
app = Flask(__name__)
app.config.from_object('_config')
db = SQLAlchemy(app)
"""
DB
"""
class Post(db.Model):
"""
represnts a single post
"""
id = db.Column(db.String, primary_key=True)
created = db.Column(db.String)
summary = db.Column(db.String)
count_likes = db.Column(db.Integer)
count_comments = db.Column(db.Integer)
link = db.Column(db.String)
score = db.Column(db.Float)
hot = db.Column(db.Float)
author = db.Column(db.String)
author_id = db.Column(db.String)
ext_link = db.Column(db.String)
def __init__(self, id, summary, count_likes,
count_comments, score, hot, link,
author, author_id, created, ext_link):
self.id = id
self.summary = summary
self.count_likes = count_likes
self.count_comments = count_comments
self.link = link
self.score = score
self.hot = hot
self.author = author
self.author_id = author_id
self.created = created
self.ext_link = ext_link
def __repr__(self):
return '<POST> id: ' + str(self.id) + ' score: ' + str(self.score)
"""
FB
"""
class FBGroupFeed(object):
def __init__(self, group_id, app_id, app_secret):
self.group_id = group_id
self.app_id = app_id
self.app_secret = app_secret
self.more_link = ''
self.access_token = app_id + '|' + app_secret
self.path = 'https://graph.facebook.com/' + \
group_id + '/feed'
self.path_graph = 'https://graph.facebook.com/'
self.fb_time_format = '%Y-%m-%dT%H:%M:%S+0000'
def get_feed(self, limit=500):
payload = { 'access_token': self.access_token,
'fields': """likes.summary(true),
message,comments.summary(true),
from,type,picture,link,
created_time,actions""",
'limit': limit
}
try:
res = requests.request('GET', self.path, params=payload)
except requests.HTTPError as e:
raise Exception()
res_json = json.loads(res.content)
if res_json['data']:
# save next link
self.more_link = res_json['paging']['next']
return res_json['data']
else:
return dict()
def get_more_feed(self):
if self.more_link:
try:
res = requests.request('GET', self.more_link)
except requests.HTTPError as e:
raise Exception()
res_json = json.loads(res.content)
if res_json['data']:
# save next link
self.more_link = res_json['paging']['next']
return res_json['data']
else:
self.more_link = ''
return dict()
return dict()
def get_recent_feed(self, ref_time, limit=5000):
print 'get_recent_feed(' + str(ref_time) + ')'
feed = self.get_feed(limit=limit)
recent_feed = []
print len(feed), " posts loaded"
for post in feed:
# post time since eposh
post_time = calendar.timegm(time.strptime(post['updated_time'],
self.fb_time_format)
)
if post_time > ref_time:
recent_feed.append(post)
print "got: ",len(recent_feed)
return recent_feed
# dont use
def get_profile_link(self, author_id):
r =requests.request('GET', self.path_graph + \
str(author_id) + \
'/picture')
return r.url
def get_comments(self, post_id, limit=5000):
payload = { 'access_token': self.access_token,
'fields': "comments,actions",
'limit': limit
}
try:
res = requests.request('GET', self.path_graph + post_id, params=payload)
except requests.HTTPError as e:
raise Exception()
if 'comments' in json.loads(res.content):
return json.loads(res.content)['comments']['data']
else:
return dict()
"""
Celery Tasks
"""
def make_celery(app):
celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)
TaskBase = celery.Task
class ContextTask(TaskBase):
abstract = True
def __call__(self, *args, **kwargs):
with app.app_context():
return TaskBase.__call__(self, *args, **kwargs)
celery.Task = ContextTask
return celery
celery = make_celery(app)
@celery.task()
def update_score(post):
# calculate hotscore
post['score'], post['hot'] = hotness(post['count_likes'],
post['count_comments'],
post['created'])
#print 'updating score: ' + str(post)
# create / update database entry
return update_or_create_post(post_id=post['post_id'],
summary=post['summary'],
count_likes=post['count_likes'],
count_comments=post['count_comments'],
link=post['link'],
score=post['score'],
hot=post['hot'],
author=post['author'],
author_id=post['author_id'],
created=post['created'],
ext_link=post['ext_link'])
def sync_init():
"""Synchronize with database-INIT"""
fb = FBGroupFeed(group_id = app.config['GROUP_ID'],
app_id = app.config['APP_ID'],
app_secret = app.config['APP_SECRET'])
ref_time = int(app.config['SYNC_START'])
posts = fb.get_feed()
for post in posts:
clean_post = dict()
clean_post['post_id'] = post['id']
clean_post['summary'] = post.get('message', "")
temp_likes = post.get('likes', 0)
if not temp_likes:
clean_post['count_likes'] = 0
else:
clean_post['count_likes'] = int(post['likes']['summary']['total_count'])
temp_comments = post.get('comments', 0)
if not temp_comments:
clean_post['count_comments'] = 0
else:
clean_post['count_comments'] = int(post['comments']['summary']['total_count'])
clean_post['link'] = post['actions'][0]['link']
clean_post['author'] = post['from']['name']
clean_post['author_id'] = post['from']['id']
clean_post['created'] = calendar.timegm(time.strptime(post['created_time'],
fb.fb_time_format)
)
print clean_post['created'], '!!!!!!!!!!!!'
clean_post['ext_link'] = post.get('link', '')
clean_post['ref_time'] = ref_time
update_score.delay(clean_post)
posts = fb.get_more_feed()
while posts:
print 'processing...', len(posts)
for post in posts:
clean_post = dict()
clean_post['post_id'] = post['id']
clean_post['summary'] = post.get('message', "")
temp_likes = post.get('likes', 0)
if not temp_likes:
clean_post['count_likes'] = 0
else:
clean_post['count_likes'] = int(post['likes']['summary']['total_count'])
temp_comments = post.get('comments', 0)
if not temp_comments:
clean_post['count_comments'] = 0
else:
clean_post['count_comments'] = int(post['comments']['summary']['total_count'])
clean_post['link'] = post['actions'][0]['link']
clean_post['author'] = post['from']['name']
clean_post['author_id'] = post['from']['id']
clean_post['created'] = post['created_time']
clean_post['created'] = calendar.timegm(time.strptime(post['created_time'],
fb.fb_time_format)
)
print clean_post['created'], '!!!!!!!!!!!!'
clean_post['ext_link'] = post.get('link', '')
clean_post['ref_time'] = ref_time
update_score.delay(clean_post)
posts = fb.get_more_feed()
@celery.task()
def sync():
"""Synchronize with database"""
fb = FBGroupFeed(group_id = app.config['GROUP_ID'],
app_id = app.config['APP_ID'],
app_secret = app.config['APP_SECRET'])
#GET REF_TIME FROM CACHE
r = redis.StrictRedis(host='localhost', port=6379, db=1)
ref_time = r.get('ref_time')
if not ref_time:
ref_time = app.config['SYNC_START']
ref_time = int(ref_time)
posts = fb.get_recent_feed(ref_time)
for post in posts:
clean_post = dict()
clean_post['post_id'] = post['id']
clean_post['summary'] = post.get('message', "")
temp_likes = post.get('likes', 0)
if not temp_likes:
clean_post['count_likes'] = 0
else:
clean_post['count_likes'] = int(post['likes']['summary']['total_count'])
temp_comments = post.get('comments', 0)
if not temp_comments:
clean_post['count_comments'] = 0
else:
clean_post['count_comments'] = int(post['comments']['summary']['total_count'])
clean_post['link'] = post['actions'][0]['link']
clean_post['author'] = post['from']['name']
clean_post['author_id'] = post['from']['id']
clean_post['created'] = calendar.timegm(time.strptime(post['created_time'],
fb.fb_time_format)
)
print clean_post['created'], '!!!!!!!!!!!!'
clean_post['ext_link'] = post.get('link', '')
clean_post['ref_time'] = ref_time
update_score.delay(clean_post)
# give processing time.... by 20?
r.set('ref_time', int(time.time())-20)
"""
utils
"""
def hotness(likes, comments, created):
t = int(created) - int(app.config['SYNC_START'])
score = likes + comments*0.3 # comment is weighed much less
if score == 0:
return score, math.log10(1) + (t)/45000
print 'diff:' + str(t)
return score, math.log10(score) + (t)/45000
def update_or_create_post(post_id, summary, count_likes,
count_comments, link, score, hot,
author, author_id, created, ext_link):
obj = Post.query.filter_by(id=post_id).first()
if obj:
# update
obj.count_likes = count_likes
obj.count_comments = count_comments
obj.score = score
obj.summary = summary
print hot
obj.hot = hot
db.session.add(obj)
db.session.commit()
return True
else:
# create obj
obj = Post(id=post_id, summary=summary, count_likes=count_likes,
count_comments=count_comments, link=link,
score=score, hot=hot, author=author, author_id=author_id,
created=created, ext_link=ext_link)
db.session.add(obj)
db.session.commit()
return True
"""
FRONTEND
"""
@app.route('/')
def index():
fb = FBGroupFeed(group_id = app.config['GROUP_ID'],
app_id = app.config['APP_ID'],
app_secret = app.config['APP_SECRET'])
trend_20 = Post.query.order_by(Post.hot.desc()).limit(20)
return render_template("index.html", page_title='trending now', posts=trend_20)
@app.route('/top')
def top():
fb = FBGroupFeed(group_id = app.config['GROUP_ID'],
app_id = app.config['APP_ID'],
app_secret = app.config['APP_SECRET'])
top_50 = Post.query.order_by(Post.score.desc()).limit(50)
return render_template("index.html", page_title='top 50',posts=top_50)
@app.route('/admin/' + app.config['SECRET'] + '/init')
def init_db():
sync_init()
return 'hello world'
@app.route('/about')
def about():
return render_template('about.html')