advent-of-code-2019/scrapper.py

49 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
from urllib import request
import gzip
import re
host = 'https://adventofcode.com/'
sol_path = '{year}/day/{day}'
input_path = sol_path + '/input'
sol_pattern = re.compile(r'Your puzzle answer was <code>(.*?)</code>')
user_pattern = re.compile(r'<div class="user">(.*?)</div>')
class BadRequest(Exception):
pass
class AdventSession(object):
def __init__(self, session_key):
head = 'session='
if session_key.startswith(head):
session = session_key
else:
session = head + session_key
self.headers = {'Cookie' : session,
'Accept-Encoding' : 'gzip'}
def get(self, url):
req = request.Request(url, headers=self.headers)
res = request.urlopen(req)
text = gzip.decompress(res.read()).decode('utf-8')
if res.status != 200:
raise BadRequest(text)
return text
def get_user(self):
text = self.get(host)
return user_pattern.search(text).group(1)
def get_input(self, year, day):
text = self.get(host + input_path.format(year=year, day=day))
return text
def get_solutions(self, year, day):
text = self.get(host + sol_path.format(year=year, day=day))
return sol_pattern.findall(text)