-
Notifications
You must be signed in to change notification settings - Fork 0
/
bookfi.py
65 lines (55 loc) · 1.5 KB
/
bookfi.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
57
58
59
60
61
62
63
64
65
#!/usr/bin/python
import doctest
import os
import sys
from parser import BookfiParser
from url import *
URL_PREFIX = 'http://en.bookfi.org/s/?q='
URL_BOOKSEE = 'http://en.booksee.org/s/?q='
def generate_search_url(query):
"""Retourne l'url de recherche sur booksee a partir d'une requete."""
l = query.split()
return URL_BOOKSEE + '+'.join(l)
def get_results(url_query):
"""Retourne la liste des urls des resultats d'une recherche"""
html = get_page(url_query)
parser = BookfiParser()
return parser.get_results(html)
def download(url_download):
md5 = get_location(url_download)
os.system('wget "%s" --referer="%s"' % (url_download, md5))
def choose_answer(results):
"""Demande a l'utilisateur de choisir dans une liste"""
for index, el in enumerate(results):
print "[%d] %s - %s" % (index, el['title'], el['author'])
while True:
try:
i = int(raw_input('Your choice: '))
except (KeyboardInterrupt, EOFError):
return None
except ValueError:
print 'Bad choice'
continue
if i >= 0 and i < len(results):
return results[i]['url']
else:
print 'Bad choice'
return None
if __name__ == '__main__':
doctest.testmod()
while True:
try:
query = raw_input('Search: ')
except (KeyboardInterrupt, EOFError):
print "\n"
sys.exit(0)
search_url = generate_search_url(query)
results = get_results(search_url)
if results:
choice = choose_answer(results)
if choice:
url_download = get_location(choice)
download(url_download)
print "\n"
else:
print 'Not found'