-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.py
313 lines (255 loc) 路 10.6 KB
/
seed.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
"""Script to seed database."""
import os
from random import choice, randint, getrandbits, randrange
from datetime import date, datetime, timedelta
from faker import Faker
from geopy.geocoders import Nominatim
import csv
import crud
import model
import server
os.system('dropdb books')
os.system('createdb books')
fake = Faker()
def random_date(start, end):
"""
This function will return a random datetime between two datetime
objects.
"""
delta = end - start
int_delta = (delta.days * 24 * 60 * 60) + delta.seconds
random_second = randrange(int_delta)
return start + timedelta(seconds=random_second)
d1 = datetime.strptime('1/1/2020 1:30 PM', '%m/%d/%Y %I:%M %p')
d2 = datetime.strptime('1/1/2021 4:50 AM', '%m/%d/%Y %I:%M %p')
rand_date = random_date(d1, d2)
users_in_db = []
def seed_users():
first_name = 'Yvonne'
last_name = 'Yeh'
username = 'yvonneyeh'
password = 'test'
password_hash = 'test'
join_date = '2020-03-17 12:34:56'
email = 'code@yvonneyeh.com'
website = 'yvonneyeh.com'
city = 'Palo Alto'
pronouns = 'she/her'
yy = crud.create_user(email, first_name, last_name, username, password, join_date, website, city, pronouns)
users_in_db.append(yy)
for n in range(20):
email = f'reader{n}@books.book'
first_name = fake.first_name()
last_name = fake.last_name()
username = first_name.lower() + '.' + last_name.lower()
# username = f'{first_name}{last_name}'
password_hash = 'test'
join_date = datetime.today()
website = None
city = None
pronouns = None
new_user = crud.create_user(email, first_name, last_name, username, password, join_date, website, city, pronouns)
users_in_db.append(new_user)
print(users_in_db)
ratings_in_db = []
def seed_ratings():
fave_books = ["Homegoing","Transcendent Kingdom","Steve Jobs"]
yy_books = ["The Circle","Bad Blood: Secrets and Lies in a Silicon Valley Startup","Imagine: How Creativity Works","The Pixar Touch: The Making of a Company","Brotopia: Breaking Up the Boys' Club of Silicon Valley"]
for title in fave_books:
# print(title)
book_id = crud.get_book_id_by_title(title)
user_id = 1
log_date = '2020-07-13'
# log_date = f"2020-07-13 {randint(0,23)}:{randint(0,59)}:{randint(0,59)}"
score = 5
read = True
my_book = crud.create_rating(user_id, book_id, log_date, score, read)
ratings_in_db.append(my_book)
for title in yy_books:
# print(title)
book_id = crud.get_book_id_by_title(title)
user_id = 1
log_date = random_date(d1, d2)
# log_date = f"2020-{randint(1,12)}-{randint(1,30)} {randint(0,23)}:{randint(0,59)}:{randint(0,59)}"
score = randint(3,5)
read = True
my_book = crud.create_rating(user_id, book_id, log_date, score, read)
ratings_in_db.append(my_book)
for n in range(100):
user_id = randint(1,10)
book_id = randint(1,80)
log_date = rand_date = random_date(d1, d2)
# log_date = datetime.today()
score = randint(1,5)
read = bool(getrandbits(1)) #choice([True, False])
rate_obj = crud.create_rating(user_id, book_id, log_date, score, read)
ratings_in_db.append(rate_obj)
print(ratings_in_db)
genres_in_db = []
def seed_genres(filename):
"""Add new genres to Genre table."""
opened_file = open(filename)
for name in opened_file:
if crud.get_genre_by_name(name) == None:
genre_obj = crud.create_genre(name)
genres_in_db.append(genre_obj)
print(genres_in_db)
authors_in_db = []
def seed_authors(filename):
f1 = open(filename)
csv_f = csv.reader(f1)
for row in csv_f:
author, link_href, title, author, description, genres, isbn, location, cover, date = row
# web_scraper_order, web_scraper_start_url, link, link_href, title, author, description, genres, isbn, location, cover, date = row
# print(crud.get_author_by_name(author))
if crud.get_author_by_name(author) == None:
# print(author)
# else:
author_obj = crud.create_author(author)
authors_in_db.append(author_obj)
wi_obj = crud.create_author("Walter Isaacson")
authors_in_db.append(wi_obj)
print(authors_in_db)
locs_in_db = []
def seed_addresses(filename):
"""Add new sample addresses to Location table."""
f = open(filename)
csv_f = csv.reader(f)
for row in csv_f:
name, address, city, state, zipcode, lat, lng = row
if crud.get_location_by_name(name) == None:
loc_obj = crud.create_location(name, float(lat), float(lng), address, city, state)
locs_in_db.append(loc_obj)
print(locs_in_db)
def seed_cities(filename):
"""Add new sample cities to Location table."""
f = open(filename)
csv_f = csv.reader(f)
for row in csv_f:
city, state, country, lat, lng = row
if crud.get_location_by_name(city) == None:
loc_obj = crud.create_location(city, float(lat), float(lng), city, city, state)
locs_in_db.append(loc_obj)
print(locs_in_db)
books_in_db = []
def seed_books(filename):
f2 = open(filename)
csv_f = csv.reader(f2)
for row in csv_f:
web_scraper_order, web_scraper_start_url, link_text, link_href, title, author, description, genres, isbn, location, cover_path, pub_date = row
a_num = crud.get_author_id_by_name(author)
# author_num = int(a_num[0])
author_num = a_num.author_id
# test_loc_id = 1
# if location != None:
# l_num = crud.get_location_id_by_name(location)
# location_num = int(l_num[0])
# print(l_num)
# print(location_num)
book_obj = crud.create_book(title, author_num, description, cover_path, isbn, link_href)
books_in_db.append(book_obj)
# wi = crud.get_author_id_by_name("Walter Isaacson")
# wi_num = int(wi[0])
# jobs_obj = crud.create_book(title, author_num, description, cover_path, isbn)
# books_in_db.append(jobs_obj)
print(books_in_db)
book_locs_in_db = []
def seed_book_locs():
# Sample data for multiple locations - Hackbright & Home
# for n in range (3,84):
# book_id = n
# loc_id = 1
# book_obj = crud.create_book_location(book_id, loc_id)
# book_locs_in_db.append(book_obj)
# for n in range (3,84):
# book_id = n
# loc_id = 2
# book_obj = crud.create_book_location(book_id, loc_id)
# book_locs_in_db.append(book_obj)
# Sample data for Apple Books
apple_books = ["Steve Jobs","Apple Confidential 2.0: The Definitive History of the World's Most Colorful Company","Dogfight: How Apple and Google Went to War and Started a Revolution", "Haunted Empire: Apple After Steve Jobs"]
apple_locs = ["1 Infinite Loop", "Apple Park", "Apple Central & Wolfe Campus","Apple Garage"]
n = 0
for book in apple_books:
if n < 4:
book_id = crud.get_book_id_by_title(book)
loc = apple_locs[n]
loc_obj = crud.get_location_by_name(loc)
loc_id = loc_obj.loc_id
book_obj = crud.create_book_location(book_id, loc_id)
book_locs_in_db.append(book_obj)
n += 1
jobs_locs = ["Steve Jobs' Home","Homestead High School"]
for loc in jobs_locs:
book_id = crud.get_book_id_by_title("Steve Jobs")
loc_obj = crud.get_location_by_name(loc)
loc_id = loc_obj.loc_id
book_obj = crud.create_book_location(book_id, loc_id)
book_locs_in_db.append(book_obj)
# Sample data for Google Books
goog_books = ["Are You Smart Enough to Work at Google?","Dogfight: How Apple and Google Went to War and Started a Revolution","Googled: The End of the World as We Know It","Google Speaks","How Google Works","I'm Feeling Lucky: The Confessions of Google Employee Number 59","Planet Google: One Company's Audacious Plan to Organize Everything We Know","The Big Switch: Rewiring the World, from Edison to Google","The Search: How Google and Its Rivals Rewrote the Rules of Business and Transformed Our Culture","What Would Google Do?","Work Rules!: Insights from Inside Google That Will Transform How You Live and Lead"]
goog_locs = ["Googleplex","Google X Lab","Google SF","Google RWC","Google Sunnyvale","Google Garage"]
n = 0
for book in goog_books:
if n < 6:
book_id = crud.get_book_id_by_title(book)
loc = goog_locs[n]
loc_obj = crud.get_location_by_name(loc)
loc_id = loc_obj.loc_id
book_obj = crud.create_book_location(book_id, loc_id)
book_locs_in_db.append(book_obj)
n += 1
for n in range(2,84):
book_id = n
loc_id = n
# if location != None:
# l_num = crud.get_location_id_by_name(location)
# location_num = int(l_num[0])
# print(l_num)
# print(location_num)
book_obj = crud.create_book_location(book_id, loc_id)
book_locs_in_db.append(book_obj)
print(book_locs_in_db)
book_genres_in_db = []
def seed_book_genres():
for n in range(1,86):
book_id = n
genre_id = randint(1,39)
book_obj = crud.create_book_genres(book_id, genre_id)
book_genres_in_db.append(book_obj)
print(book_genres_in_db)
def add_yy_books():
homegoing = 1
t_kingdom = 2
stanford = 3
tofu_house = 4
loc_obj = crud.get_location_by_name('Palo Alto')
paly = loc_obj.loc_id
hg_obj1 = crud.create_book_location(homegoing, stanford)
hg_obj2 = crud.create_book_location(homegoing, paly)
tk_obj1 = crud.create_book_location(t_kingdom, stanford)
tk_obj2 = crud.create_book_location(t_kingdom, tofu_house)
tk_obj3 = crud.create_book_location(t_kingdom, paly)
book_locs_in_db.append(hg_obj1)
book_locs_in_db.append(hg_obj2)
book_locs_in_db.append(tk_obj1)
book_locs_in_db.append(tk_obj2)
book_locs_in_db.append(tk_obj3)
#---------------------------------------------------------------------#
if __name__ == '__main__':
model.connect_to_db(server.app)
# Create tables if not already created. Delete all existing entries in tables.
model.db.create_all()
print("Tables created. Deleting all rows and creating new seed data.")
# Seed sample data into the database
seed_authors('data/authors.csv')
seed_books('data/sv_books.csv')
seed_addresses('data/addresses.csv')
seed_cities('data/cities.csv')
seed_book_locs()
seed_genres('data/genres.txt')
seed_book_genres()
seed_users()
seed_ratings()
add_yy_books()
print("Sample data seeded")