-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_input.py
More file actions
58 lines (45 loc) · 1.48 KB
/
get_input.py
File metadata and controls
58 lines (45 loc) · 1.48 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
"""
Fetches input for a particular year/day from adventofcode
"""
import argparse
from datetime import date
import os
import requests
def create_input_file(year, day, path=None):
"""Fetches my input from Advent of Code."""
daystr = 'day' + str(day)
url = 'https://adventofcode.com/' + str(year) + '/day/' + str(day) + '/input'
cookies = dict()
with open('session.txt', 'r') as file:
cookies['session'] = file.read().strip()
if path is None:
path = os.path.join(str(year), daystr, 'input', 'input.txt')
dirname = os.path.dirname(path)
if dirname != '' and not os.path.exists(dirname):
os.makedirs(dirname)
with open(path, 'w') as file:
input_request = requests.get(url=url, cookies=cookies)
file.write(input_request.text)
def get_day(day):
"""Gets today's date, or the date passed in."""
if day is not None:
return day
return date.today().day
def get_year(year):
"""Gets today's date, or the date passed in."""
if year is not None:
return year
return date.today().year
def main():
"""Fetches the input for an AoC day."""
parser = argparse.ArgumentParser()
parser.add_argument('--day', '-d', default=None, type=int)
parser.add_argument('--year', '-y', default=None, type=int)
parser.add_argument('--path', '-p', type=str)
args = parser.parse_args()
day = get_day(args.day)
year = get_year(args.year)
print(f'Fetching input for Year {year}, Day {day}')
create_input_file(year, day, args.path)
if __name__ == "__main__":
main()