-
Notifications
You must be signed in to change notification settings - Fork 52
/
bookrank.py
35 lines (29 loc) · 881 Bytes
/
bookrank.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
#!/usr/bin/env python3
from atexit import register
from re import compile
from threading import Thread
from time import ctime
from urllib.request import urlopen
REGEX = compile(r"#([\d,]+) in Books ")
AMZN = "http://amazon.com/dp/"
ISBNs = {
'0132269937': "Core Python Programming",
'0132356139': "Python Web Development with Django",
'0137143419': "Python Fundamentals"
}
def get_ranking(isbn):
page = urlopen("%s%s" % (AMZN, isbn)) # Or str.format()
data = page.read().decode()
page.close()
return REGEX.findall(data)[0]
def _show_ranking(isbn):
print("- %r ranked %s" % (ISBNs[isbn], get_ranking(isbn)))
def _main():
print("At", ctime(), "on Amazon...")
for isbn in ISBNs:
Thread(target=_show_ranking, args=(isbn,)).start()
@register
def _atexit():
print("all DONE at:", ctime())
if __name__ == "__main__":
_main()