This repository has been archived by the owner on Feb 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getbookinfo.py
75 lines (60 loc) · 2.26 KB
/
getbookinfo.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
#!/usr/bin/env python
# -----------------------------------------------------------------------
# getbookinfo.py
# Author: Sophie Li, Jayson Wu, Connie Xu
# -----------------------------------------------------------------------
# this script retrieves the book info given the isbn
import requests
from book import Book
from sys import argv
# given an isbn, retrieves from googlebooks api info and returns a list in the following form
# [title, subtitle, authors, publisher, publishedDate, description]
def getBookInfo(isbn):
num = str(isbn)
r = requests.get('https://www.googleapis.com/books/v1/volumes?q=isbn:'+num)
req = r.text.replace('false', '\'false\'')
req = req.replace('true', '\'true\'')
# dictionary containing data from http request
bookInfo = eval(req)
if isbn is '':
return None
if 'error' in bookInfo:
return 'error query'
elif bookInfo['totalItems'] == 0:
return None
else:
# dictionary containing specifics about book
volumeInfo = bookInfo['items'][0]['volumeInfo']
title = ''
subtitle = ''
authors = []
publisher = ''
publishedDate = ''
description = ''
image=''
if 'title' in volumeInfo:
title = volumeInfo['title']
if 'subtitle' in volumeInfo:
subtitle = volumeInfo['subtitle']
if 'authors' in volumeInfo:
for author in volumeInfo['authors']:
authors.append(author)
if 'publisher' in volumeInfo:
publisher = volumeInfo['publisher']
if 'publishedDate' in volumeInfo:
publishedDate = volumeInfo['publishedDate']
if 'description' in volumeInfo:
description = volumeInfo['description']
if 'imageLinks' in volumeInfo:
if 'thumbnail' in volumeInfo['imageLinks']:
image = volumeInfo['imageLinks']['thumbnail']
book = Book(isbn, title, subtitle, authors, publisher,
publishedDate, description, image)
return book
# takes in an isbn from the command line and returns book info
def main(argv):
isbn = argv[1]
print(getBookInfo(isbn))
# tested books: 9780134076454, 9780321498052, 9780133133417
if __name__ == '__main__':
main(argv)