-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomplex_async.py
39 lines (27 loc) · 989 Bytes
/
complex_async.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
import asyncio
import api2ch
api = api2ch.Api2chAsync()
async def parse_post(url: str) -> str:
valid, board, thread_id = api2ch.parse_url(url)
if not valid:
raise api2ch.Api2chError(404, 'Invalid URL')
thread = await api.thread(board, thread_id)
post = thread.posts[0]
text = ''
text += f'{post.dt().isoformat()} | Пост №{post.post_id}: {post.url(thread.board)}:\n\n'
text += f'{post.header}\n' if thread.enable_subject else ''
text += f'{post.body_text}\n\n'
if post.files:
text += 'Файлы:\n'
for f in post.files:
text += f'— {f.original_name}, {f.size_string}: {f.url()}\n'
return text
async def pretty_print_post(url: str):
try:
text = await parse_post(url)
except api2ch.Api2chError as e:
print('Request Error', e.code, e.reason)
else:
print(text)
if __name__ == '__main__':
asyncio.run(pretty_print_post('https://2ch.hk/cg/res/1323206.html'))