-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
237 lines (171 loc) · 8.18 KB
/
crud.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
""" CRUD file for the LocalSound project """
from model import db, User, Artist, Location, connect_to_db
from spotipy.oauth2 import SpotifyOAuth, SpotifyClientCredentials
import spotipy
import random
import os
API_KEY = os.environ['SPOTIPY_CLIENT_SECRET']
SPOTIPY_CLIENT_ID = os.environ['SPOTIPY_CLIENT_ID']
auth_manager = SpotifyClientCredentials()
sp = spotipy.Spotify(auth_manager=auth_manager)
def seed_artist(artist_name, artist_password, city, state, artist_URI):
""" The original artist seed data"""
seed_status = True
artist_name = artist_name.lower()
state = state.lower()
if city == '' or city == None:
city = None
else:
city = city.lower()
artist = Artist.query.filter(Artist.artist_name == artist_name).one_or_none()
location = Location.query.filter(Location.city == city, Location.state == state).one_or_none()
if artist == None:
if location == None: # See create_location for why this is here #When called in server call extra
loc_obj = Location.query.filter_by(city = None, state = state).one()
new_artist = Artist(artist_name = artist_name, artist_password = artist_password, seed_status = seed_status,
location_id = loc_obj.location_id, artist_URI = artist_URI)
db.session.add(new_artist)
return new_artist
else:
loc_obj = Location.query.filter_by(city = city, state = state).one()
new_artist = Artist(artist_name = artist_name, artist_password = artist_password, seed_status = seed_status,
location_id = loc_obj.location_id, artist_URI = artist_URI)
db.session.add(new_artist)
return new_artist
else:
return False
def seed_location(city, state):
""" Adds a new location to the DB"""
# This should be able to take in a location 2 ways.
# 1. When the csv is loaded into the application via seed.
# 2. When a user or artist inputs a location that is not currently in the db 3.0
state = state.lower()
if city == '' or city == None:
city = None
else:
city = city.lower()
location = Location.query.filter(Location.city == city, Location.state == state).one_or_none()
if location == None:
new_location = Location(state = state, city = city)
db.session.add(new_location)
return new_location
def create_user(name, password, city, state):
""" Adds a new user to the user table"""
if city == '' or city == None:
city = None
else:
city = city.lower()
state = state.lower()
user = User.query.filter(User.u_name == name).one_or_none() #us.one_or_none() allows for exceptions when handling errors
location = Location.query.filter(Location.city == city, Location.state == state).one_or_none()
if user == None:
if location == None: #Checks if location doesn't exist
state = Location.query.filter_by(state = state, city = None).one() #If location doesn't exist yet, assign the default (state code)
new_user = User(u_name = name, u_password = password, location_id = state.location_id)
db.session.add(new_user)
db.session.commit()
return new_user
else:
loc_obj = Location.query.filter_by(city = city, state = state).one() # gets the PK from location db
new_user = User(u_name = name, u_password = password, location_id = loc_obj.location_id)
db.session.add(new_user)
db.session.commit()
return new_user
else:
return False
def create_artist(artist_name, artist_password, city, state, artist_URI, link_1 = None, link_2 = None):
""" Adds a new artist from the form and adds it to the artist table"""
seed_status = False
artist_name = artist_name.lower()
state = state.lower()
if city == '' or city == None:
city = None
else:
city = city.lower()
artist = Artist.query.filter(Artist.artist_name == artist_name).one_or_none()
location = Location.query.filter(Location.city == city, Location.state == state).one_or_none()
# ^^^^ Multiple rows were found when one or none was required ^^^^
if artist == None:
if location == None:
new_location = Location(city = city, state = state) #
db.session.add(new_location) #
db.session.commit() #
loc_obj = Location.query.filter_by(city = city, state = state).one()
new_artist = Artist(artist_name = artist_name, artist_password = artist_password, seed_status = seed_status,
location_id = loc_obj.location_id, artist_URI = artist_URI, link_1 = link_1, link_2 = link_2)
db.session.add(new_artist)
db.session.commit()
return new_artist
else:
loc_obj = Location.query.filter_by(city = city, state = state).one()
new_artist = Artist(artist_name = artist_name, artist_password = artist_password, seed_status = seed_status,
location_id = loc_obj.location_id, artist_URI = artist_URI, link_1 = link_1, link_2 = link_2)
db.session.add(new_artist)
db.session.commit()
return new_artist
else:
return False
def create_location(city, state):
""" Adds a new location to the DB.
Called in create_artist."""
state = state.lower()
if city == '' or city == None:
city = None
else:
city = city.lower()
location = Location.query.filter(Location.city == city, Location.state == state).one_or_none()
if location == None:
new_location = Location(state = state, city = city)
db.session.add(new_location)
db.session.commit()
return new_location
def get_user_by_username(username):
"""gets the user by their username """
user = User.query.filter(User.u_name == username).one_or_none()
return user
def get_artist_by_name(name):
"""gets the artist by their stagename """
artist = Artist.query.filter(Artist.artist_name == name).one_or_none()
return artist
def get_artists(city, state):
""" Gets all artists in a particular location """
city = city.lower()
state = state.lower()
rec_list = []
count = 0
loc_obj = Location.query.filter(Location.city == city, Location.state == state).first()
if loc_obj is None:
loc_obj = Location.query.filter(Location.city == None, Location.state == state).one()
artists = Artist.query.filter(Artist.location_id == loc_obj.location_id).all()
for artist in artists:
rec_list.append(artist.artist_name)
rec_lis = random.sample(rec_list, k=40) #Picks 30 random artists to get from API, cut down to playlist of 10
return rec_lis
def spotify_info(artists):
""" This takes in a pre-selected artist list and returns a sorted dictionary """
spotify_dic = {}
count = 0
for artist in artists:
artist_info = sp.search(artist, limit = 1, type = 'artist')
artist_items = artist_info['artists']['items']
if len(artist_items) > 0:
if artist == artist_items[0]['name'].lower():
try:
spotify_dic[count] = {}
artist_tracks = sp.artist_top_tracks(artist_items[0]['id'])
spotify_dic[count]['artist_pic'] = artist_items[0]['images'][2]['url'] #Artist image how do I turn this into an image? src in HTML
spotify_dic[count]['artist_name'] = artist_tracks['tracks'][0]['artists'][0]['name']
spotify_dic[count]['track_name'] = artist_tracks['tracks'][0]['name']
spotify_dic[count]['album_name'] = artist_tracks['tracks'][0]['album']['name']
spotify_dic[count]['track_preview'] = artist_tracks['tracks'][0]['preview_url']
count += 1
except:
pass
if count == 10:
return spotify_dic
return spotify_dic
if __name__ == "__main__":
from server import app
connect_to_db(app)
# db.drop_all()
# db.create_all()