-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·138 lines (112 loc) · 3.6 KB
/
main.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
import json
import logging
from flask import Flask, redirect
from google.appengine.ext import deferred
from apis.facebook_feed import get_page_feed, get_post_async
from database import StoryPost
app = Flask(__name__)
@app.route('/')
def hello():
logging.debug(redirect("https://t.me/hk_acg_feeds", code=302))
return redirect("https://t.me/hk_acg_feeds", code=302)
def task(stories, which_board):
def check_story(rpc):
try:
result = rpc.get_result()
fb_object = json.loads(result.content)
# if story and story.get('score') >= 100:
story = StoryPost.cvt_FBObject_StoryPost(fb_object)
StoryPost.add(story, which_board)
# except urlfetch.DownloadError as ex:
# logging.exception(ex)
except Exception as e:
logging.exception(e)
# TODO: don't fetch already loaded (>=150 score) stories
# logging.debug(stories)
stories_new = filter(lambda id: not StoryPost.checkPostIdAlreadyExist(id), stories)
rpcs = map(lambda id: get_post_async(id, check_story), stories_new)
for rpc in rpcs:
rpc.wait()
def pick_id(result, limit=100):
lst = map(lambda e: e['id'], result['data'])
ret = [lst[i::limit] for i in xrange(limit)]
ret = ret[::-1] # list(reversed(ret))
# logging.debug(ret)
return ret
@app.route('/cron/pink')
def cron_pink():
stories = get_page_feed('pink')
if stories is None:
logging.debug('stories is none')
return 'stories is none', 500
chunks = pick_id(stories, 50)
for chunk in chunks:
deferred.defer(task, chunk, 'pink')
logging.debug('OK')
return 'OK'
@app.route('/cron/blue')
def cron_blue():
stories = get_page_feed('blue')
if stories is None:
logging.debug('stories is none')
return 'stories is none', 500
chunks = pick_id(stories, 50)
for chunk in chunks:
deferred.defer(task, chunk, 'blue')
logging.debug('OK')
return 'OK'
@app.route('/cron/black')
def cron_black():
stories = get_page_feed('black')
if stories is None:
logging.debug('stories is none')
return 'stories is none', 500
chunks = pick_id(stories, 50)
for chunk in chunks:
deferred.defer(task, chunk, 'black')
logging.debug('OK')
return 'OK'
@app.route('/cron/cos')
def cron_cos():
stories = get_page_feed('cos')
if stories is None:
logging.debug('stories is none')
return 'stories is none', 500
chunks = pick_id(stories, 50)
for chunk in chunks:
deferred.defer(task, chunk, 'cos')
logging.debug('OK')
return 'OK'
@app.route('/cron/music_plastic')
def cron_music_plastic():
stories = get_page_feed('music_plastic')
if stories is None:
logging.debug('stories is none')
return 'stories is none', 500
chunks = pick_id(stories, 50)
for chunk in chunks:
deferred.defer(task, chunk, 'music_plastic')
logging.debug('OK')
return 'OK'
@app.route('/cron/maid')
def cron_maid():
stories = get_page_feed('maid')
if stories is None:
logging.debug('stories is none')
return 'stories is none', 500
chunks = pick_id(stories, 50)
for chunk in chunks:
deferred.defer(task, chunk, 'maid')
logging.debug('OK')
return 'OK'
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
logging.debug('?_?', 404)
return '?_?', 404
@app.errorhandler(500)
def application_error(e):
logging.exception(e)
"""Return a custom 500 error."""
logging.debug('xx( : {}'.format(e))
return 'xx( : {}'.format(e), 500