Repository navigation
Expand file tree
/
Copy pathsynch.py
More file actions
89 lines (76 loc) · 2.31 KB
/
Copy pathsynch.py
File metadata and controls
89 lines (76 loc) · 2.31 KB
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
82
83
84
85
86
87
88
89
import os
import json
import requests
from requests.exceptions import HTTPError
import time
start_time = time.time()
GOOGLE_BOOKS_URL = "https://www.googleapis.com/books/v1/volumes?q=isbn:"
LIST_ISBN = [
'9780002005883',
'9780002238304',
'9780002261982',
'9780006163831',
'9780006178736',
'9780006280897',
'9780006280934',
'9780006353287',
'9780006380832',
'9780006470229',
]
BOOKS = []
class Book():
def __init__(self, isbn):
self.isbn = isbn
def __str__(self):
return self.isbn
def add_data(self, d):
self.data = d
def read(self):
return self.isbn, self.data
def extract_fields_from_response(item):
"""Extract fields from API's response"""
volume_info = item.get("volumeInfo", {})
title = volume_info.get("title", None)
subtitle = volume_info.get("subtitle", None)
description = volume_info.get("description", None)
published_date = volume_info.get("publishedDate", None)
return (
title,
subtitle,
description,
published_date,
)
def get_book_details_seq(isbn, session):
"""Get book details using Google Books API (sequentially)"""
url = GOOGLE_BOOKS_URL + isbn
response = None
try:
response = session.get(url)
response.raise_for_status()
print(f"Response status ({url}): {response.status_code}")
except HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"An error ocurred: {err}")
response_json = response.json()
items = response_json.get("items", [{}])[0]
return items
def run():
with requests.Session() as session:
for isbn in LIST_ISBN:
BOOKS.append(Book(isbn))
try:
response = get_book_details_seq(isbn, session)
parsed_response = extract_fields_from_response(response)
[b.add_data(parsed_response) for b in BOOKS if getattr(b, "isbn") == isbn]
print(f"Response: {json.dumps(parsed_response, indent=2)}")
except Exception as err:
print(f"Exception occured: {err}")
pass
def main():
run()
for b in BOOKS:
print(b.read())
if __name__ == '__main__':
main()
print("--- %s seconds ---" % (time.time() - start_time))