2 # vim:fileencoding=utf-8
10 from google
.appengine
.api
import memcache
11 from google
.appengine
.api
import urlfetch
13 timeParser
= re
.compile(r
'^(\d+)([smhd])?$')
14 linkre
= re
.compile(r
' <https?://(?!i.imgur.com/)[^>]+>')
15 linkjsre
= re
.compile(r
' <javascript:[^>]+>')
23 timeZhUnitMap
= (60, 60, 24, 36524)
24 timeZhUnits
= (u
'秒', u
'分', u
'小时', u
'天')
27 '''将 数字 转化为 xxKiB 的形式'''
31 while left
> 1100 and unit
< 3:
39 return '%.1f%siB' % (left
, units
[unit
])
41 def strftime(time
, timezone
, show_date
=False):
42 '''将时间转换为字符串,考虑时区,可能带日期'''
46 format
= '%m-%d %H:%M:%S'
47 return (time
+ timezone
).strftime(format
)
50 '''将 3s,5d,1h,6m 等转换成秒数'''
51 m
= timeParser
.match(s
)
53 raise ValueError('not a time')
59 return n
* timeUnitMap
[u
]
64 for i
in timeZhUnitMap
:
69 return u
''.join(reversed(map(lambda x
, y
: unicode(x
)+y
if x
else u
'', r
, timeZhUnits
)))
73 if len(nick
.encode('utf-8')) > config
.nick_maxlen
:
76 cat
= unicodedata
.category(i
)
77 # Lt & Lm are special chars
78 if (not (cat
.startswith('L') or cat
.startswith('N')) or cat
in ('Lm', 'Lt')) \
79 and i
not in config
.allowedSymbolInNick
:
85 links
= linkre
.findall(msg
)
87 msg
= linkre
.sub('', msg
)
88 msg
= linkjsre
.sub('', msg
)
92 def __init__(self
, name
):
96 while memcache
.get(self
.name
):
98 memcache
.set(self
.name
, True)
101 memcache
.set(self
.name
, False)