-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
322 lines (241 loc) · 11.8 KB
/
app.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
import os
import re
import json
import weakref
import logging
import traceback
from datetime import datetime
from bs4 import BeautifulSoup
from aiohttp import web
import aiohttp
import asyncio
import requests
from utils import parse_content
LOG_LEVEL = logging.DEBUG
FORMAT = '%(asctime)s-%(funcName)-10s(%(lineno)d) %(levelname)-5s: %(message)s'
logging.basicConfig(
level=LOG_LEVEL,
format=FORMAT,
)
logger = logging.getLogger('aiohttp.access')
WS_DOMAIN = os.getenv("WS_DOMAIN", "localhost")
WS_HOST = os.getenv("WS_HOST", "127.0.0.1")
WS_PORT = int(os.getenv("WS_PORT", 5000))
MAX_THREADS = 5
PTT_URL = 'https://www.ptt.cc'
async def cancel_tasks(request):
try:
channel_id = request.match_info.get("channel_id")
logger.debug(f'ws: {channel_id}')
ws = request.app["websockets"].pop(channel_id, None)
await ws.close()
rep = f'id: {channel_id}, websocket canceled'
except:
rep = f'id: {channel_id}, websocket cancel failed'
return web.Response(text=rep)
async def socket_handler(request):
channel_id = request.match_info.get("channel_id")
ws = web.WebSocketResponse()
await ws.prepare(request)
request.app["websockets"][channel_id] = ws
logger.info(f"Client connected: {channel_id}")
try:
async for msg in ws:
if msg.type == aiohttp.WSMsgType.TEXT:
req = json.loads(msg.data)
if req['type'] == 'PTT':
board = req['board']
datetime.strptime("20210108", "%Y%m%d")
target_start_date = datetime.strptime(req['start_date'], "%Y%m%d")
target_end_date = datetime.strptime(req['end_date'], "%Y%m%d")
key_word = req['key_word']
start_page = 1
last_page = await get_last_page(board)
logger.info(f'Board: {board}, Last page: {last_page}')
page_list = [p for p in range(start_page, last_page + 1)]
logger.info(f'Page list length: {len(page_list)}')
target_start_page = await search_target_date_page(
page_list, target_start_date, page_list.index(start_page), len(page_list) - 1, board
)
logger.info(f'Target start page: {target_start_page}')
page_queue = asyncio.Queue()
div_queue = asyncio.Queue()
[page_queue.put_nowait(i) for i in range(target_start_page, last_page + 1)]
article_ids_tasks = [
handle_article_ids_tasks(task_id, page_queue, div_queue, ws, board)
for task_id in range(MAX_THREADS)
]
divs_tasks = [
handle_divs_tasks(task_id, div_queue, ws, key_word, target_end_date, board)
for task_id in range(MAX_THREADS)
]
logger.info('Start crawler tasks ...')
# loop.run_until_complete(asyncio.wait(article_ids_tasks + divs_tasks))
for t in article_ids_tasks + divs_tasks:
asyncio.ensure_future(t, loop=loop)
except:
logger.error(str(traceback.format_exc()))
finally:
request.app["websockets"].pop(channel_id, None)
logger.info(f"Client connection closed: {channel_id}")
raise web.HTTPOk()
async def handle_article_ids_tasks(task_id, page_queue, div_queue, ws, board):
while not page_queue.empty():
if ws.closed:
logger.debug(f'Websocket connection closed, article_ids_task left, task id: [{task_id}]')
return
current_req = await page_queue.get()
try:
await get_article_ids(current_req, div_queue, board)
except ConnectionResetError:
logger.error(f'Websocket connection error, article_ids_task left, task id: [{task_id}]')
except:
logger.debug(traceback.format_exc())
async def get_article_ids(req, div_queue, board):
async with aiohttp.ClientSession() as session:
logger.debug(f'Processing index: {str(req)}')
async with session.get(url=PTT_URL + '/bbs/' + board + '/index' + str(req) + '.html', timeout=30) as response:
assert response.status == 200
csv_content = await response.text()
soup = BeautifulSoup(csv_content, 'html.parser')
divs = soup.find_all("div", "r-ent")
[div_queue.put_nowait(div) for div in divs]
return True
async def handle_divs_tasks(task_id, div_queue, ws, key_word, target_end_date, board):
while True:
if ws.closed:
logger.debug(f'Websocket connection closed, divs_task left, task id: [{task_id}]')
return
current_div = await div_queue.get()
try:
success, content = await parse_the_article(current_div, key_word, target_end_date, board)
if success:
# await ws.send_str(str(content))
await ws.send_json(content)
else:
logger.debug(f'Content message: {content}')
except ConnectionResetError:
logger.error(f'Websocket connection error, divs_task left, task id: [{task_id}]')
except:
logger.debug(traceback.format_exc())
if div_queue.empty():
if task_id == MAX_THREADS - 1:
await ws.send_str('done')
return
async def parse_the_article(div, key_word, target_end_date, board):
# ex. link would be <a href="/bbs/PublicServan/M.1127742013.A.240.html">Re: [問題] 職等</a>
href = div.find('a')['href']
link = PTT_URL + href
article_id = re.sub('\.html', '', href.split('/')[-1])
async with aiohttp.ClientSession() as session:
async with session.get(url=link, cookies={'over18': '1'}, timeout=30) as response:
assert response.status == 200
rep = await response.text()
soup = BeautifulSoup(rep, 'html.parser')
content = parse_content(soup.find(id="main-content"), link, article_id, board)
if key_word:
article_date = datetime.strptime(str(content['date']), "%a %b %d %H:%M:%S %Y")
if article_date > target_end_date:
return False, 'Out of date.'
if key_word not in str(content):
return False, 'No key word in content'
messages = content.get('messages')
content['messages'] = []
for m in messages:
if key_word in str(m):
content['messages'].append(m)
content['content'] = content.get('content') if key_word in str(content.get('content')) else ''
return True, content
async def get_last_page(board, timeout=3):
async with aiohttp.ClientSession() as session:
req_url = PTT_URL + '/bbs/' + board + '/index.html'
async with session.get(url=req_url, cookies={'over18': '1'}, timeout=timeout) as response:
assert response.status == 200
content = await response.text()
first_page = re.search(r'href="/bbs/' + board + r'/index(\d+).html">‹', content)
if first_page is None:
return 1
return int(first_page.group(1)) + 1
async def search_target_date_page(page_list, target_date, start_index, end_index, board):
while end_index >= start_index:
middle_index = round((start_index + end_index) / 2)
date_article_first, date_article_last = await get_article_first_last_date(page_list, middle_index, board)
logger.debug(f'{date_article_first} | < {target_date.date()} > | {date_article_last}')
if date_article_first.date() <= target_date.date() <= date_article_last.date():
return page_list[middle_index]
elif date_article_first.date() < target_date.date():
start_index = middle_index + 1
else:
end_index = middle_index - 1
logger.info(
f'Can not find target date, the nearest date page: {page_list[start_index]}, start index: {start_index}, end_index: {end_index}'
)
return page_list[end_index]
async def get_article_first_last_date(page_list, page_index, board):
async with aiohttp.ClientSession() as session:
req_url = PTT_URL + '/bbs/' + board + '/index' + str(page_list[page_index]) + '.html'
async with session.get(url=req_url, cookies={'over18': '1'}, timeout=30) as response:
assert response.status == 200
content = await response.text()
soup = BeautifulSoup(content, 'html.parser')
divs = soup.find_all("div", "r-ent")
if len(page_list) - 1 == page_index:
# 最新頁過濾掉置頂文
filtered_divs = [
div
for div in divs
if div.find('div', 'mark').text != 'M' and '(本文已被刪除)' not in div.find('div', 'title').text
]
else:
filtered_divs = [div for div in divs if '(本文已被刪除)' not in div.find('div', 'title').text]
date_article_first = ''
date_article_last = ''
reverse_index = -1
for i in range(len(filtered_divs)):
try:
if not date_article_first:
link = PTT_URL + filtered_divs[i].find('a')['href']
resp = requests.get(url=link, cookies={'over18': '1'})
soup = BeautifulSoup(resp.text, 'html.parser')
main_content = soup.find(id="main-content")
metas = main_content.select('div.article-metaline')
date = metas[2].select('span.article-meta-value')[0].string
date_article_first = datetime.strptime(date, "%a %b %d %H:%M:%S %Y")
if not date_article_last:
link = PTT_URL + filtered_divs[reverse_index].find('a')['href']
resp = requests.get(url=link, cookies={'over18': '1'})
soup = BeautifulSoup(resp.text, 'html.parser')
main_content = soup.find(id="main-content")
metas = main_content.select('div.article-metaline')
date = metas[2].select('span.article-meta-value')[0].string
date_article_last = datetime.strptime(date, "%a %b %d %H:%M:%S %Y")
except Exception as e:
logger.debug(e)
if date_article_first and date_article_last:
break
reverse_index -= 1
return date_article_first, date_article_last
async def index(request):
return web.FileResponse('./index.html')
async def criteria(request):
return web.FileResponse('./static/criteria.html')
async def result_page(request):
return web.FileResponse('./static/result.html')
def main():
aio_app = web.Application()
# 建立一個 reference dict 準備關聯全部 ws 連線物件, key 為 {channel_id}
aio_app["websockets"] = weakref.WeakValueDictionary()
aio_app.router.add_route('GET', '/index', criteria)
aio_app.router.add_route('GET', '/result', result_page)
# aio_app.router.add_route('GET', '/index2', index)
aio_app.add_routes([web.static('/static', './static')])
aio_app.router.add_route('GET', '/socket/{channel_id}', socket_handler)
aio_app.router.add_route('GET', '/socket/cancel/{channel_id}', cancel_tasks)
runner = web.AppRunner(aio_app, access_log=logger)
loop.run_until_complete(runner.setup())
site = web.TCPSite(runner, WS_HOST, WS_PORT)
loop.run_until_complete(site.start())
loop.run_forever()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
main()