-
Notifications
You must be signed in to change notification settings - Fork 26
/
WebsiteBase.py
327 lines (276 loc) · 11.9 KB
/
WebsiteBase.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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# -*- coding: utf-8 -*-
######################
# Author : 高明飞
# Data : 2016-07-22
# Brief : 用于获取网站新通知的爬虫基类
######################
import requests, re, time, sqlite3, json, logging
from bs4 import BeautifulSoup
class WebsiteBase:
# Name : 网站名称
# DBName : 数据库名称,不要包含后缀
# AgentID : 微信发布时需要用到的AgentID
# CheckContent : 是否需要打开URL检查内容,True or False
# KeyWords : 过滤用关键词List,如果不需要设置为[]
# KeyWordsThreshold : 关键词阈值,内容页包含的关键词个数超过这个值才认为符合要求
# SpecialKeyWords : 特殊关键词,不受阈值的影响,只要包含一个即符合要求
# encoding : 网站的编码格式,不设置的话默认为utf-8
def __init__(self, Name, DBName, AgentID, CheckContent, KeyWords, KeyWordsThreshold, SpecialKeyWords = [], encoding = 'utf-8'):
self.Name = Name
self.DBName = DBName + '.db'
self.DBCheckedName = DBName + '_checked.db'
self.AgentID = AgentID
self.CheckContent = CheckContent
self.KeyWords = KeyWords
self.encoding = encoding
self.KeyWordsThreshold = KeyWordsThreshold
self.SpecialKeyWords = SpecialKeyWords
# Error Status
self.err = 0
# Wchat ID & Password
f = open('wchat')
self.corpid = f.readline().strip()
self.corpsecret = f.readline().strip()
f.close()
# Init DB
conn = sqlite3.connect(self.DBName)
cursor = conn.cursor()
try:
cursor.execute(
'create table Articles (Title TEXT, Brief TEXT, URL TEXT, DATE TEXT, Published INTEGER)')
except:
pass
cursor.close()
conn.commit()
conn.close()
# Use this DB to record checked messages.
conn = sqlite3.connect(self.DBCheckedName)
cursor = conn.cursor()
try:
cursor.execute('create table URL (URL TEXT)')
except:
pass
cursor.close()
conn.commit()
conn.close()
def GET(self):
returnErr = None
# Open DB
conn = sqlite3.connect(self.DBName)
cursor = conn.cursor()
conn2 = sqlite3.connect(self.DBCheckedName)
cursor2 = conn2.cursor()
logging.warning('Getting : ' + self.Name + '......')
PageRange = self.GetPageRange()
for p in PageRange:
logging.warning(' Getting : ' + str(p))
try:
time.sleep(3)
response = self.GetMainPage(p)
response.encoding = self.encoding
except Exception as err:
returnErr = err
logging.error(' ' + str(p))
logging.error(' ' + repr(err))
continue
soup = BeautifulSoup(response.text, 'html5lib')
soup = self.GetEnclose(soup)
tags = self.GetTags(soup)
logging.warning(' Number of Pages : ' + str(len(tags)))
for tag in tags:
try:
# Get Title
Title = self.GetTitle(tag)
if not Title:
continue
logging.warning(' Checking : ' + Title)
# Get URL
ContentURL = self.GetURL(tag)
# Get time
PublishTime = self.GetPublishTime(tag)
except Exception as err:
logging.error(' Format Error!')
logging.error(' ' + repr(err))
continue
# Check DB
cursor2.execute(
"select * from URL where URL = ?",
[(ContentURL)]
)
# Already exists
if cursor2.fetchone():
continue
# Addition Check
if not self.AdditionCheck(tag):
continue
if self.KeyWords:
# Check Title
# 标题仅检查特殊关键词,以此减少各种误报情况
specialflagcount = 0
keywordstring = ' 关键词:'
for keyword in self.SpecialKeyWords:
if (Title.count(keyword) > 0):
specialflagcount += Title.count(keyword)
keywordstring = keywordstring + keyword + ';'
if specialflagcount == 0 and not self.CheckContent:
# Next Title
continue
# Check Content
flagcount = 0
if self.CheckContent and ContentURL != '':
try:
time.sleep(3)
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36'}
response = requests.get(ContentURL, timeout=21, headers = header)
response.encoding = self.encoding
except Exception as err:
# returnErr = err
logging.error(' ' + ContentURL)
logging.error(' ' + repr(err))
continue
for keyword in self.KeyWords:
if (response.text.count(keyword)) > 0:
flagcount += response.text.count(keyword)
keywordstring = keywordstring + keyword + ';'
for keyword in self.SpecialKeyWords:
if (response.text.count(keyword)) > 0:
specialflagcount += response.text.count(keyword)
keywordstring = keywordstring + keyword + ';'
# Update Checked DB
cursor2.execute(
"insert into URL (URL) values (?)",
[(ContentURL)]
)
if flagcount < self.KeyWordsThreshold and specialflagcount == 0:
# Next Title
continue
# Get Brief
if self.KeyWords:
BriefString = self.GetBrief(tag, str(flagcount) + keywordstring)
else:
BriefString = self.GetBrief(tag, '')
# Update DB
cursor.execute(
"select * from Articles where Title = ? and Brief = ? and URL = ? and DATE = ?",
(Title, BriefString, ContentURL, PublishTime)
)
# Already exist
if cursor.fetchone():
continue
cursor.execute(
"insert into Articles (Title, Brief, URL, DATE, Published) values (?, ?, ?, ?, ?)",
(Title, BriefString, ContentURL, PublishTime, 0)
)
logging.warning(' Updating : ' + Title + '......')
# Close DB
cursor.close()
conn.commit()
conn.close()
cursor2.close()
conn2.commit()
conn2.close()
if returnErr:
raise returnErr
def Update(self):
logging.warning('Updating : ' + self.Name + '......')
# Init Wchat
access_token = self.InitWchat()
if not access_token:
return
# Open DB
conn = sqlite3.connect(self.DBName)
cursor = conn.cursor()
cursor.execute("select * from Articles where Published = 0")
unpublished = cursor.fetchall()
for record in unpublished:
# Publish
logging.warning(' Publishing : ' + record[0] + '......')
newsdata = {'touser': '@all',
'msgtype': 'news',
'agentid': self.AgentID,
'news': {'articles': [{
'title': record[0],
'description': record[1],
'url': record[2]
}]}}
try:
# time.sleep(1)
r = requests.post('https://qyapi.weixin.qq.com/cgi-bin/message/send', params=access_token,
data=json.dumps(newsdata, ensure_ascii=False).encode('utf-8'), timeout=21)
except Exception as err:
logging.error(' Publish Error!')
logging.error(' ' + repr(err))
else:
if 'errcode' in r.json() and r.json()['errcode'] == 0:
logging.warning(' Publish Success!')
cursor.execute("update Articles set Published = 1 where Title = ? and URL = ? and DATE = ?",
(record[0], record[2], record[3]))
else:
logging.error(' Publish Error!')
logging.error(' ' + json.dumps(newsdata, ensure_ascii=False))
logging.error(' ' + r.json())
# Close DB
cursor.close()
conn.commit()
conn.close()
def ReportErrStatus(self, errstr, errstate):
access_token = self.InitWchat()
if not access_token:
return
if errstate:
newsdata = {'touser': 'g199209',
'msgtype': 'news',
'agentid': 0,
'news': {'articles': [{
'title': '云端程序错误',
'description': self.Name + ' :\r\n' + errstr,
}]}}
else:
newsdata = {'touser': 'g199209',
'msgtype': 'news',
'agentid': 0,
'news': {'articles': [{
'title': '云端程序正常运行',
'description': self.Name + ' :\r\n云端程序已从上次错误中恢复,现已正常运行~\r\n' +
'错误共发生' + str(self.err) + '次',
}]}}
try:
r = requests.post('https://qyapi.weixin.qq.com/cgi-bin/message/send', params=access_token,
data=json.dumps(newsdata, ensure_ascii=False).encode('utf-8'), timeout=21)
except:
logging.error('Send Wchat Report Error!')
def InitWchat(self):
# Init Wchat
Auth = {'corpid': self.corpid,
'corpsecret': self.corpsecret}
try:
r = requests.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken', params=Auth, timeout=21)
except Exception as err:
logging.error('Wchat Init Timeout!!')
logging.error(repr(err))
return None
else:
if 'access_token' in r.json():
return {'access_token': r.json()['access_token']}
else:
logging.error('Wchat Init Error!!')
return None
def GetPageRange(self):
pass
def GetMainPage(self, page):
pass
def GetEnclose(self, soup):
pass
def GetTags(self, soup):
pass
def GetTitle(self, tag):
pass
def GetURL(self, tag):
pass
def GetPublishTime(self, tag):
pass
def AdditionCheck(self, tag):
pass
def GetBrief(self, tag, keywordstring):
pass