-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathactivity.py
More file actions
382 lines (340 loc) · 17.5 KB
/
activity.py
File metadata and controls
382 lines (340 loc) · 17.5 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Andrey Derevyagin'
__copyright__ = 'Copyright © 2015'
import time
from cgi import escape
import copy
CLEAR_NONE = 0
CLEAR_SELF = 1
FILL_TYPE_PLAIN = 0
FILL_TYPE_HTML = 1
# after add new activity type add them to ALL_ACTIVITIES list
# and fix "user_activity" and "all_activities" if need
ACTIVITY_SCAN = 1 # "Nightmare has scanned on 2014/10/04 05:31:50.\n"
ACTIVITY_GENERATED = 2 # + "Violet was generated from 星光産業 ."
ACTIVITY_ME_ADD_FRIEND = 5 # + "Filter added 葵 to friend list."
ACTIVITY_APPROACH_KANOJO = 7 # + "KH approached めりい."
ACTIVITY_ME_STOLE_KANOJO = 8 # + "Devourer stole うる from Nobody."
ACTIVITY_MY_KANOJO_STOLEN = 9 # + "ふみえ was stolen by Nobody."
ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS = 10 # + "呪いのBlu-ray added ぽいと to friend list."
ACTIVITY_BECOME_NEW_LEVEL = 11 # + "Everyone became Lev.\"99\"."
ACTIVITY_MARRIED = 15 # "Devourer get married with うる."
# user defined
# can change "activity_type" in "clear" function to show in client
ACTIVITY_JOINED = 101 # +
ACTIVITY_BREAKUP = 102 # +
ACTIVITY_ADD_AS_ENEMY = 103 # +
ALL_ACTIVITIES = (ACTIVITY_SCAN, ACTIVITY_GENERATED, ACTIVITY_ME_ADD_FRIEND, ACTIVITY_APPROACH_KANOJO, ACTIVITY_ME_STOLE_KANOJO, ACTIVITY_MY_KANOJO_STOLEN, ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS, ACTIVITY_BECOME_NEW_LEVEL, ACTIVITY_MARRIED, ACTIVITY_JOINED, ACTIVITY_BREAKUP)
class ActivityManager(object):
"""docstring for ActivityManager"""
def __init__(self, db=None):
super(ActivityManager, self).__init__()
self._db = db
self.last_aid = 1
if self._db and self._db.seqs.find_one({ 'colection': 'activities' }) is None:
self._db.seqs.insert({
'colection': 'activities',
'id': 0
})
def create(self, activity_info):
'''
{
'kanojo': null,
'scanned': null,
'user': null,
'other_user': null,
'activity': 'human readeble string',
'created_timestamp': 0,
'id': 0,
'activity_type': 0
}
'''
activity_required_fields = ['activity_type', ]
for key in activity_required_fields:
if not activity_info.has_key(key):
print 'Error: "%s" key not found in activity'%key
return None
if self._db:
aid = self._db.seqs.find_and_modify(
query = {'colection': 'activities'},
update = {'$inc': {'id': 1}},
fields = {'id': 1, '_id': 0},
new = True
)
aid = aid.get('id', -1) if aid else -2
else:
aid = self.last_aid
self.last_aid += 1
activity = { key: activity_info[key] for key in activity_required_fields }
activity['id'] = aid
activity['created_timestamp'] = int(time.time())
if activity_info.has_key('user'):
if isinstance(activity_info.get('user'), dict):
activity['user'] = activity_info.get('user').get('id')
else:
activity['user'] = activity_info.get('user')
if activity_info.has_key('other_user'):
if isinstance(activity_info.get('other_user'), dict):
activity['other_user'] = activity_info.get('other_user').get('id')
else:
activity['other_user'] = activity_info.get('other_user')
if activity_info.has_key('kanojo'):
if isinstance(activity_info.get('kanojo'), dict):
activity['kanojo'] = activity_info.get('kanojo').get('id')
else:
activity['kanojo'] = activity_info.get('kanojo')
if activity_info.has_key('scanned'):
activity['scanned'] = activity_info.get('scanned')
if activity_info.has_key('activity'):
activity['activity'] = activity_info.get('activity')
if self._db:
try:
self._db.activity.insert(activity)
self.last_aid = aid
except pymongo.errors.DuplicateKeyError as e:
return self.create(activity_info)
return activity
def clear(self, activity, clear=CLEAR_SELF, user_id=None):
if activity is None:
# TODO: maybe should return somthing else?
return activity
if activity == CLEAR_NONE:
return activity
allow_keys = ('kanojo', 'scanned', 'user', 'other_user', 'activity', 'created_timestamp', 'id', 'activity_type', 'activity_type2', )
rv = { key: activity[key] for key in allow_keys if activity.has_key(key) }
if user_id and rv.get('activity_type') == ACTIVITY_ME_STOLE_KANOJO and rv.get('other_user') == user_id:
rv['activity_type'] = ACTIVITY_MY_KANOJO_STOLEN
if user_id and rv.get('activity_type') == ACTIVITY_ME_ADD_FRIEND and rv.get('other_user') == user_id:
rv['activity_type'] = ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS
#(rv['user'], rv['other_user']) = (rv.get('other_user'), rv.get('user'))
# exchenge user and other_user
if rv.get('activity_type') in [ACTIVITY_APPROACH_KANOJO, ACTIVITY_MY_KANOJO_STOLEN, ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS]:
(rv['user'], rv['other_user']) = (rv.get('other_user'), rv.get('user'))
if not rv.has_key('activity'):
at = rv.get('activity_type')
if ACTIVITY_SCAN == at:
human_time = time.strftime("%d %b %Y %H:%M:%S", time.localtime(rv.get(created_timestamp)))
rv['activity'] = '{user_name} has scanned on ' + human_time+ '.'
elif ACTIVITY_GENERATED == at:
rv['activity'] = '{kanojo_name} was generated.'
elif ACTIVITY_ME_ADD_FRIEND == at:
rv['activity'] = '{user_name} added {kanojo_name} to friend list.'
elif ACTIVITY_APPROACH_KANOJO == at:
rv['activity'] = '{other_user_name} approached {kanojo_name}.'
elif ACTIVITY_ME_STOLE_KANOJO == at:
rv['activity'] = '{user_name} stole {kanojo_name} from {other_user_name}.'
elif ACTIVITY_MY_KANOJO_STOLEN == at:
rv['activity'] = '{kanojo_name} was stolen by {other_user_name}.'
elif ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS == at:
rv['activity'] = '{other_user_name} added {kanojo_name} to friend list.'
elif ACTIVITY_BECOME_NEW_LEVEL == at:
rv['activity'] = '{user_name} became Lev.\"{user_level}\".'
elif ACTIVITY_MARRIED == at:
rv['activity'] = '{user_name} get married with {kanojo_name}.'
elif ACTIVITY_JOINED == at:
rv['activity'] = '{user_name} has joined.'
elif ACTIVITY_BREAKUP == at:
rv['activity'] = '{user_name} break up with {kanojo_name}.'
rv['activity_type'] = ACTIVITY_ME_ADD_FRIEND
rv['activity_type2'] = ACTIVITY_BREAKUP
elif ACTIVITY_ADD_AS_ENEMY == at:
rv['activity'] = '{user_name} added {other_user_name} as enemy.'
return rv
def activities_by_query(self, query, skip=0, limit=6, user_id=None):
if limit > -1:
iterator = self._db.activity.find(query).sort([('created_timestamp', -1), ('id', -1), ]).skip(skip).limit(limit)
else:
iterator = self._db.activity.find(query).sort([('created_timestamp', -1), ('id', -1), ]).skip(skip)
rv = []
for a in iterator:
rv.append(self.clear(a, clear=CLEAR_SELF, user_id=user_id))
return rv
def user_activity(self, user_id, skip=0, limit=6):
rv = []
if self._db:
activity_types = copy.copy(list(ALL_ACTIVITIES))
activity_types.remove(ACTIVITY_APPROACH_KANOJO)
activity_types.remove(ACTIVITY_JOINED)
query = {
'$or': [
{
'user': user_id,
'activity_type': { '$in': activity_types },
},
{
'other_user': user_id,
'activity_type': { '$in': [ACTIVITY_APPROACH_KANOJO, ACTIVITY_ME_STOLE_KANOJO, ACTIVITY_ME_ADD_FRIEND] },
}
],
}
rv = self.activities_by_query(query, skip=skip, limit=limit, user_id=user_id)
return rv
def user_activities_4html(self, user_id, skip=0, limit=6):
rv = []
if self._db:
activity_types = copy.copy(list(ALL_ACTIVITIES))
activity_types.remove(ACTIVITY_APPROACH_KANOJO)
activity_types.remove(ACTIVITY_JOINED)
activity_types.append(ACTIVITY_ADD_AS_ENEMY)
query = {
'$or': [
{
'user': user_id,
'activity_type': { '$in': activity_types },
},
{
'other_user': user_id,
'activity_type': { '$in': [ACTIVITY_APPROACH_KANOJO, ACTIVITY_ME_STOLE_KANOJO, ACTIVITY_ME_ADD_FRIEND] },
}
],
}
rv = self.activities_by_query(query, skip=skip, limit=limit, user_id=user_id)
return rv
def kanojo_activities_4html(self, kanojo_id, skip=0, limit=6):
rv = []
if self._db:
activity_types = copy.copy(list(ALL_ACTIVITIES))
query = {
'$or': [
{
'kanojo': kanojo_id,
'activity_type': { '$in': activity_types },
}
],
}
rv = self.activities_by_query(query, skip=skip, limit=limit)
return rv
def all_activities(self, skip=0, limit=20, since_id=0):
rv = []
if self._db:
activity_types = copy.copy(list(ALL_ACTIVITIES))
activity_types.remove(ACTIVITY_APPROACH_KANOJO)
activity_types.remove(ACTIVITY_SCAN)
activity_types.append(ACTIVITY_ADD_AS_ENEMY)
query = {
'activity_type': { '$in': activity_types },
}
if since_id > 0:
query['id'] = { '$gt': since_id }
rv = self.activities_by_query(query, skip=skip, limit=limit)
return rv
def kanojo_ids(self, activities):
rv = map(lambda el: el.get('kanojo'), filter(lambda a: a.get('kanojo'), activities))
return list(set(rv))
def user_ids(self, activities):
rv = map(lambda el: el.get('user'), filter(lambda a: a.get('user'), activities))
rv.extend(map(lambda el: el.get('other_user'), filter(lambda a: a.get('other_user'), activities)))
return list(set(rv))
def fill_format_activities(self, activities, fill_type=FILL_TYPE_PLAIN):
rv = []
for a in activities:
f = {}
if a.get('kanojo'):
if FILL_TYPE_PLAIN == fill_type:
f['kanojo_name'] = a['kanojo'].get('name').encode('utf-8')
elif FILL_TYPE_HTML == fill_type:
a['kanojo_url'] = '/kanojo/%d.html'%a['kanojo'].get('id')
if a['kanojo'].get('id'):
f['kanojo_name'] = '<a href="%s">%s</a>'%(a['kanojo_url'], escape(a['kanojo'].get('name').encode('utf-8')))
else:
f['kanojo_name'] = '%s'%escape(a['kanojo'].get('name').encode('utf-8'))
if a.get('user'):
if FILL_TYPE_PLAIN == fill_type:
f['user_name'] = a['user'].get('name').encode('utf-8')
elif FILL_TYPE_HTML == fill_type:
a['user_url'] = '/user/%d.html'%a['user'].get('id')
if a['user'].get('id'):
f['user_name'] = '<a href="%s">%s</a>'%(a['user_url'], escape(a['user'].get('name').encode('utf-8')))
else:
f['user_name'] = '%s'%escape(a['user'].get('name').encode('utf-8'))
f['user_level'] = a['user'].get('level')
if a.get('other_user'):
if FILL_TYPE_PLAIN == fill_type:
f['other_user_name'] = a['other_user'].get('name').encode('utf-8')
elif FILL_TYPE_HTML == fill_type:
a['other_user_url'] = '/user/%d.html'%a['other_user'].get('id')
if a['other_user'].get('id'):
f['other_user_name'] = '<a href="%s">%s</a>'%(a['other_user_url'], escape(a['other_user'].get('name').encode('utf-8')))
else:
f['other_user_name'] = '%s'%escape(a['other_user'].get('name').encode('utf-8'))
f['other_user_level'] = a['other_user'].get('level')
try:
a['activity'] = a['activity'].format(**f)
except KeyError, err:
print 'Error in activity format(KeyError): ', err, a
continue
rv.append(a)
return rv
def fill_activities(self, activities, users, kanojos, def_user, def_kanojo, fill_type=FILL_TYPE_PLAIN):
for a in activities:
if a.has_key('kanojo'):
kanojo = next((k for k in kanojos if k.get('id') == a.get('kanojo')), None)
a['kanojo'] = kanojo if kanojo else def_kanojo
if a.has_key('user'):
user = next((u for u in users if u.get('id') == a.get('user')), None)
a['user'] = user if user else def_user
if a.has_key('other_user'):
other_user = next((u for u in users if u.get('id') == a.get('other_user')), None)
a['other_user'] = other_user if other_user else def_user
activities = self.fill_format_activities(activities, fill_type=fill_type)
return activities
def time_diff(self, tm):
'@ x day ago'
return ''
return '@ %d seconds ago'%tm
def create_html_block(self, activities_filled):
if len(activities_filled) == 0:
return '<h1 class="msg_small_alert">No activitities.</h1>'
rv = ''
tm = int(time.time())
for a in activities_filled:
# <div class="activities_box" id="activity70"><div class="l_activities_box"><a href="/user/2.html"><img class="icon" height="50" width="50" src="http://gdrive-cdn.herokuapp.com/5594f233507a7e0009dfdd2b/2.jpg"></a></div><div class="r_activities_box"><a href="/kanojo/1.html"><img class="icon" height="50" width="50" src="http://gdrive-cdn.herokuapp.com/549987b3cd22cc00070385a9/best_girl.png"></a></div><div class="c_activities_box"><span html="true"><a href="/user/2.html">Red Pear</a> added <a href="/kanojo/1.html">ヴェルデ</a> to friend list.</span><br><span id="activity70_time" value="1436440765">@ 1 day ago</span></div></div>
# LEFT
if a.get('activity_type') in [ACTIVITY_GENERATED, ACTIVITY_MY_KANOJO_STOLEN, ]:
(url, img) = (a.get('kanojo_url'), a.get('kanojo', {}).get('profile_image_url'))
elif a.get('activity_type') in [ACTIVITY_APPROACH_KANOJO, ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS, ]:
(url, img) = (a.get('other_user_url'), a.get('other_user', {}).get('profile_image_url'))
else:
(url, img) = (a.get('user_url'), ''+a.get('user', {}).get('profile_image_url'))
tmp = '<div class="l_activities_box"><a href="%s"><img class="icon" height="50" width="50" src="%s"></a></div>'%(url, img)
# RIGHT
if a.get('activity_type') in [ACTIVITY_ME_ADD_FRIEND, ACTIVITY_ME_STOLE_KANOJO, ACTIVITY_BREAKUP, ACTIVITY_APPROACH_KANOJO, ACTIVITY_MY_KANOJO_ADDED_TO_FRIENDS, ]:
(url, img) = (a.get('kanojo_url'), a.get('kanojo', {}).get('profile_image_url'))
elif a.get('activity_type') in [ACTIVITY_ADD_AS_ENEMY, ACTIVITY_MY_KANOJO_STOLEN, ]:
(url, img) = (a.get('other_user_url'), ''+a.get('other_user', {}).get('profile_image_url'))
else:
(url, img) = None, None
if url:
tmp += '<div class="r_activities_box"><a href="%s"><img class="icon" height="50" width="50" src="%s"></a></div>'%(url, img)
# CENTER
tmp += '<div class="c_activities_box"><span html="true">%s</span><br><span id="activity%d_time" value="%d">%s</span></div>'%(a.get('activity').decode('utf-8'), a.get('id'), a.get('created_timestamp'), self.time_diff(tm - a.get('created_timestamp')))
rv += '<div class="activities_box" id="activity%d">%s</div>'%(a.get('id'), tmp)
return rv
if __name__ == "__main__":
from pymongo import MongoClient
import config
mdb_connection_string = config.MDB_CONNECTION_STRING
db_name = mdb_connection_string.split('/')[-1]
db = MongoClient(mdb_connection_string)[db_name]
a = ActivityManager(db)
#a = ActivityManager()
'''
print a.create({
'activity_type': ACTIVITY_GENERATED,
'user': 1,
'kanojo': 1
})
'''
'''
print a.create({
'activity_type': ACTIVITY_APPROACH_MY_KANOJO,
'user': 2,
'other_user': 1,
'kanojo': 1
})
'''
print a.user_activity(2)
tmp = a.all_activities()
print tmp
print a.user_ids(tmp)