-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.py
83 lines (70 loc) · 1.85 KB
/
cache.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
def statuses(obj, item):
v = {
'id': item['id'],
'text': item['text'],
'user': {'name': item['user']['name']},
'ctime': time.time(),
}
k = item['id']
obj[k] = v
def privatemsg(obj, item):
if session.me['id'] == item['sender_id']: # noqa
sender = item['recipient_id']
else:
sender = item['sender_id']
v = {
'id': sender,
'ctime': time.time(),
}
k1 = item.get('in_reply_to', {}).get('id')
k2 = item['id']
if k1:
obj[k1] = v
obj[k2] = v
def users(obj, item):
if item.get('repost_status_id'):
v = {
'id': item['repost_user_id'],
'name': item['repost_screen_name'],
'ctime': time.time(),
}
k = item['repost_status_id']
obj[k] = v
elif item.get('photo'):
v = {
'id': item['user']['id'],
'name': item['user']['name'],
'ctime': time.time(),
}
k = item['id']
obj[k] = v
elif item.get('in_reply_to_status_id'):
v = {
'id': item['in_reply_to_user_id'],
'name': item['in_reply_to_screen_name'],
'ctime': time.time(),
}
k = item['in_reply_to_status_id']
obj[k] = v
def users_profile(obj, item):
k, v = item['id'], item
try:
del v['status']
except:
pass
v['ctime'] = time.time()
obj[k] = v
def truncate(d, size=64):
items = sorted(d.items(), key=lambda x: x[1]['ctime'])
return dict(items[-size:])
def save(session, data, keys):
globals()['session'] = session
for key in keys.split(','):
func = globals().get(key)
obj = session.get(key, {})
for item in data:
func(obj, item)
session[key] = truncate(obj)