-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathold_comment_parser.py
More file actions
74 lines (58 loc) · 2.11 KB
/
Copy pathold_comment_parser.py
File metadata and controls
74 lines (58 loc) · 2.11 KB
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
"""Parse an old.reddit user comments listing into structured records.
Server-rendered HTML. Pure: HTML in -> list[dict] out. Each comment is a
`div.thing.comment` carrying the fields as data-* attributes; score + time come
from the tagline. Same 5-field schema as the Playwright comment scrapper:
subreddit, author, score, created_iso, permalink.
"""
from dataclasses import dataclass, asdict
from bs4 import BeautifulSoup
THING_SELECTOR = "div.thing.comment"
@dataclass
class Comment:
subreddit: str | None
author: str | None
score: int | None # comment votes
created_iso: str | None # comment timestamp
permalink: str | None
def _score(thing):
# Tagline renders three score spans (vote states); .unvoted is the real one.
el = thing.select_one(".score.unvoted")
if el and el.get("title"):
try:
return int(el["title"])
except ValueError:
return None
return None
def _permalink(thing):
rel = thing.get("data-permalink")
return "https://www.reddit.com" + rel if rel else None
def _created(thing):
t = thing.select_one(".tagline time")
return t.get("datetime") if t else None
def parse_user_comments(html):
"""HTML -> list of comment dicts. Deduped by fullname, order preserved."""
soup = BeautifulSoup(html, "html.parser")
seen = set()
out = []
for thing in soup.select(THING_SELECTOR):
key = thing.get("data-fullname") or thing.get("data-permalink")
if key in seen:
continue
seen.add(key)
out.append(
asdict(
Comment(
subreddit=thing.get("data-subreddit"),
author=thing.get("data-author"),
score=_score(thing),
created_iso=_created(thing),
permalink=_permalink(thing),
)
)
)
return out
def next_page_url(html):
"""Return the old.reddit 'next' page URL, or None at the end."""
soup = BeautifulSoup(html, "html.parser")
a = soup.select_one('.nextprev a[rel~="next"]')
return a.get("href") if a else None