-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
78 lines (64 loc) · 2.93 KB
/
helpers.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import os
import requests
import urllib.parse
from flask import redirect, render_template, request, session
from functools import wraps
def lookup(search):
"""Look up quote for symbol."""
# Contact API
try:
api_key = os.environ.get("API_KEY")
response = requests.get(f"https://www.googleapis.com/books/v1/volumes?q={urllib.parse.quote_plus(search)}&key={api_key}")
response.raise_for_status()
except requests.RequestException:
return None
# Parse response
try:
search = response.json()
allBooks = [] #UPDATED
eachBook = [] #UPDATED
for value in search.values():
if type(value) == list:
for i in value:
identity = i['id']
eachBook += [identity]
for a, ve in i.items():
if type(ve) == dict and a == 'volumeInfo':
for row, re in ve.items():
if row == 'title':
name = re
eachBook += [name] #UPDATED
if row == 'description':
description = re
eachBook += [description] #UPDATED
if row == 'authors':
authors = re[0]
if len(re) > 1:
for names in range(1, len(re)):
authors += ", " +re[names]
authors += "."
eachBook += [authors] #UPDATED
if row == 'pageCount':
pagecount = str(re)
eachBook += [pagecount]#UPDATED
if row == 'industryIdentifiers':
for i in re:
if i['type'] =='ISBN_13':
indentifier = i['identifier']
eachBook += [indentifier]
allBooks += [eachBook]#UPDATED
eachBook = [] #UPDATED
return allBooks #UPDATED
except (KeyError, TypeError, ValueError):
return None
def login_required(f):
"""
Decorate routes to require login.
http://flask.pocoo.org/docs/1.0/patterns/viewdecorators/
"""
@wraps(f)
def decorated_function(*args, **kwargs):
if session.get("user_id") is None:
return redirect("/login")
return f(*args, **kwargs)
return decorated_function