Skip to content

Instantly share code, notes, and snippets.

Created July 17, 2018 23:20
Show Gist options
  • Save Phoenix-Effect/3608410ac4ff1255aaea82420e60b775 to your computer and use it in GitHub Desktop.
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.
import os
import re
from airtable import Airtable
from airtable.auth import AirtableAuth
import requests
import pprint
from pathlib import Path
PP = pprint.PrettyPrinter(indent=4)
# given an isbn number returns the data
def getBookData(isbn):
r = requests.get('' + 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']
description = ""
year = bookData['volumeInfo']['publishedDate']
image = bookData['volumeInfo']['imageLinks']['thumbnail']
imageAttachment = [{'url': image}]
if 'pageCount' in bookData['volumeInfo']:
length = bookData['volumeInfo']['pageCount']
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 ='Name', author)
if not searchAuth:
authID = creatorTable.insert({'Name': author})
authID = authID['id']
authID = searchAuth[0]['id']
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)
if bookData['totalItems'] == 0:
print("NOT FOUND: " + book['fields']['Name'])
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']:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment