-
Notifications
You must be signed in to change notification settings - Fork 51
/
bookrank3CF.py
43 lines (33 loc) · 1.04 KB
/
bookrank3CF.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
#!/usr/bin/env python3
from atexit import register
from concurrent.futures import ThreadPoolExecutor
from re import compile
import requests
from threading import Thread
from time import ctime
#from urllib.request import urlopen - Doesn't work.
REGEX = compile(b"#([\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):
with requests.get("{0}{1}".format(AMZN, isbn)) as page:
return str(REGEX.findall(page.content)[0], "utf-8")
def _show_ranking(isbn):
print("- %r ranked %s" % (ISBNs[isbn], get_ranking(isbn)))
def _main():
print("At", ctime(), "on Amazon...")
with ThreadPoolExecutor(3) as executor:
for isbn, ranking in zip(
ISBNs,
executor.map(get_ranking, ISBNs)
):
print("- %r ranked %s" % (ISBNs[isbn], ranking))
@register
def _atexit():
print("all DONE at:", ctime())
if __name__ == "__main__":
_main()