-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoprated.py
executable file
·55 lines (41 loc) · 1.26 KB
/
toprated.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
#!/usr/bin/env python3
from models import TwoChannel
from bs4 import BeautifulSoup as BS
from prettytable import PrettyTable
from argparse import ArgumentParser
# set up argparse
aparser = ArgumentParser()
aparser.add_argument("thread_id", help="Thread ID")
aparser.add_argument(
"-n", "--count", type=int, nargs=1, default=9999, help="Count of posts"
)
args = aparser.parse_args()
# get posts
ch = TwoChannel()
posts = ch.get_thread_posts(args.thread_id)
# make a rating
rating = {}
for post in posts:
comment_soup = BS(post["comment"], "html.parser")
comment_link = comment_soup.find("a")
if comment_link:
parent_id = comment_link.get("data-num")
else:
continue
if parent_id:
parent_url = ch.base_url + f"res/{args.thread_id}.html#{parent_id}"
if parent_url not in rating:
rating[parent_url] = 1
else:
rating[parent_url] += 1
count = args.count[0] if isinstance(args.count, list) else args.count
rating = sorted(rating.items(), key=lambda x: x[1])
rating = rating[-count:]
# output the rating
tbl = PrettyTable(
["Id", "Ответы"], border=False, sortby="Ответы", reversesort=True
)
for r in rating:
tbl.add_row([r[0], r[1]])
print(tbl)
# TODO: if the thread doesn't exist