-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel_06.py
56 lines (45 loc) · 1.57 KB
/
level_06.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
51
52
53
54
55
56
#!/bin/env python
# coding=utf-8
# http://www.pythonchallenge.com/pc/def/channel.html
# Nothing in zipfile.
import re
import zipfile
from io import BytesIO
import requests
PREFIX = "http://www.pythonchallenge.com/pc/def/"
url = PREFIX + 'channel.zip'
def catch(text, pattern=r'<!--(.*?)-->', cnt=0):
return re.findall(pattern, text, re.DOTALL)[cnt]
def analysis(something):
with zipfile.ZipFile(BytesIO(something), 'r') as channel:
readme = channel.read('readme.txt').decode()
NOTHING = catch(readme, r'start from ([0-9]+)')
MAX_RANGE = len(channel.infolist())
return NOTHING, MAX_RANGE
def solve(something, nothing, MAXRANGE):
reo = re.compile(r'nothing is ([0-9]+)')
nothing = nothing
print("Following the nothing chain and picking up the crumbs!")
with zipfile.ZipFile(BytesIO(something), 'r') as channel:
comments = []
for i in range(int(MAXRANGE)):
name = '{}.txt'.format(nothing)
text = channel.read(name).decode()
comments.append(channel.getinfo(name).comment.decode())
m = reo.search(text)
print('{}:{}'.format(i, text))
if m:
nothing = m.group(1)
else:
break
print("Done\n")
return "".join(comments)
if __name__ == "__main__":
r = requests.get(url)
something = r.content
NOTHING, MAXRANGE = analysis(something)
# NOTHING=90052, MAXRANGE = 910
answer = solve(something, NOTHING, MAXRANGE)
print(answer)
# oxygen
# http://www.pythonchallenge.com/pc/def/oxygen.html