-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
90 lines (62 loc) · 2.41 KB
/
scraper.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
from bs4 import BeautifulSoup
import requests
from html2md import html2md
import re
def request(args, method: str, url: str, **kwargs):
return requests.request(method,
url,
cookies={"session": args.session},
**kwargs)
def create_scraper(args, url: str):
res = request(args, "get", url)
scraper = BeautifulSoup(res.content, "html.parser")
return scraper
def get_available_days(args):
scraper = create_scraper(args, f"https://adventofcode.com/{args.year}")
calendar = scraper.select_one(".calendar")
links = calendar.select('a')
return len(links)
def get_available_events(args):
scraper = create_scraper(args, "https://adventofcode.com/events")
return (y.text.removeprefix("[").removesuffix("]")
for y in scraper.select(".eventlist-event > a"))
def get_progress(args):
scraper = create_scraper(args, f"https://adventofcode.com/{args.year}")
calendar = scraper.select_one(".calendar")
for c in calendar.select(
":not(:is(.calendar-verycomplete, .calendar-complete)) > .calendar-mark-complete,"
":not(.calendar-verycomplete) > .calendar-mark-verycomplete"):
c.replace_with(" ")
return calendar.text
def get_daily_progress(args):
p = get_progress(args).splitlines()
return {
int(d[1]): i[-2:].replace(" ", "☆").replace("*", "★")
for i in p
if (d := re.search(r"(\d{1,2}) (?:\*| ){2}", i))
}
def get_description(args):
base_url = f"https://adventofcode.com/{args.year}/day/{args.day}"
scraper = create_scraper(args, base_url)
descs = scraper.select("article.day-desc")
desc = html2md(descs[0], 1, base_url)
if len(descs) > 1:
desc += "\n" + html2md(descs[1], 2, base_url)
return desc
def get_input(args):
res = request(args, "get",
f"https://adventofcode.com/{args.year}/day/{args.day}/input")
return res.text
def submit_answer(args, answer):
res = request(args,
"post",
f"https://adventofcode.com/{args.year}/day/{args.day}/answer",
data={
"level": args.part,
"answer": answer
})
scraper = BeautifulSoup(res.content, "html.parser")
p = scraper.select_one("article > p")
for a in p.select("a"):
a.decompose()
return p.text