-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_input.py
More file actions
31 lines (20 loc) · 750 Bytes
/
get_input.py
File metadata and controls
31 lines (20 loc) · 750 Bytes
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
import requests
import os
def get_input_from_web(day):
url = 'http://adventofcode.com/2016/day/{}/input'.format(day)
# session.txt contains the session token, taken from browser
with open('data/session.txt', 'r') as file:
cookie = {'session': file.read()}
request = requests.get(url, cookies=cookie)
with open('data/input_{}.txt'.format(day), 'w') as file:
file.write(request.text)
file.flush()
return request.text
def get_input(day):
if not os.path.exists('data/input_{}.txt'.format(day)):
get_input_from_web(day)
with open('data/input_{}.txt'.format(day), 'r') as file:
return file.read().rstrip('\n')
# Testing
if __name__ == '__main__':
get_input_from_web(1)