34
34
MAX_RECORDINGS_PER_ADD = 100
35
35
DEFAULT_NUMBER_OF_PLAYLISTS_PER_CALL = 25
36
36
37
+ supported_services = {
38
+ "spotify" : SpotifyService ,
39
+ "apple_music" : AppleService ,
40
+ "soundcloud" : SoundCloudService
41
+ }
37
42
38
43
def validate_create_playlist_required_items (jspf ):
39
44
"""Given a JSPF dict, ensure that the title and public fields are present.
@@ -870,18 +875,18 @@ def export_playlist(playlist_mbid, service):
870
875
if not is_valid_uuid (playlist_mbid ):
871
876
log_raise_400 ("Provided playlist ID is invalid." )
872
877
873
- if service != "spotify" and service != "apple_music" and service != "soundcloud" :
874
- raise APIBadRequest (f"Service { service } is not supported. We currently only support 'spotify', 'apple_music' and 'soundcloud'." )
878
+ if service not in supported_services :
879
+ raise APIBadRequest (f"Service { service } is not supported. "
880
+ f"Supported services are: 'spotify', 'apple_music', 'soundcloud'." )
881
+
882
+ service_class = supported_services [service ]()
875
883
876
884
if service == "spotify" :
877
- spotify_service = SpotifyService ()
878
- token = spotify_service .get_user (user ["id" ], refresh = True )
885
+ token = service_class .get_user (user ["id" ], refresh = True )
879
886
elif service == "apple_music" :
880
- apple_service = AppleService ()
881
- token = apple_service .get_user (user ["id" ])
887
+ token = service_class .get_user (user ["id" ])
882
888
elif service == "soundcloud" :
883
- soundcloud_service = SoundCloudService ()
884
- token = soundcloud_service .get_user (user ["id" ])
889
+ token = service_class .get_user (user ["id" ])
885
890
886
891
if not token :
887
892
raise APIBadRequest (f"Service { service } is not linked. Please link your { service } account first." )
@@ -923,11 +928,6 @@ def import_playlist_from_music_service(service):
923
928
:resheader Content-Type: *application/json*
924
929
"""
925
930
user = validate_auth_header ()
926
- supported_services = {
927
- "spotify" : SpotifyService ,
928
- "apple_music" : AppleService ,
929
- "soundcloud" : SoundCloudService
930
- }
931
931
932
932
if service not in supported_services :
933
933
raise APIBadRequest (f"Service { service } is not supported. "
@@ -939,11 +939,9 @@ def import_playlist_from_music_service(service):
939
939
token = service_class .get_user (user ["id" ], refresh = True )
940
940
elif service == "apple_music" :
941
941
# TODO: implement refresh token for AppleMusic
942
- apple_service = AppleService ()
943
- token = apple_service .get_user (user ["id" ])
942
+ token = service_class .get_user (user ["id" ])
944
943
elif service == "soundcloud" :
945
- soundcloud_service = SoundCloudService ()
946
- token = soundcloud_service .get_user (user ["id" ])
944
+ token = service_class .get_user (user ["id" ])
947
945
948
946
if not token :
949
947
raise APIBadRequest (f"Service { service } is not linked. Please link your { service } account first." )
@@ -1093,23 +1091,23 @@ def export_playlist_jspf(service):
1093
1091
"""
1094
1092
user = validate_auth_header ()
1095
1093
1096
- if service != "spotify" and service != "apple_music" and service != "soundcloud" :
1097
- raise APIBadRequest (f"Service { service } is not supported. We currently only support 'spotify', 'apple_music' and 'soundcloud'." )
1098
-
1094
+ if service not in supported_services :
1095
+ raise APIBadRequest (f"Service { service } is not supported. "
1096
+ f"Supported services are: 'spotify', 'apple_music', 'soundcloud'." )
1097
+
1098
+ service_class = supported_services [service ]()
1099
+
1099
1100
if service == "spotify" :
1100
- spotify_service = SpotifyService ()
1101
- token = spotify_service .get_user (user ["id" ], refresh = True )
1101
+ token = service_class .get_user (user ["id" ], refresh = True )
1102
1102
elif service == "apple_music" :
1103
- apple_service = AppleService ()
1104
- token = apple_service .get_user (user ["id" ])
1103
+ token = service_class .get_user (user ["id" ])
1105
1104
elif service == "soundcloud" :
1106
- soundcloud_service = SoundCloudService ()
1107
- token = soundcloud_service .get_user (user ["id" ])
1105
+ token = service_class .get_user (user ["id" ])
1108
1106
1109
1107
if not token :
1110
1108
raise APIBadRequest (f"Service { service } is not linked. Please link your { service } account first." )
1111
1109
1112
- if service == 'spotify' and not SPOTIFY_PLAYLIST_PERMISSIONS .issubset (set (token ["scopes" ])):
1110
+ if service == 'spotify' and not SPOTIFY_PLAYLIST_PERMISSIONS .issubset (set (token ["scopes" ])):
1113
1111
raise APIBadRequest (f"Missing scopes playlist-modify-public and playlist-modify-private to export playlists."
1114
1112
f" Please relink your { service } account from ListenBrainz settings with appropriate scopes"
1115
1113
f" to use this feature." )
0 commit comments