This repository was archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimeline.py
130 lines (111 loc) · 4.42 KB
/
timeline.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
import datetime
import logging
from urllib import unquote
import queries
import twitter
import data
import constants
import misc
from formatting import itemHTML
from mclock import MCLock
from oauth import OAuthClient
from google.appengine.api import memcache
""" Timeline update frequency. Update no more often than this """
TIMELINE_UPDATE_FREQ = datetime.timedelta(0, 90)
#TIMELINE_UPDATE_FREQ = datetime.timedelta(0, 0)
""" How many timeline entries to fetch. No more than 200! """
FETCH_COUNT=100
MAX_PAGES_TO_FETCH=3
TIMELINE_LOCK_TIMEOUT=600
def updateTimeLine(u):
tl = queries.getUserTimeline(u)
t = twitter.Api(oauth=OAuthClient(handler=None,token=u))
if tl.timeline_last_updated==None or \
(tl.timeline_last_updated+TIMELINE_UPDATE_FREQ) < \
datetime.datetime.now():
l = MCLock(u.screen_name, timeout=TIMELINE_LOCK_TIMEOUT)
if l.lock():
try:
_updateTimeLine(u,t,tl)
finally:
l.unlock()
else:
logging.debug("Timeline for %s is already been updated" % u.screen_name)
else:
logging.debug("Timeline for %s is up to date" % u.screen_name)
def _updateTimeLine(u,t,tl):
logging.debug("Updating timeline for user %s" % u.screen_name)
groups = queries.loadGroups(u)
ui = {} # friend index index
page = 1
done = False
fetched = 0
since_id = tl.timeline_max_id
since = datetime.datetime.now()-constants.BACK_ENTRIES
first = None
while not done and page<=MAX_PAGES_TO_FETCH:
try:
logging.debug("Fetching page %d of %s timeline (since id %d)" % \
(page, u.screen_name, since_id))
timeline = t.GetFriendsTimeline(since_id = since_id,\
page=page, count=FETCH_COUNT)
page = page + 1
except Exception:
logging.exception("Error fetching friends timeline for %s" % \
u.screen_name)
raise
if timeline==None or len(timeline)==0:
break
for e in timeline:
if not first:
first = e
logging.debug("Got timeline entry %d" % e.id)
if e.id<=since_id:
done = True
break
ts = datetime.datetime.utcfromtimestamp(\
e.GetCreatedAtInSeconds())
if ts<=since:
logging.debug("Stopping, as encountered an entry, " \
"which is older than cut off date %s" % \
since.ctime())
done = True
break
if tl.timeline_max_id < e.id:
tl.timeline_max_id = e.id
if e.user.screen_name==u.screen_name:
# skip my own entries
continue
eu = ui.get(e.user.screen_name, None)
if eu == None:
eu = queries.getFriendByName(e.user.screen_name,u)
if eu == None:
logging.error("Entry from unknown friend %s!" % \
e.user.screen_name)
continue
else:
ui[e.user.screen_name]=eu
_addTimeLineEntry(e,ts,u,eu)
fetched = fetched+1
# Save timeline_max_id between page
tl.put()
tl.timeline_last_updated=datetime.datetime.now()
tl.put()
logging.debug("Fetced %d timeline entries for %s" % \
(fetched, u.screen_name))
if not first: return
last = datetime.datetime.utcfromtimestamp(first.GetCreatedAtInSeconds())
memcache.set(str(u.key()), last, constants.LAST_MESSAGE_CACHE_TIME,
namespace = constants.LAST_MESSAGE_NAMESPACE)
def _addTimeLineEntry(e,ts,u,friend):
logging.debug("Adding timeline entry %d" % e.id)
s = data.StatusUpdate(id = e.id,
text = e.text,
created_at = ts,
truncated = False, #TODO
in_reply_to_status_id = -1, #TODO
in_reply_to_user_id = -1, #TODO
in_reply_to_screen_name = None, #TODO
group = friend.group.key(),
from_friend = friend.key())
s.put()