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
| #!/usr/bin/env python
# -*- coding: utf-8 -*-
#latest edit: 2009.06.19
#to ensure the utf8 encoding environment
import sys
default_encoding = 'utf-8'
if sys.getdefaultencoding() != default_encoding:
reload(sys)
sys.setdefaultencoding(default_encoding)
import urllib
import base64
import re
import time #time zone convertion
from google.appengine.api import urlfetch
from google.appengine.ext import db
def time_from_0_to_8(timestr,timezone=8):
TIMEFORMAT="%a %b %d %X +0000 %Y"
#Sat Jan 03 23:08:54 +0000 2009
ISOTIMEFORMAT='%Y-%m-%d %X'
x=time.strptime(timestr, TIMEFORMAT)
m=time.mktime(x)+60*60*timezone
p=time.strftime(ISOTIMEFORMAT,time.localtime(m))
return p
def timestr_add_mins(timestr,mins=0,future=""):
'''
receive time str, format like: 2009-06-10 13:00:33
and integer mins
return seconds;
'''
ISOTIMEFORMAT='%Y-%m-%d %X'
x=time.strptime(timestr, ISOTIMEFORMAT)
m=time.mktime(x)+60*mins
p=time.strftime(ISOTIMEFORMAT,time.localtime(m))
return p
def add_time(p_time="",send_time=""):
ISOTIMEFORMAT='%Y-%m-%d %X'
now=time.localtime()
#print now.tm_year
p=""
match = re.search(
r"""(?:(?P<year>\d{4})-(?=\d+))?
(?:(?:(?P<month>\d{1,2})-)?(?P<date>\d{1,2})\s+)?
(?P<hour>\d{1,2}):(?P<mins>\d{1,2}) |
(?P<min3>\d+)
""", p_time, re.VERBOSE)
if match:
min3 = match.group("min3")
if min3:
x=time.strptime(send_time, ISOTIMEFORMAT)
x=time.mktime(x)+int(min3)*60
x=time.strftime(ISOTIMEFORMAT,time.localtime(x))
return x
year = match.group("year")
if year==None:
year=now.tm_year
month = match.group("month")
date = match.group("date")
if month==None :
month=now.tm_mon
if date==None:
date=now.tm_mday
hour=match.group("hour")
mins=match.group("mins")
return """%s-%02d-%02d %02d:%02d:00"""%(year,int(month),int(date),int(hour),int(mins))
class Msgs(db.Model):
msg_id = db.StringProperty()
time = time = db.StringProperty() #the excution time; format: 2009-01-31 23:59
send_time = db.StringProperty()
d = db.BooleanProperty(default=False)
p = db.BooleanProperty(default=False)
q = db.BooleanProperty(default=False)
task= db.TextProperty()
user_id = db.StringProperty()
user_name = db.StringProperty()
done = db.BooleanProperty(default=False)
def print_all(self):
print "ID:%s in %s mins was %s, DPD:%d%d%d, by %s(%s). Task:%s" % \
(self.msg_id,self.time,self.send_time,self.d,self.p,\
self.done,self.user_name,self.user_id,self.task)
def reply_msg(user,sn,msg,reply_id=""):
auth=base64.b64encode(user+":"+sn)
auth='Basic '+auth
form_fields = {
"status": msg,
"in_reply_to_status_id":reply_id
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url="""http://api.fanfou.com/statuses/update.xml""",\
payload=form_data,
headers={'Authorization':auth},
method=urlfetch.POST
)
if result.status_code != 200:
print "Send Msg:%s Error!"%msg
def private_msg(user,sn,msg,to_id):
auth=base64.b64encode(user+":"+sn)
auth='Basic '+auth
form_fields = {
"text": msg,
"user":to_id
}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url="""http://api.fanfou.com/direct_messages/new.xml""",\
payload=form_data,
headers={'Authorization':auth},
method=urlfetch.POST
)
if result.status_code != 200:
print "Send Private Msg:%s Error!"%msg
#get one page of to user's replies
def getPage(user,sn,since="",page=1):
auth=base64.b64encode(user+":"+sn)
auth='Basic '+auth
result = urlfetch.fetch(url="http://api.fanfou.com/statuses/replies.xml?since_id=%s&page=%d" % (since,int(page)),headers={'Authorization':auth})
if result.status_code == 200:
return result.content
else:
return False
#parse replies
#this is a private function used only by parse.
def parseMsg(msg,send_time):
#the msg is formated as: "@... d[pq] \d+ \w+"
#return tuple (d,p,q,time,content), the first 3 are boolean type; time :integer;content:task
match = re.search(r"@\S+\s+(?P<mode>\w+)\s+(?P<time>[-\d\s:]+)\s+(?P<todo>.*)", msg, re.DOTALL | re.VERBOSE)
d=False
p=False
q=False
time=0
content=""
if match:
#print msg
mode = match.group("mode")
#print "mode: %s"%mode
if mode.find("d")!=-1:
d=True
if mode.find("p")!=-1:
p=True
if mode.find("q")!=-1:
q=True
time=add_time(match.group("time"),send_time)
content=match.group("todo")
content=db.Text(content,"utf_8")
return (d,p,q,time,content)
else:
return ()
def parse(content):
#regex to parse the content
matches = re.findall(
r"""(?sx)<created_at>([^<]+)</created_at>\s* #0 msg created time
<id>([^<]+)</id>\s* #1 msg id
<text><!\[CDATA\[(.*?)\]\]></text>.*? #2 msg content
<id>([^<]+)</id>\s* #3 msg sender's id
<name>([^<]+)</name> #4 msg sender's name
""",content)
numbers =0
for m in matches:
msg=Msgs()
msg.msg_id=m[1]
msg.send_time=time_from_0_to_8(m[0])
msg.user_id=m[3]
msg.user_name=m[4]
x=parseMsg(m[2],msg.send_time)
if x:
try:
(msg.d,msg.p,msg.q,msg.time,msg.task)=x
msg.put()
numbers+=1
if not msg.q:#q=True:
receipt='''@%s 收到,我将于%s提醒您。谢谢使用!'''%\
(msg.user_name,msg.time[:-3])
reply_msg("timers","jianjian",receipt,msg.msg_id)
#print msg.msg_id
except:
print "asign value error"
return numbers
return numbers
def get_latest_id():
msg=db.GqlQuery("SELECT * FROM Msgs ORDER BY send_time DESC LIMIT 1")#send_time
for m in msg:
if m.msg_id!=None:
return m.msg_id
else:
return ""
def display_all():
msg=db.GqlQuery("SELECT * FROM Msgs ORDER BY send_time DESC")#send_time
if not msg.count():
return
for m in msg:
m.print_all()
def get_records():
msg=db.GqlQuery("SELECT * FROM Msgs")#send_time
return msg.count()
#condition= WHERE....
def delete_all(condition=""):
msg=db.GqlQuery("SELECT * FROM Msgs %s"%condition)#send_time
for m in msg:
db.delete(m)
def count_down(user,sn):
msg=db.GqlQuery("SELECT * FROM Msgs WHERE done= :1",False)
index=0
now=time.gmtime()
now=time.mktime(now)+3600*8
ISOTIMEFORMAT='%Y-%m-%d %X'
x=time.strftime(ISOTIMEFORMAT,time.gmtime(now))
for m in msg:
index+=1
#m.time-=1
print "task id: %s, exe time:%s vs now:%s; result: %s"%(m.msg_id,m.time,x,m.time<=x)
if (m.time<=x):
#m.done=True
#d,p
if m.d:
reply_msg(user,sn,'''@%s %s'''%(m.user_name,m.task),m.msg_id)
if m.p:
private_msg(user,sn,m.task,m.user_id)
m.done=True
m.put()
# m.print_all()
print "there are %d tasks to do."%index
#display_all()
def mainloop(user,sn,latest):
page=1
while(1):
msg=getPage(user,sn,latest,page)
#print msg
if msg:
num=parse(msg)
page+=1
#page=page+1
if num==0:
break
print ""
user="timers"
sn="jianjian"
#delete_all()
#display_all()
#rutine check
count_down(user,sn)
latest=get_latest_id()
if latest==None:
latest=""
mainloop(user,sn,latest) |