/
create_db.py
212 lines (201 loc) · 6.08 KB
/
create_db.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import psycopg2
import json, logging
from sqlalchemy.orm import sessionmaker
from models import db, Book, Author, Publisher
#from models import Base, Book, Author, Publisher, engine
#Base.metadata.bind = engine
#DBSession = sessionmaker(bind=engine)
#session = DBSession()
conn = psycopg2.connect(host="localhost", database="bookdb", user="patrick", password="sqlRAGTAG", port="5432")
cursor = conn.cursor()
def load_json(filename):
with open(filename) as file:
jsn = json.load(file)
file.close()
return jsn
def create_books():
book = load_json('books.json')
counter = 0
dupe = False
for oneBook in book:
title = oneBook['title']
g_id = oneBook['google_id']
try:
isbn_ = oneBook['isbn']
except:
isbn_ = "None"
try:
img_url = oneBook['image_url']
except:
img_url = "None"
try:
pub_date = oneBook['publication_date']
except:
pub_date = "None"
try:
desc = oneBook['description']
except:
desc = "None"
publishers_attribute = oneBook['publishers']
for i in publishers_attribute:
publisher = i['name']
authors_attribute = oneBook['authors']
for j in authors_attribute:
author = j['name']
cursor.execute('SELECT id, name FROM author')
rows = cursor.fetchall()
print(author)
for row in rows:
#print(row)
if(row[1] == author):
author_id = row[0]
break
print(author_id)
cursor.execute('SELECT id, name FROM publisher')
rows = cursor.fetchall()
print(publisher)
for row in rows:
#print(row)
if(row[1] == publisher):
pub_id = row[0]
break
print(pub_id)
newBook = Book(id = counter, title = title, google_id = g_id, isbn = isbn_, image_url = img_url, publication_date = pub_date, description = desc, author = author, publisher = publisher, pub_id = pub_id, author_id = author_id)
cursor.execute("SELECT google_id FROM book")
rows = cursor.fetchall()
for row in rows:
if g_id in row:
dupe = True
continue
if(not dupe):
# After I create the book, I can then add it to my session.
db.session.add(newBook)
# commit the session to my DB.
db.session.commit()
counter += 1
print("we added a book into the DB! it was: "+title)
else:
print("there was a dupe!")
dupe = False
def create_authors():
book = load_json('books.json')
counter = 0
dupe = False
for oneBook in book:
title = oneBook['title']
publishers_attribute = oneBook['publishers']
for i in publishers_attribute:
publisher = i['name']
authors_attribute = oneBook['authors']
for j in authors_attribute:
author = j['name']
try:
born = j['born']
except:
born = "None"
try:
education = j['education']
except:
education = "None"
try:
nationality = j['nationality']
except:
nationality = "None"
try:
desc = j['description']
except:
desc = "None"
try:
alma = j['alma_mater']
except:
alma = "None"
try:
wiki = j['wikipedia_url']
except:
wiki = "None"
try:
died = j['died']
except:
died = "None"
try:
img_url = j['image_url']
except:
img_url = "None"
newAuthor = Author(id = counter, name = author, born = born, education = education, nationality = nationality, description = desc, alma_mater = alma, wikipedia_url = wiki, image_url = img_url)
cursor.execute("SELECT name FROM author")
rows = cursor.fetchall()
for row in rows:
if author in row:
dupe = True
continue
if(not dupe):
db.session.add(newAuthor)
db.session.commit()
counter += 1
print("we added an author into the DB! it was: "+author)
else:
print("there was a dupe!")
dupe = False
'''
if session.query(Authors).filter(Authors.name == newAuthor.name).count() == 0:
db.session.add(newAuthor)
db.session.commit()
counter += 1
'''
def create_publishers():
book = load_json('books.json')
counter = 0
dupe = False
for oneBook in book:
title = oneBook['title']
publishers_attribute = oneBook['publishers']
authors_attribute = oneBook['authors']
for j in authors_attribute:
author = j['name']
for i in publishers_attribute:
publisher = i['name']
try:
wiki = i['wikipedia_url']
except:
wiki = "None"
try:
desc = i['description']
except:
desc = "None"
try:
owner = i['owner']
except:
owner = "None"
try:
img_url = i['image_url']
except:
img_url = "None"
try:
website = i['website']
except:
website = "None"
newPublisher = Publisher(id = counter, name = publisher, wikipedia_url = wiki, description = desc, owner = owner, image_url = img_url, website = website)
cursor.execute('SELECT name FROM publisher')
rows = cursor.fetchall()
for row in rows:
if publisher in row:
dupe = True
continue
if(not dupe):
db.session.add(newPublisher)
db.session.commit()
counter += 1
print("we added a publisher into the DB! it was " + publisher)
else:
print("there was a dupe!")
dupe = False
'''
if session.query(Publishers).filter(Publishers.name == newPublisher.name).count() == 0:
session.add(newPublisher)
session.commit()
counter += 1
'''
create_authors()
create_publishers()
create_books()
cursor.close()