88 update_album ,
99)
1010from src .database .crud .genre import create_genre
11- from src .database .crud .track import (
12- all_tracks_have_artists ,
13- create_track_or_none ,
14- get_album_tracks ,
15- get_user_albums_with_no_tracks ,
16- )
1711from src .database .crud .playlist import (
18- add_playlist_album_index ,
1912 create_playlist ,
20- get_playlist_albums ,
13+ delete_playlist ,
2114 get_playlist_by_id_or_none ,
22- get_user_playlists ,
23- playlist_has_null_album_indexes ,
24- update_playlist_with_albums ,
2515)
2616from src .database .crud .user import get_or_create_user
2717from src .musicbrainz import MusicbrainzClient
@@ -38,60 +28,28 @@ def database_controller(spotify: SpotifyClient, musicbrainz: MusicbrainzClient):
3828 def populate_user ():
3929 access_token = request .cookies .get ("spotify_access_token" )
4030 user = spotify .get_current_user (access_token )
31+ (db_user , _ ) = get_or_create_user (user )
4132 simplified_playlists = spotify .get_all_playlists (
4233 user_id = user .id , access_token = access_token
4334 )
44- (db_user ,) = get_or_create_user (user )
4535
4636 for simplified_playlist in simplified_playlists :
47- if "Albums" in simplified_playlist .name :
37+ if (
38+ "Albums" in simplified_playlist .name
39+ and "New Albums 06/09/24" in simplified_playlist .name
40+ ):
41+ delete_playlist ("0ubqg4akSDebHReJIG8sbd" )
4842 db_playlist = get_playlist_by_id_or_none (simplified_playlist .id )
49-
50- if db_playlist is None :
51- [playlist , albums ] = [
52- spotify .get_playlist (
53- access_token = access_token , id = simplified_playlist .id
54- ),
55- spotify .get_playlist_album_info (
56- access_token = access_token , id = simplified_playlist .id
57- ),
58- ]
59- create_playlist (playlist , albums , db_user )
60- else :
61- if db_playlist .snapshot_id != simplified_playlist .snapshot_id :
62- [playlist , albums ] = [
63- spotify .get_playlist (
64- access_token = access_token , id = simplified_playlist .id
65- ),
66- spotify .get_playlist_album_info (
67- access_token = access_token , id = simplified_playlist .id
68- ),
69- ]
70- update_playlist_with_albums (playlist , albums )
71-
72- return make_response ("Playlist data populated" , 201 )
73-
74- @database_controller .route (
75- "populate_additional_album_details_from_playlist" , methods = ["GET" ]
76- )
77- def populate_additional_album_details_from_playlist ():
78- access_token = request .cookies .get ("spotify_access_token" )
79- user = spotify .get_current_user (access_token )
80- playlists = get_user_playlists (user .id )
81-
82- for playlist in playlists :
83- albums = get_playlist_albums (playlist .id )
84- if albums == []:
85- continue
86- batch_albums = split_list (albums , 20 )
87- for album_chunk in batch_albums :
88- sleep (0.5 )
89- albums = spotify .get_multiple_albums (
90- access_token = access_token , ids = [album .id for album in album_chunk ]
91- )
92- for db_album in albums :
93- album = spotify .get_album (access_token = access_token , id = db_album .id )
94- update_album (album )
43+ if (
44+ db_playlist is None
45+ or db_playlist .snapshot_id != simplified_playlist .snapshot_id
46+ ):
47+ if db_playlist is not None :
48+ delete_playlist (db_playlist .id )
49+ playlist = spotify .get_playlist (
50+ access_token = access_token , id = simplified_playlist .id
51+ )
52+ create_playlist (playlist , db_user )
9553
9654 return make_response ("Playlist data populated" , 201 )
9755
@@ -100,12 +58,8 @@ def populate_additional_album_details():
10058 access_token = request .cookies .get ("spotify_access_token" )
10159 user = spotify .get_current_user (access_token )
10260 albums = get_user_albums (user .id )
103- albums_without_label = [album for album in albums ] # if album.label is None]
104- if albums_without_label == []:
105- return make_response ("No Albums to process" , 204 )
106- batch_albums = split_list (albums_without_label , 20 )
61+ batch_albums = split_list (albums , 20 )
10762 for album_chunk in batch_albums :
108- sleep (0.5 )
10963 albums = spotify .get_multiple_albums (
11064 access_token = access_token , ids = [album .id for album in album_chunk ]
11165 )
@@ -130,74 +84,6 @@ def populate_user_album_genres():
13084 populate_album_genres_by_user_id (user .id , musicbrainz )
13185 return make_response ("User album genres populated" , 201 )
13286
133- @database_controller .route ("populate_album_tracks" , methods = ["GET" ])
134- def populate_album_tracks ():
135- access_token = request .cookies .get ("spotify_access_token" )
136- user = spotify .get_current_user (access_token )
137- albums = get_user_albums_with_no_tracks (user .id )
138- for album in albums :
139- existing_album_tracks = get_album_tracks (album )
140- if existing_album_tracks != []:
141- continue
142- sleep (0.5 )
143-
144- album_tracks = spotify .get_album (
145- access_token = access_token , id = album .id
146- ).tracks .items
147- for track in album_tracks :
148- create_track_or_none (track , album )
149-
150- return make_response ("User album genres populated" , 201 )
151-
152- @database_controller .route ("populate_playlist_album_indexes" , methods = ["GET" ])
153- def populate_playlist_album_indexes ():
154- access_token = request .cookies .get ("spotify_access_token" )
155- user = spotify .get_current_user (access_token )
156- simplified_playlists = spotify .get_all_playlists (
157- user_id = user .id , access_token = access_token
158- )
159-
160- for simplified_playlist in simplified_playlists :
161- if "Albums" in simplified_playlist .name :
162- if not playlist_has_null_album_indexes (simplified_playlist .id ):
163- continue
164- sleep (1 )
165-
166- [playlist , albums ] = [
167- spotify .get_playlist (
168- access_token = access_token , id = simplified_playlist .id
169- ),
170- spotify .get_playlist_album_info (
171- access_token = access_token , id = simplified_playlist .id
172- ),
173- ]
174-
175- for index , album in enumerate (albums ):
176- add_playlist_album_index (
177- playlist_id = playlist .id , album_id = album .id , index = index
178- )
179-
180- return make_response ("Playlist album indexes populated" , 201 )
181-
182- @database_controller .route ("populate_track_artists" , methods = ["GET" ])
183- def populate_track_artists ():
184- access_token = request .cookies .get ("spotify_access_token" )
185- user = spotify .get_current_user (access_token )
186- albums = get_user_albums (user .id )
187- offset = 1800
188- for index , album in enumerate (albums [offset :]):
189- if all_tracks_have_artists (album .id ):
190- continue
191- sleep (0.5 )
192- print (index + offset )
193- album_tracks = spotify .get_album (
194- access_token = access_token , id = album .id
195- ).tracks .items
196- for track in album_tracks :
197- create_track_or_none (track , album )
198-
199- return make_response ("User album genres populated" , 201 )
200-
20187 return database_controller
20288
20389
0 commit comments