summaryrefslogtreecommitdiff
path: root/app/books/retriever.py
blob: e98f46f8810a691294f505da42a6cc169ac96b16 (plain)
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
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)