forked from Nmargolis/Landlords-Ratings-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed2.py
190 lines (141 loc) · 6.44 KB
/
seed2.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
"""Utility file to seed ratings database from MovieLens data in seed_data/"""
from model import User, Landlord, Address, Review, Convo, Userconvo, Message, Building, Resource
from model import connect_to_db, db
from server import app
import datetime
from faker import Faker
import requests
import json
import mapbox
import os
from random import randint
fake = Faker()
geocoder = mapbox.Geocoder(access_token='pk.eyJ1Ijoibm1hcmdvbGlzODkiLCJhIjoiY2lnbXZkZ3pwMDA0NG5ra280cmI4MzlwaCJ9.HHElSYk4EV_D2Jeg4gLJfg')
def load_users():
"""Create fake users and load into database."""
# Delete all rows in table to avoid adding duplicates
User.query.delete()
for i in range(0, 50):
user = User(fname=fake.first_name(),
lname=fake.last_name(),
email=fake.safe_email(),
password=fake.password(length=10, special_chars=False, digits=True, upper_case=True, lower_case=True))
# print user
# Add the user to the database
db.session.add(user)
# Commit all additons to database
db.session.commit()
def load_landlords():
"""Create fake landlords and load into database."""
# Delete all rows in table to avoid adding duplicates
Landlord.query.delete()
for i in range(0, 50):
landlord = Landlord(fname=fake.first_name(),
lname=fake.last_name())
# Add the landlord to the database
db.session.add(landlord)
# Commit all additions to database
db.session.commit()
def load_buildings():
"""Create fake buildings and load into database."""
# Delete all rows in table to avoid adding duplicates
Building.query.delete()
for i in range(0, 50):
# Question, if I only want 5 buildings, how do I do that when I'm creating addresses? Random number between 1 and 5?
building = Building(name=fake.company())
# Add the building to the database
db.session.add(building)
# Commit all additions to database
db.session.commit()
def load_addresses():
"""Create addresses and load into database."""
# Delete all rows in table to avoid adding duplicates
Address.query.delete()
# Initialize counter to add unit numbers 1/4 of the time
i = 0
# Iterate through text file with each line containing "lattitude|longigude"
for pair in open('data/u.coordinates'):
latlng = pair.split('|')
lat = latlng[0]
lng = latlng[1]
# Make request to mapbox geocoding api using lng and lat
# req = 'https://api.mapbox.com/geocoding/v5/mapbox.places/{},{}.json?access_token=pk.eyJ1Ijoibm1hcmdvbGlzODkiLCJhIjoibGxsVVJETSJ9.dQv5byiwSyj--mr7Bgwezw'.format(lng, lat)
# r = requests.get(req)
response = geocoder.reverse(lon=lng, lat=lat, types=['address'])
json_response = response.json()
# print 'response: ', json_response, '\n'
if json_response["features"] == []:
# print 'empty'
continue
else:
num = json_response["features"][0]["address"]
st = json_response["features"][0]["text"]
street = '{} {}'.format(num, st)
if len(json_response["features"][0]["context"]) == 5:
city = json_response["features"][0]["context"][1]["text"]
zipcode = json_response["features"][0]["context"][2]["text"]
state = json_response["features"][0]["context"][3]["text"]
country = json_response["features"][0]["context"][4]["text"]
else:
continue
# For some reason -122.42691912,37.81240737 doesn't have the city attribute in context.
# https://api.mapbox.com/geocoding/v5/mapbox.places/-122.42691912,37.81240737.json?types=address&access_token=pk.eyJ1Ijoibm1hcmdvbGlzODkiLCJhIjoibGxsVVJETSJ9.dQv5byiwSyj--mr7Bgwezw
# 1/4 of the times, create an address with unit and building id
if i % 4 == 0:
building_id = randint(0, 10)
address = Address(street=street,
city=city,
state=state,
zipcode=zipcode,
country=country,
unit=fake.building_number(),
lat=lat,
lng=lng,
building_id=building_id)
# The rest of the time, create an address without unit and building id
else:
address = Address(street=street,
city=city,
state=state,
zipcode=zipcode,
country=country,
lat=lat,
lng=lng,)
# Add the address to the database
db.session.add(address)
# Commit all additions to database
db.session.commit()
i += 1
def load_reviews():
"""Create reviews and load into database."""
# Delete all rows in table to avoid adding duplicates
Review.query.delete()
for i in range(0, 50):
# Set moved_in_at because moved_out_at and created_at depend on moved_in_at
moved_in_at = fake.date_time_between(start_date="-5y", end_date="now")
review = Review(user_id=randint(1, 50),
landlord_id=randint(1, 50),
address_id=randint(1, 44),
moved_in_at=moved_in_at,
moved_out_at=fake.date_time_between_dates(datetime_start=moved_in_at, datetime_end=None),
created_at=fake.date_time_between_dates(datetime_start=moved_in_at, datetime_end=None),
rating1=randint(1, 5),
rating2=randint(1, 5),
rating3=randint(1, 5),
rating4=randint(1, 5),
rating5=randint(1, 5),
comment=fake.text(max_nb_chars=500))
# Add the review to the database
db.session.add(review)
# Commit all additions to database
db.session.commit()
if __name__ == "__main__":
connect_to_db(app)
# # In case tables haven't been created, create them
# db.create_all()
# Import different types of data
load_users()
load_landlords()
load_buildings()
load_addresses()
load_reviews()