forked from yetone/collipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
199 lines (166 loc) · 4.93 KB
/
helpers.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
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
# coding: utf-8
from HTMLParser import HTMLParser
import os
import re
import time
import config
import models as m
from libs import xss
config = config.rec()
class UsernameParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.names = []
def handle_starttag(self, tag, attrs):
if tag == 'a':
if len(attrs) == 0:
pass
else:
for (variable, value) in attrs:
if variable == 'data-username':
self.names.append(value)
def get_day(timestamp):
FORY = '%d'
os.environ["TZ"] = config.default_timezone
time.tzset()
str = time.strftime(FORY, time.localtime(timestamp))
return str
'''
def get_month(timestamp):
FORY = '%b'
os.environ["TZ"] = config.default_timezone
time.tzset()
str = time.strftime(FORY, time.localtime(timestamp))
return str
'''
def format_date(timestamp):
FORY = '%Y-%m-%d @ %H:%M'
FORM = '%m-%d @ %H:%M'
FORH = '%H:%M'
os.environ["TZ"] = config.default_timezone
time.tzset()
rtime = time.strftime(FORM, time.localtime(timestamp))
htime = time.strftime(FORH, time.localtime(timestamp))
now = int(time.time())
t = now - timestamp
if t < 60:
str = '刚刚'
elif t < 60 * 60:
min = t / 60
str = '%d 分钟前' % min
elif t < 60 * 60 * 24:
h = t / (60 * 60)
str = '%d 小时前 %s' % (h, htime)
elif t < 60 * 60 * 24 * 3:
d = t / (60 * 60 * 24)
if d == 1:
str = '昨天 ' + rtime
else:
str = '前天 ' + rtime
else:
str = time.strftime(FORY, time.localtime(timestamp))
return str
def format_date2(timestamp):
FORY = '%Y-%m-%d @ %H:%M'
os.environ["TZ"] = config.default_timezone
time.tzset()
str = time.strftime(FORY, time.localtime(timestamp))
return str
def get_year():
timestamp = int(time.time())
FORY = '%Y'
os.environ["TZ"] = config.default_timezone
time.tzset()
str = time.strftime(FORY, time.localtime(timestamp))
return str
def get_month():
timestamp = int(time.time())
FORY = '%m'
os.environ["TZ"] = config.default_timezone
time.tzset()
str = time.strftime(FORY, time.localtime(timestamp))
return str
def format_text(text):
floor = ur'#(\d+)楼\s'
for match in re.finditer(floor, text):
url = match.group(1)
floor = match.group(0)
nurl = '<a class="toreply" href="#;">#<span class="tofloor">%s</span>楼 </a>' % (url)
text = text.replace(floor, nurl)
return text
def reply_content(text):
return text[0:26]
def regex(pattern, data, flags=0):
if isinstance(pattern, basestring):
pattern = re.compile(pattern, flags)
return pattern.match(data)
def email(data):
pattern = r'^.+@[^.].*\.[a-z]{2,10}$'
return regex(pattern, data, re.IGNORECASE)
def url(data):
pattern = (
r'(?i)^((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}'
r'/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+'
r'|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’]))$')
return regex(pattern, data, re.IGNORECASE)
def username(data):
pattern = r'^[a-zA-Z0-9]+$'
return regex(pattern, data)
def get_mention_names(content):
up = UsernameParser()
up.feed(content)
up.close()
names1 = up.names
names = {}.fromkeys(names1).keys()
return names
def strip_tags(html):
if html:
html = html.strip()
html = html.strip("\n")
result = []
parse = HTMLParser()
parse.handle_data = result.append
parse.feed(html)
parse.close()
return "".join(result)
return ''
def strip_xss_tags(html):
return xss.parsehtml(html)
def filter_img_tags(htmlstr):
re_img = re.compile('<\s*img[^>]*>', re.L)
re_br = re.compile('<br\s*?/?>')
s = re_img.sub('', htmlstr)
s = re_br.sub('', s)
return s
def get_img_list(text):
img_path = ur'\/static\/[^\s\"]*\.(jpg|jpeg|png|bmp|gif)'
path_list = []
for match in re.finditer(img_path, text):
path = match.group(0)
path_list += [path]
return path_list
def force_int(value, default=1):
try:
return int(value)
except:
return default
class _Missing(object):
def __repr__(self):
return 'no value'
def __reduce__(self):
return '_missing'
_missing = _Missing()
class cached_property(object):
def __init__(self, func, name=None, doc=None):
self.__name__ = name or func.__name__
self.__module__ = func.__module__
self.__doc__ = doc or func.__doc__
self.func = func
def __get__(self, obj, type=None):
if obj is None:
return self
value = obj.__dict__.get(self.__name__, _missing)
if value is _missing:
value = self.func(obj)
obj.__dict__[self.__name__] = value
return value