From 0e45a4aca9f31ea796349196f811b79c0b4827e4 Mon Sep 17 00:00:00 2001 From: Fyssion Date: Wed, 14 Aug 2024 01:19:05 -0500 Subject: [PATCH] Refactor 3rd party API instantiation out of services --- playlist_sync/__main__.py | 18 ++++++++++++++---- playlist_sync/services/base.py | 2 +- playlist_sync/services/spotify.py | 16 ++++++---------- playlist_sync/services/ytmusic.py | 15 ++++++--------- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/playlist_sync/__main__.py b/playlist_sync/__main__.py index ea99be7..953ab20 100644 --- a/playlist_sync/__main__.py +++ b/playlist_sync/__main__.py @@ -7,7 +7,10 @@ import logging import pathlib +import spotipy +import ytmusicapi from rich.logging import RichHandler +from spotipy.oauth2 import SpotifyOAuth import config from playlist_sync.playlist import Playlist @@ -22,10 +25,17 @@ logging.getLogger().addHandler(sh) if not pathlib.Path('./browser.json').is_file(): - YTMusic.setup() - -yt = YTMusic('browser.json') -sp = Spotify(client_id=config.spotify_client_id, client_secret=config.spotify_client_secret) + ytmusicapi.setup(filepath='browser.json') + +auth_manager = SpotifyOAuth( + client_id=config.spotify_client_id, + client_secret=config.spotify_client_secret, + open_browser=False, + redirect_uri='http://localhost:5000/', + scope='playlist-read-private,playlist-read-collaborative', +) +yt = YTMusic(ytmusicapi.YTMusic('browser.json')) +sp = Spotify(spotipy.Spotify(auth_manager=auth_manager)) playlist = Playlist.fetch_from(sp, config.sync_from_url) playlist.sync_to(yt, config.sync_to_id) diff --git a/playlist_sync/services/base.py b/playlist_sync/services/base.py index b11a6be..82d1a17 100644 --- a/playlist_sync/services/base.py +++ b/playlist_sync/services/base.py @@ -4,7 +4,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Protocol, runtime_checkable +from typing import TYPE_CHECKING, Any, Protocol, runtime_checkable if TYPE_CHECKING: from playlist_sync.track import Track diff --git a/playlist_sync/services/spotify.py b/playlist_sync/services/spotify.py index f95918a..3b61535 100644 --- a/playlist_sync/services/spotify.py +++ b/playlist_sync/services/spotify.py @@ -5,24 +5,20 @@ from __future__ import annotations import logging -from typing import Any - -import spotipy -from spotipy.oauth2 import SpotifyOAuth +from typing import TYPE_CHECKING, Any from playlist_sync.services.base import BaseService from playlist_sync.track import Track +if TYPE_CHECKING: + import spotipy + log = logging.getLogger('playlist_sync.services.spotify') class Spotify(BaseService): - def __init__(self, *args, **kwargs): - kwargs.setdefault('scope', 'playlist-read-private,playlist-read-collaborative') - kwargs.setdefault('redirect_uri', 'http://localhost:5000/') - kwargs.setdefault('open_browser', False) - auth_manager = SpotifyOAuth(*args, **kwargs) - self.api = spotipy.Spotify(auth_manager=auth_manager) + def __init__(self, api: spotipy.Spotify): + self.api = api FETCH_PLAYLIST_KWARGS: dict[str, Any] = dict( fields='total,limit,items(track(name))', diff --git a/playlist_sync/services/ytmusic.py b/playlist_sync/services/ytmusic.py index 2348342..9e6dcee 100644 --- a/playlist_sync/services/ytmusic.py +++ b/playlist_sync/services/ytmusic.py @@ -5,23 +5,20 @@ from __future__ import annotations import logging - -import ytmusicapi -from ytmusicapi import YTMusic as YTMusicAPI +from typing import TYPE_CHECKING from playlist_sync.services.base import BaseService from playlist_sync.track import Track +if TYPE_CHECKING: + import ytmusicapi + log = logging.getLogger('playlist_sync.services.ytmusic') class YTMusic(BaseService): - def __init__(self, *args, **kwargs): - self.api = YTMusicAPI(*args, **kwargs) - - @staticmethod - def setup(): - ytmusicapi.setup(filepath='browser.json') + def __init__(self, api: ytmusicapi.YTMusic): + self.api = api def fetch_playlist(self, url: str) -> list[Track]: log.info(f'Fetching playlist {url}')