diff options
author | luxagraf <sng@luxagraf.net> | 2015-10-01 22:10:21 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2015-10-01 22:10:21 -0400 |
commit | e0de7a6c08bbbd31fae2e7b796457a982cb3e895 (patch) | |
tree | 998de97c36aef12098f5545d3dcfd690e600b23b /app/books/retriever.py | |
parent | 93ac5f4acb8d1249461425c97d46bbf69915112d (diff) |
did a lot of work on the books section
Diffstat (limited to 'app/books/retriever.py')
-rw-r--r-- | app/books/retriever.py | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/app/books/retriever.py b/app/books/retriever.py new file mode 100644 index 0000000..e98f46f --- /dev/null +++ b/app/books/retriever.py @@ -0,0 +1,60 @@ +import requests +url = "https://www.googleapis.com/books/v1/volumes?q=isbn:%s&key=AIzaSyCCYn5v58R08msS06pRfOzTHANUuG2tSHI" % (book.isbn) + +def get_book_isbn(book): + print(book.title) + if book.author_name != "Instapaper": + url = "https://www.googleapis.com/books/v1/volumes?q=%s+inauthor:%s&key=AIzaSyCCYn5v58R08msS06pRfOzTHANUuG2tSHI" % (book.title, book.author_name) + r = requests.get(url, timeout=3.001) + data = r.json() + isbn10 = "" + isbn13 = "" + for item in data["items"]: + for isbn in item["volumeInfo"]["industryIdentifiers"]: + print(isbn['identifier']) + if isbn['type'] == "ISBN_13": + isbn13 = isbn['identifier'] + elif isbn['type'] == "ISBN_10": + isbn10 = isbn['identifier'] + print(isbn10) + return isbn10 + break + +import json +def get_book_data(book): + if book.author_name != "Instapaper": + url = "https://openlibrary.org/api/books?bibkeys=ISBN:%s&format=json&jscmd=data" % book.isbn + r = requests.get(url, timeout=9.001) + j = json.loads(r.text) + obj = "ISBN:%s" % book.isbn + try: + pages = j[obj]['number_of_pages'] + except: + pages = '' + try: + publish_date = j[obj]['publish_date'] + except: + publish_date = '' + try: + publish_places = j[obj]['publish_places'][0]['name'] + except: + publish_places = '' + try: + openlib_url = j[obj]['url'] + except: + openlib_url = '' + print("pages " + str(pages)) + print("date " + publish_date) + print("place " + publish_places) + print("openlib " + openlib_url) + book.pages = pages + book.publish_date = publish_date + book.publish_place = publish_places + book.openlib_url = openlib_url + book.save() + +def fetch_image(book): + path = '%s/%s/%s.jpg' %(settings.IMAGES_ROOT, 'book-covers', book.slug) + r = requests.get(book.image, timeout=6.001) + im = io.StringIO(r.content) # constructs a StringIO holding the image + im.save(path) |