-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactivity.py
113 lines (103 loc) · 3.3 KB
/
activity.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
# -*- coding: utf-8 -*-
from func import *
def validate(c):
if c.startswith('hd') or c.startswith('活动'):
return True
return False
def answer(ToUserName, FromUserName, CreateTime, MsgType, Content):
# a = Activity.query.filter(Activity.end >= datetime.datetime.now()).order_by(Activity.start)
a = Activity.query.order_by(-Activity.pri)
Content = []
cur = datetime.date.today()
for i in a:
if i.end < cur:
continue
l = Link(title=i.title, url=i.url, desc=i.title, picUrl=i.avatar)
Content.append(l)
if len(Content) >= 10:
break
db.session.commit()
return genLinkXml(ToUserName, FromUserName, CreateTime, MsgType, Content)
def parseDate(title):
title = title.encode('utf-8')
l = title.find("【")
r = title.find("】", l)
s = title[l:r]
c = 0
l = []
# print s
for i in s:
if i.isdigit():
c *= 10
c += ord(i) - ord('0')
else:
if c > 0:
l.append(c)
c = 0
if c > 0:
l.append(c)
if len(l) == 4:
pass
elif len(l) == 3:
l.insert(2, l[0])
elif len(l) == 2:
l.append(l[0])
l.append(l[1])
else:
l = None
return l
def updateActivityByDate(ssM, ssD, stM, stD, title, url, avatar, view, reply):
# print ssM, ssD, stM, stD, title, url, avatar, view, reply
a = Activity.query.filter_by(url = url).first()
if a:
db.session.delete(a)
a = Activity(ssM, ssD, stM, stD, title, url, avatar, view, reply)
db.session.add(a)
else:
a = Activity(ssM, ssD, stM, stD, title, url, avatar, view, reply)
db.session.add(a)
db.session.commit()
def clearActivity():
q = Activity.query.filter().all()
for i in q:
db.session.delete(i)
db.session.commit()
def checkIfClosed(url):
res = getContentByUrl(url)
soup = BeautifulSoup(res)
soup = soup.find(name='a', attrs={'title' : '主题操作记录'})
if soup:
if u'关闭' in soup.getText():
return True
url = 'http://www.oiegg.com/' + soup['href']
res = getContentByUrl(url)
soup = BeautifulSoup(res)
soup = soup.find(name='table', attrs={'summary' : '主题操作记录'})
print soup
for i in soup.findAll('tr'):
j = i.findAll('td')
if u'关闭' in j[2].getText():
return True
return False
def update():
try:
clearActivity()
res = getContentByUrl("http://www.oiegg.com/forumdisplay.php?fid=172")
soup = BeautifulSoup(res)
soup = soup.find("table", attrs={"id" : "forum_172"})
soup = soup.findAll('tbody')
for i in soup:
try:
title = i.find('span').getText()
ssM, ssD, stM, stD = parseDate(title)
url = 'http://www.oiegg.com/' + i.find('span').find('a')['href']
num = i.find(attrs = {'class' : 'nums'}).getText()
num = num.split('/')
reply = int(num[0])
view = int(num[1])
if not checkIfClosed(url):
updateActivityByDate(ssM, ssD, stM, stD, title, url, getAvatarByUrl(url), view, reply)
except:
pass
except:
pass