2 # vim:fileencoding=utf-8
9 from google
.appengine
.api
import memcache
11 timeParser
= re
.compile(r
'^(\d+)([smhd])?$')
12 linkre
= re
.compile(r
' <https?://(?!i.imgur.com/)[^>]+>')
13 linkjsre
= re
.compile(r
' <javascript:[^>]+>')
21 timeZhUnitMap
= (60, 60, 24, 36524)
22 timeZhUnits
= (u
'秒', u
'分', u
'小时', u
'天')
25 '''将 数字 转化为 xxKiB 的形式'''
29 while left
> 1100 and unit
< 3:
37 return '%.1f%siB' % (left
, units
[unit
])
39 def strftime(time
, timezone
, show_date
=False):
40 '''将时间转换为字符串,考虑时区,可能带日期'''
44 format
= '%m-%d %H:%M:%S'
45 return (time
+ timezone
).strftime(format
)
48 '''将 3s,5d,1h,6m 等转换成秒数'''
49 m
= timeParser
.match(s
)
51 raise ValueError('not a time')
57 return n
* timeUnitMap
[u
]
62 for i
in timeZhUnitMap
:
67 return u
''.join(reversed(map(lambda x
, y
: unicode(x
)+y
if x
else u
'', r
, timeZhUnits
)))
71 if len(nick
.encode('utf-8')) > config
.nick_maxlen
:
74 if not unicodedata
.category(i
).startswith('L') and i
not in config
.allowedSymbolInNick
:
80 links
= linkre
.findall(msg
)
82 msg
= linkre
.sub('', msg
)
83 msg
= linkjsre
.sub('', msg
)
87 def __init__(self
, name
):
91 while memcache
.get(self
.name
):
93 memcache
.set(self
.name
, True)
96 memcache
.set(self
.name
, False)