-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_openlibrary_data_by_isbn.py
executable file
·43 lines (32 loc) · 1.17 KB
/
get_openlibrary_data_by_isbn.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 python
"""
get book info with ISBN via openlibrary api
Example usage:
% ./get_openlibrary_data_by_isbn.py 1111111111
{"ISBN:1111111111": {"publishers": [{"name": "test publisher"}], "title": "test title", "url": "http://openlibrary.org/books/OL25760947M/test_title", "identifiers": {"openlibrary": ["OL25760947M"], "isbn_10": ["1111111111"]}, "publish_date": "2015", "key": "/books/OL25760947M", "authors": [{"url": "http://openlibrary.org/authors/OL650810A/abhishek_gaurav", "name": "abhishek gaurav"}]}}
"""
import requests
import sys
try:
if len(sys.argv) == 2:
isbn = sys.argv[1]
else:
print("Usage: %s ISBN" % str(sys.argv[0]))
sys.exit(1)
except IndexError:
print("Usage: %s ISBN" % str(sys.argv[0]))
sys.exit(1)
isbn = sys.argv[1]
if not isbn.isdigit():
print("ISBN values contain only digits")
sys.exit(1)
if len(isbn) != 10 and len(isbn) != 13:
print("ISBN values are 10 or 13 digits long")
sys.exit(1)
URL = "https://openlibrary.org/api/books?bibkeys=ISBN:{isbn}&jscmd=data&format=json"
r = requests.get(URL.format(isbn=isbn))
if(r.status_code == 200):
print(r.text)
else:
print("Failed to properly contact API")
sys.exit(1)