Skip to content

Instantly share code, notes, and snippets.

@atihkin
Last active April 27, 2020 12:47
Show Gist options
  • Save atihkin/ace09d8d1234dd526d527b02f34ffa0f to your computer and use it in GitHub Desktop.
Save atihkin/ace09d8d1234dd526d527b02f34ffa0f to your computer and use it in GitHub Desktop.
Calling the Google Books API to categorize books using ISBN
import pandas as pd, requests, json, csv
df = pd.ExcelFile('books.xlsx').parse('Sheet1')
df1=df['ISBN13']
isbn_list = df1.tolist()
headers = {
'apikey': 'API-KEY-HERE'
}
for i in isbn_list:
response = requests.get(
'https://www.googleapis.com/books/v1/volumes?q=isbn:' + str(int(i)),
headers=headers)
jsonRes = json.loads(response.text)
if jsonRes['totalItems']!=0:
if not(jsonRes['items'][0]['volumeInfo'].get('categories')) is None:
print str(jsonRes['items'][0]['volumeInfo']['categories'])
with open('categories.csv',"a") as csvfile:
bookwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
bookwriter.writerow(jsonRes['items'][0]['volumeInfo']['categories'])
else:
print 'category does not exist'
with open('categories.csv',"a") as csvfile:
bookwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
bookwriter.writerow('category does not exist')
else:
print 'does not exist'
with open('categories.csv',"a") as csvfile:
bookwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
bookwriter.writerow('does not exist')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment