-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
304 lines (252 loc) · 8.79 KB
/
application.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
import twitter
import simplejson as json
import apikey
import config
from urllib import urlopen, urlencode
from random import shuffle, random, choice
from datetime import datetime
from flask import Flask
from flask import request
from google.appengine.ext import db
from google.appengine.api import taskqueue
app = Flask(__name__)
import user_management
from user_management import CurryUser
from user_management import UserLink
from analyzer import analyze
import effects
from effects import Special
# TODO: should be placed in a proper scope
logging.getLogger().setLevel(logging.DEBUG)
api = twitter.Api(
consumer_key = apikey.CONSUMER_KEY,
consumer_secret = apikey.CONSUMER_SECRET,
access_token_key = apikey.ACCESS_TOKEN_KEY,
access_token_secret = apikey.ACCESS_TOKEN_SECRET,
cache = None
)
class History(db.Model):
# key name = tweet id
timestamp = db.DateTimeProperty(auto_now_add=True)
def is_duplicated(tweet):
if History.get_by_key_name(str(tweet.id)):
return True
else:
return False
@app.route('/cron/fetch_and_post')
def do_fetch_and_post():
user = (CurryUser.all()
.order('last_fetch')
.fetch(limit=1)
)[0]
if user.key().name() == config.MY_NAME:
# skip bot itself
user.last_fetch = datetime.now()
user.put()
return 'ok'
logging.info("fetching for user '%s'" % user.key().name())
taskqueue.add(
queue_name='fetch-queue',
url='/task/fetch_material/%s/0' % user.key().name(),
)
user.last_fetch = datetime.now()
user.put()
return 'ok'
@app.route('/task/fetch_material/<username>/<force_top>', methods=['GET', 'POST'])
def do_task_fetch_material(username, force_top):
user = CurryUser.get_by_key_name(username)
force_top = bool(int(force_top))
if not user:
logging.error("no such user '%s'" % username)
return 'bad'
tweet_list = api.GetUserTimeline(screen_name=username, count=config.FETCH_COUNT)
tweet = None
material_list = None
success = False
if force_top:
tweet_list = tweet_list[0:1]
else:
shuffle(tweet_list)
#
# select material
#
for tweet in tweet_list:
# check history
if not force_top and is_duplicated(tweet):
continue
text = tweet.GetText().encode('utf-8')
material_list = analyze(
text,
count=config.TWEET_MATERIAL_MAX
)
if len(material_list) > 0:
# found material
success = True
break
if success:
# record to history
# TODO: trim history chronically
History(
key_name=str(tweet.id),
timestamp=datetime.now()
).put()
else:
logging.info("material not found for user '%s'" % username)
return 'bad'
#
# select receivers
#
link_list = (UserLink
.all()
.filter('sender = ', user)
.order('timestamp')
.fetch(limit=config.RECEIVER_MAX)
)
for link in link_list:
# randomize material per receiver
shuffle(material_list)
count = 1 + int(random() * len(material_list))
receive_material = material_list[:count]
taskqueue.add(
queue_name='post-queue',
url='/task/post_material/%s/%s' % (username, link.receiver.key().name()),
params={'material': receive_material}
)
link.timestamp=datetime.now()
logging.debug("sending from user '%s' to '%s' with material '%s'" %
(username, link.receiver.key().name(), repr(receive_material)))
# update timestamp
db.put(link_list)
# send to karei_bot if no receivers
if len(link_list) == 0:
shuffle(material_list)
count = 1 + int(random() * len(material_list))
receive_material = material_list[:count]
taskqueue.add(
queue_name='post-queue',
url='/task/post_material/%s/%s' % (username, config.MY_NAME),
params={'material': receive_material}
)
logging.debug("sending from user '%s' to '%s' with material '%s'" %
(username, config.MY_NAME, repr(receive_material)))
return 'ok'
@app.route('/task/post_material/<sender_name>/<receiver_name>', methods=['POST'])
def do_task_post_material(sender_name, receiver_name):
material_list = request.form.getlist('material')
material_str_list = []
for material in material_list:
material_str_list.append(u'「%s」' % material)
material_str = u'、'.join(material_str_list)
logging.debug('constructed material string %s' % material_str)
#
# lookup special words
#
effect_list = []
undefined_material_list = []
for material in material_list:
s = Special.get_by_key_name(material)
if s:
effect_list.append(s)
else:
undefined_material_list.append(material)
effect_string = choice(effect_list).effect_string if effect_list else u"なにも おこらなかった"
state_string = choice(effect_list).state_string if effect_list else ""
# generate effects for undefined material
for material in undefined_material_list:
# added for the ease of processing but never updated to DB
# TODO
effect_list.append(
Special(
key_name=material,
spicy=random() * 10,
kal=int(random() * 1000),
color=[
int(random() * 256),
int(random() * 256),
int(random() * 256)
],
price=int(random() * 10000),
effect_string="",
state_string="",
)
)
#
# post the tweet
#
try:
status = api.PostUpdate(
u'@%s は @%s の カレーに %sを 入れた。 %s' % (
sender_name,
receiver_name,
material_str,
effect_string
)
)
logging.debug("posted '%s'" % status.GetText())
except twitter.TwitterError, e:
logging.debug('duplicated user %s with material %s', (sender_name, material_list))
#
# update the curry
#
receiver = CurryUser.get_by_key_name(receiver_name)
if receiver.curry_count > 0:
receiver.curry_material += u'、' + material_str
else:
receiver.curry_material = material_str
tmp_color = [0, 0, 0]
for effect in effect_list:
receiver.spicy *= effect.spicy
receiver.kal += effect.kal
receiver.price += effect.price
tmp_color[0] += effect.color[0]
tmp_color[1] += effect.color[1]
tmp_color[2] += effect.color[2]
total = receiver.curry_count + len(material_list)
receiver.color = [
int(float(receiver.color[0] * receiver.curry_count + tmp_color[0] * len(material_list))/total),
int(float(receiver.color[1] * receiver.curry_count + tmp_color[1] * len(material_list))/total),
int(float(receiver.color[2] * receiver.curry_count + tmp_color[2] * len(material_list))/total),
]
receiver.curry_count = total
if state_string:
receiver.state_string = state_string
receiver.put()
# complete the curry if the material is enough
if receiver.curry_count >= config.CURRY_MATERIAL_MAX:
taskqueue.add(
queue_name='post-queue',
url='/task/post_curry/%s' % receiver_name,
)
return 'ok'
@app.route('/task/post_curry/<username>', methods=['GET', 'POST'])
def do_task_post_curry(username):
user = CurryUser.get_by_key_name(username)
display_string = effects.display_effect(user)
# post the tweet
try:
status = api.PostUpdate(
u'@%s の カレーが できあがった! 今日の材料は %s。%s %s' % (
username,
user.curry_material,
display_string,
user.state_string
)
)
user.curry_count = 0
user.curry_material = ""
#TODO: move to instance method
user.spicy = 1.0
user.price = 0
user.kal = 0
user.color = [128, 128, 0]
user.state_string = ""
user.put()
logging.debug("posted '%s'" % status.GetText())
except twitter.TwitterError, e:
logging.debug('duplicated user %s with material %s', (username, material_list))
return 'ok'
if __name__ == '__main__':
app.run()