Skip to content

Commit

Permalink
Refactor 3rd party API instantiation out of services
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyssion committed Aug 14, 2024
1 parent b8d1bfc commit 0e45a4a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
18 changes: 14 additions & 4 deletions playlist_sync/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
2 changes: 1 addition & 1 deletion playlist_sync/services/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions playlist_sync/services/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))',
Expand Down
15 changes: 6 additions & 9 deletions playlist_sync/services/ytmusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}')
Expand Down

0 comments on commit 0e45a4a

Please sign in to comment.