-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper_tools.py
50 lines (38 loc) · 1.38 KB
/
scraper_tools.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
import requests
from bs4 import BeautifulSoup
from markdownify import markdownify as md
AOC_URL = 'https://adventofcode.com'
def download_day(year, day, auth_token) -> tuple:
url = f'{AOC_URL}/{year}/day/{day}'
raw = requests.get(url, timeout=3)
if raw.status_code != 200:
print(f'AOC {year} Day {day} could not be downloaded')
return False
soup = BeautifulSoup(raw.content, 'html.parser')
article = md(str(soup.find('article')))
article = article.split('\n')
article[0] = '#' + article[0].replace('-', '')
article[1] = f'> [adventofcode.com/{year}/day/{day}](url)'
article.pop()
content = '\n'.join(article)
if auth_token is not None:
try:
raw = requests.get(f'{url}/input', timeout=3, cookies={'session': auth_token}).content
data = str(raw.decode('utf8'))
except:
print(f'AOC {year} Day {day} input data could not be downloaded')
data = None
else:
data = None
return (content, data)
def download_year(year, auth_token, start=1, stop=26) -> list:
challenges = {}
for day in range(start, stop + 1):
download = download_day(year, day, auth_token)
if download is False:
break
else:
challenges[str(day)] = download
if len(challenges) == 0:
return False
return challenges