Created
July 17, 2018 23:20
-
-
Save Phoenix-Effect/3608410ac4ff1255aaea82420e60b775 to your computer and use it in GitHub Desktop.
Pulls ISBN numbers/titles from an airtable and then download's the book information from google books and reuploads to airtable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import re | |
from airtable import Airtable | |
from airtable.auth import AirtableAuth | |
import requests | |
import pprint | |
from pathlib import Path | |
APIKEY = "ENTER THIS" | |
BASEURL = "ENTER THIS" | |
GOOGLEAPIKEY = "ENTER THIS" | |
PP = pprint.PrettyPrinter(indent=4) | |
# given an isbn number returns the data | |
def getBookData(isbn): | |
r = requests.get('https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn) | |
return r.json() | |
# given an airtable row and book data update the row with data | |
def updateBookData(book, bookData): | |
authors = bookData['volumeInfo']['authors'] | |
authorID = findAuthor(authors) | |
if 'description' in bookData['volumeInfo']: | |
description = bookData['volumeInfo']['description'] | |
else: | |
description = "" | |
year = bookData['volumeInfo']['publishedDate'] | |
image = bookData['volumeInfo']['imageLinks']['thumbnail'] | |
imageAttachment = [{'url': image}] | |
if 'pageCount' in bookData['volumeInfo']: | |
length = bookData['volumeInfo']['pageCount'] | |
else: | |
length = -1 | |
fixed = True | |
sanitizedData = {'Author': authorID, 'Description': description, 'Year': year, | |
'Image': imageAttachment, 'sys.length': length, 'sys.fixed': fixed} | |
at = Airtable(BASEURL, "Resource-Books", APIKEY) | |
at.update(book['id'], sanitizedData) | |
# at.update("yesmess", sanitizedData) | |
return 0 | |
# check if author is in creators list, if not then makes it | |
def findAuthor(authors): | |
creatorTable = Airtable(BASEURL, "Creators", APIKEY) | |
authorIds = [] | |
for author in authors: | |
searchAuth = creatorTable.search('Name', author) | |
if not searchAuth: | |
authID = creatorTable.insert({'Name': author}) | |
authID = authID['id'] | |
else: | |
authID = searchAuth[0]['id'] | |
authorIds.append(authID) | |
return authorIds | |
# this fixes the book my adding missing information | |
def fixBook(book): | |
isbn = book['fields']['ISBN'] # get the isbn | |
isbn = isbn[isbn.find(':')+2:] | |
bookData = getBookData(isbn) | |
#PP.pprint(bookData) | |
if bookData['totalItems'] == 0: | |
print("NOT FOUND: " + book['fields']['Name']) | |
else: | |
print("UPDATING: " + book['fields']['Name']) | |
updateBookData(book, bookData['items'][0]) | |
print("UPDATED: " + book['fields']['Name']) | |
return bookData | |
# MAIN program | |
airtable = Airtable(BASEURL, "Resource-Books", APIKEY) | |
books = airtable.get_all(view='All', maxRecords=80) | |
for book in books: | |
if not 'Fixed' in book['fields']: | |
fixBook(book) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment