forked from derand/kanojo_server
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivity.py
More file actions
399 lines (352 loc) · 14.4 KB
/
activity.py
File metadata and controls
399 lines (352 loc) · 14.4 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = 'Andrey Derevyagin'
__copyright__ = 'Copyright © 2015'
import copy
import pymongo.errors
import time
from html import escape
from kanojo import as_product
from constants import *
CLEAR_NONE = 0
CLEAR_SELF = 1
FILL_TYPE_PLAIN = 0
FILL_TYPE_HTML = 1
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, ACTIVITY_ADD_AS_ENEMY)
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({ 'collection': 'activities' }) is None:
self._db.seqs.insert({
'collection': 'activities',
'id': 0
})
# Adds new activity to the database
def create(self, activity_info):
'''
{
'kanojo': null,
'product': 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 key not in activity_info:
print('Error: "%s" key not found in activity'%key)
return None
if self._db:
aid = self._db.seqs.find_and_modify(
query = {'collection': 'activities'},
update = {'$inc': {'id': 1}},
fields = {'id': 1, '_id': 0},
new = True
)
aid = aid.get('id', -1) if aid else -2
while self._db.activity.find_one({'id': aid}):
aid += 1
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())
# Main User/owner user
if 'user' in activity_info:
if isinstance(activity_info.get('user'), dict):
activity['user'] = activity_info.get('user').get('id')
else:
activity['user'] = activity_info.get('user')
# Usually enemy user interacting with kanojo
if 'other_user' in activity_info:
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')
# Kanojo in question
if 'kanojo' in activity_info:
if isinstance(activity_info.get('kanojo'), dict):
activity['kanojo'] = activity_info.get('kanojo').get('id')
else:
activity['kanojo'] = activity_info.get('kanojo')
# Product of Kanojo not sure if this is the dict or what
if 'product' in activity_info:
activity['product'] = activity_info.get('product')
# String supplied with activity
if 'activity' in activity_info:
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 something else?
return activity
if activity == CLEAR_NONE:
return activity
allow_keys = ('kanojo', 'product', 'user', 'other_user', 'activity', 'created_timestamp', 'id', 'activity_type', 'activity_type2', )
rv = { key: activity[key] for key in allow_keys if key in activity }
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'))
# exchange 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 'activity' not in rv:
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 from {product_name} in {nationality}.'
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 Lvl.\"{user_level}\".'
elif ACTIVITY_MARRIED == at:
rv['activity'] = '{user_name} has married {kanojo_name}.'
elif ACTIVITY_JOINED == at:
rv['activity'] = '{user_name} has joined.'
elif ACTIVITY_BREAKUP == at:
rv['activity'] = '{user_name} broke 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
# Return all activities related to the kanojo within the defined set
def kanojo_activities(self, kanojo_id, skip=0, limit=6):
rv = []
if self._db:
activity_types = copy.copy(list(ALL_ACTIVITIES))
activity_types.remove(ACTIVITY_BECOME_NEW_LEVEL)
activity_types.remove(ACTIVITY_JOINED)
activity_types.remove(ACTIVITY_ADD_AS_ENEMY)
query = {
'kanojo': kanojo_id,
'activity_type': { '$in': activity_types }
}
rv = self.activities_by_query(query, skip=skip, limit=limit)
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 = [el.get('kanojo') for el in [a for a in activities if a.get('kanojo')]]
return list(set(rv))
def user_ids(self, activities):
rv = [el.get('user') for el in [a for a in activities if a.get('user')]]
rv.extend([el.get('other_user') for el in [a for a in activities if a.get('other_user')]])
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')
f['product_name'] = a['kanojo'].get('product_name')
f['nationality'] = a['kanojo'].get('nationality')
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')))
else:
f['kanojo_name'] = '%s'%escape(a['kanojo'].get('name'))
if a.get('user'):
if FILL_TYPE_PLAIN == fill_type:
f['user_name'] = a['user'].get('name')
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')))
else:
f['user_name'] = '%s'%escape(a['user'].get('name'))
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')
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')))
else:
f['other_user_name'] = '%s'%escape(a['other_user'].get('name'))
f['other_user_level'] = a['other_user'].get('level')
try:
a['activity'] = a['activity'].format(**f)
except KeyError as 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 'kanojo' in a:
kanojo = next((k for k in kanojos if k.get('id') == a.get('kanojo')), None)
a['kanojo'] = kanojo if kanojo else def_kanojo
a['product'] = as_product(kanojo) if kanojo else as_product(def_kanojo)
if 'user' in a:
user = next((u for u in users if u.get('id') == a.get('user')), None)
a['user'] = user if user else def_user
if 'other_user' in a:
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'), 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))