Skip to content

Commit f81e1b2

Browse files
committed
Refactor playlist_api
Clean up and refcator some duplicated code
1 parent b89fb87 commit f81e1b2

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

Diff for: listenbrainz/webserver/views/playlist_api.py

+25-27
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
MAX_RECORDINGS_PER_ADD = 100
3535
DEFAULT_NUMBER_OF_PLAYLISTS_PER_CALL = 25
3636

37+
supported_services = {
38+
"spotify": SpotifyService,
39+
"apple_music": AppleService,
40+
"soundcloud": SoundCloudService
41+
}
3742

3843
def validate_create_playlist_required_items(jspf):
3944
"""Given a JSPF dict, ensure that the title and public fields are present.
@@ -870,18 +875,18 @@ def export_playlist(playlist_mbid, service):
870875
if not is_valid_uuid(playlist_mbid):
871876
log_raise_400("Provided playlist ID is invalid.")
872877

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]()
875883

876884
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)
879886
elif service == "apple_music":
880-
apple_service = AppleService()
881-
token = apple_service.get_user(user["id"])
887+
token = service_class.get_user(user["id"])
882888
elif service == "soundcloud":
883-
soundcloud_service = SoundCloudService()
884-
token = soundcloud_service.get_user(user["id"])
889+
token = service_class.get_user(user["id"])
885890

886891
if not token:
887892
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):
923928
:resheader Content-Type: *application/json*
924929
"""
925930
user = validate_auth_header()
926-
supported_services = {
927-
"spotify": SpotifyService,
928-
"apple_music": AppleService,
929-
"soundcloud": SoundCloudService
930-
}
931931

932932
if service not in supported_services:
933933
raise APIBadRequest(f"Service {service} is not supported. "
@@ -939,11 +939,9 @@ def import_playlist_from_music_service(service):
939939
token = service_class.get_user(user["id"], refresh=True)
940940
elif service == "apple_music":
941941
# 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"])
944943
elif service == "soundcloud":
945-
soundcloud_service = SoundCloudService()
946-
token = soundcloud_service.get_user(user["id"])
944+
token = service_class.get_user(user["id"])
947945

948946
if not token:
949947
raise APIBadRequest(f"Service {service} is not linked. Please link your {service} account first.")
@@ -1093,23 +1091,23 @@ def export_playlist_jspf(service):
10931091
"""
10941092
user = validate_auth_header()
10951093

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+
10991100
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)
11021102
elif service == "apple_music":
1103-
apple_service = AppleService()
1104-
token = apple_service.get_user(user["id"])
1103+
token = service_class.get_user(user["id"])
11051104
elif service == "soundcloud":
1106-
soundcloud_service = SoundCloudService()
1107-
token = soundcloud_service.get_user(user["id"])
1105+
token = service_class.get_user(user["id"])
11081106

11091107
if not token:
11101108
raise APIBadRequest(f"Service {service} is not linked. Please link your {service} account first.")
11111109

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"])):
11131111
raise APIBadRequest(f"Missing scopes playlist-modify-public and playlist-modify-private to export playlists."
11141112
f" Please relink your {service} account from ListenBrainz settings with appropriate scopes"
11151113
f" to use this feature.")

0 commit comments

Comments
 (0)