-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmetadata_fetcher.py
82 lines (75 loc) · 3.54 KB
/
metadata_fetcher.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import google_api
import logging
logger = logging.getLogger(__name__)
class book:
def __init__(self,name):
gbooks = google_api.connect("books","v1")
books_count = gbooks.mylibrary().bookshelves().get(shelf = "7").execute()["volumeCount"]
books_list = gbooks.mylibrary().bookshelves().volumes().list(shelf="7",maxResults = books_count).execute()["items"]
logger.info(f"Total {books_count} books found")
for book in books_list:
if book["volumeInfo"]["title"] == name:
logger.info(f"Fetching metadata for {name}")
self.found_book = True
self.name = book["volumeInfo"]["title"]
self.url = book["accessInfo"]["webReaderLink"][0:-39]
if "authors" in book["volumeInfo"]:
self.authors = book["volumeInfo"]["authors"]
else:
self.authors = []
if "publisher" in book["volumeInfo"]:
self.publisher = book["volumeInfo"]["publisher"]
else:
self.publisher = ""
query = f"intitle:{self.name}"
for author in self.authors:
query += f" inauthor:{author}"
if len(self.publisher):
query += f" inpublisher:{self.publisher}"
try:
books_query = gbooks.volumes().list(q = query,orderBy = "relevance",langRestrict ="en-GB,en").execute()["items"][0]
book_query_response = books_query["volumeInfo"]
if "imageLinks" in book_query_response:
self.thumbnail = book_query_response["imageLinks"]["thumbnail"]
else:
self.thumbnail = None
if "industryIdentifiers" in book_query_response:
self.isbn = book_query_response["industryIdentifiers"][0]["identifier"]
else:
self.isbn = 0
if "id" in book_query_response:
self.id = books_query['id']
else:
self.id = ""
if "description" in book_query_response:
self.about = book_query_response["description"]
else:
self.about = ""
if "categories" in book_query_response:
self.categories = book_query_response["categories"]
else:
self.categories = []
if "pageCount" in book_query_response:
self.page_count = book_query_response["pageCount"]
else:
self.page_count = 0
if "publishedDate" in book_query_response:
self.publishedDate = book_query_response["publishedDate"]
else:
self.publishedDate = ""
try:
self.isbn = int(self.isbn)
except:
self.isbn = 0
except:
self.thumbnail = None
self.isbn = 0
self.id = ""
self.about = ""
self.categories = []
self.page_count = 0
self.publishedDate = ""
break
else:
self.found_book = False
logger.info(f"Book with title '{name}' does not exist in books library")