-
Notifications
You must be signed in to change notification settings - Fork 7
/
request_book.py
161 lines (124 loc) · 4.68 KB
/
request_book.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
""" method for rendering requested books
Main module will build a dictionary from a json string, in this case from a file.
"""
def reorganize_openlibrary_data(k, v):
""" Prepare openlibrary data to become a book object.
The k value isn't really used anymore but it is preserved for now.
"""
def serialize_multi_entry_fields(multi_entry_list, extraction_key="", separator="; "):
""" Take a list of similar dicts and serialize some contents to a string
This dict structure is specific to openlibrary.
The extraction key is specific to the list of dicts being encoded.
The separator is arbitrary and used for readibility and later parsing.
"""
# intermediate list which will be joined to produce the encoded string
result_list = list()
for dict_item in multi_entry_list:
result_list.append(dict_item[extraction_key])
return separator.join(result_list)
# what is this dict for? possibly intended to make a dict instead of list.
# see note below where the list is added to session.
bookdata_dict = dict()
simpledata_list = list()
# Try for each ISBN, prefer ISBN13.
ISBN_acquired = False
isbn_to_store = ""
try:
# try the isbn 13 then the 10
isbn_to_store = v['identifiers']['isbn_13'][0]
ISBN_acquired = True
except KeyError:
pass
# get the isbn10 if the isbn13 failed.
if not ISBN_acquired:
try:
# try the isbn 13 then the 10
isbn_to_store = v['identifiers']['isbn_10'][0]
ISBN_acquired = True
except KeyError:
pass
# append isbn or empty string.
simpledata_list.append(isbn_to_store)
try:
# just take the first one in the list
simpledata_list.append(v['identifiers']['openlibrary'][0])
except KeyError:
simpledata_list.append("")
try:
# just take the first one in the list
simpledata_list.append(v['identifiers']['lccn'][0])
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(v['title'])
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(v['number_of_pages'])
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(v['publish_date'])
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(serialize_multi_entry_fields(v['authors'],'name'))
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(serialize_multi_entry_fields(v['subjects'],'name'))
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(v["cover"]["medium"])
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(v["ebooks"][0]["preview_url"])
except KeyError:
simpledata_list.append("")
try:
simpledata_list.append(v['classifications']['dewey_decimal_class'][0])
except KeyError:
simpledata_list.append("")
# location
simpledata_list.append("")
return simpledata_list
if __name__ == "__main__":
"""
"""
from controller import db, Book
from sqlalchemy.exc import IntegrityError
import json
# import this to build a database
# no idea why you would want this, but it can be used as a base for manual fixtures or something.
db.create_all()
sourcejsonfile = "dataman/UPDATE_DB_FOR_OLID/isbn_response_data_052916_1628.json"
with open(sourcejsonfile) as f:
source_data = json.load(f)
# bookdata_list order: 0. isbn, 1. title, 2. # pages, 3. publish_date, 4. authors.
# there are more fields but this needs to become a dict
for k, v in source_data.iteritems():
bookdata_list = reorganize_openlibrary_data(k, v)
# convert this to a dict and pass to session that way
"""
bookdata = Book(bookdata_list[0],
bookdata_list[1],
bookdata_list[2],
bookdata_list[3],
bookdata_list[4],
bookdata_list[5],
bookdata_list[6],
bookdata_list[7],
bookdata_list[8],
bookdata_list[9],
bookdata_list[10])
"""
# just use unpack for now
bookdata = Book(*bookdata_list)
db.session.add(bookdata)
try:
db.session.commit()
except IntegrityError:
print("Book failed, already included: {}".format(bookdata_list[1], bookdata_list[0]))
db.session.rollback()