~cdv/aoc-2018-rs

aoc-2018-rs/DownloadDay.py -rwxr-xr-x 959 bytes View raw
fd04524d — Christopher Vittal Solve day twenty 1 year, 5 months ago
                                                                                
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
#!/usr/bin/env python3

from bs4 import BeautifulSoup
import requests
import json
import sys
import pypandoc

SESSION_KEY = open('.session', "r").read().strip()

DESCR_URL = 'http://adventofcode.com/2018/day/{}'
DESCR = 'descriptions/Day{:02}.md'

INPUT_URL = 'http://adventofcode.com/2018/day/{}/input'
INPUT = 'data/day{:02}'

session = dict(session=SESSION_KEY)

def downloadDesc(day):
  r = requests.get(DESCR_URL.format(day), cookies=session)
  soup = BeautifulSoup(r.text, "html5lib")
  data = '\n'.join(str(p) for p in soup('article'))
  pypandoc.convert_text(data, 'markdown', format='html', outputfile=DESCR.format(day))

def downloadInput(day):
  r = requests.get(INPUT_URL.format(day), cookies=session)
  with open(INPUT.format(day), 'w') as of:
    of.write(r.text)

if __name__ == "__main__":
  if len(sys.argv) < 2:
    print("Usage: {} <day>".format(sys.argv[0]))
  else:
    day = int(sys.argv[1])
    downloadDesc(day)
    downloadInput(day)