8000 Added · rubenhortas/python_challenge@2a8fc00 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2a8fc00

Browse files
committed
Added
1 parent 58bb3a0 commit 2a8fc00

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

04.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env python
2+
# _*_ coding:utf-8 _*
3+
4+
"""
5+
@author: Rubén Hortas Astariz <http://rubenhortas.blogspot.com>
6+
@contact: rubenhortas at gmail.com
7+
@github: http://github.com/rubenhortas
8+
@license: CC BY-NC-SA 3.0 <http://creativecommons.org/licenses/by-nc-sa/3.0/>
9+
@file: 04
10+
"""
11+
12+
# http://www.pythonchallenge.com/pc/def/linkedlist.php
13+
14+
import re
15+
import signal
16+
import urllib2
17+
18+
from handlers.python import exit_signal_handler
19+
from handlers.url import print_html
20+
21+
BASE_URL = 'http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing='
22+
CHAIN_RE = re.compile('(nothing[a-zA-Z =]*)(?P<next>[0-9]*)')
23+
24+
25+
def get_url(i, url):
26+
try:
27+
print '{0} - Fetching {1}'.format(i, url)
28+
29+
response = urllib2.urlopen(url)
30+
html = response.read()
31+
32+
match = CHAIN_RE.search(html)
33+
if match:
34+
next_url = "{0}{1}".format(BASE_URL, match.group('next'))
35+
get_url(i + 1, next_url)
36+
else:
37+
print_html(html)
38+
39+
except Exception as e:
40+
print e.message
41+
42+
43+
if __name__ == '__main__':
44+
signal.signal(signal.SIGINT, exit_signal_handler)
45+
46+
get_url(0, "http://www.pythonchallenge.com/pc/def/linkedlist.php") # 86 calls
47+
get_url(0, "http://www.pythonchallenge.com/pc/def/linkedlist.php?nothing=8022") # 164 calls

0 commit comments

Comments
 (0)
0