From f78806e809b71d25a846af2dd361b16abedead6d Mon Sep 17 00:00:00 2001 From: sigma67 Date: Mon, 23 Oct 2023 19:43:03 +0200 Subject: [PATCH] implement browser availability check --- spotify_to_ytmusic/setup.py | 4 +++- spotify_to_ytmusic/spotify.py | 2 ++ spotify_to_ytmusic/utils/__init__.py | 0 spotify_to_ytmusic/utils/browser.py | 10 ++++++++++ spotify_to_ytmusic/{ => utils}/match.py | 0 spotify_to_ytmusic/ytmusic.py | 2 +- tests/test_cli.py | 1 + 7 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 spotify_to_ytmusic/utils/__init__.py create mode 100644 spotify_to_ytmusic/utils/browser.py rename spotify_to_ytmusic/{ => utils}/match.py (100%) diff --git a/spotify_to_ytmusic/setup.py b/spotify_to_ytmusic/setup.py index 1f0896e..86dffb1 100644 --- a/spotify_to_ytmusic/setup.py +++ b/spotify_to_ytmusic/setup.py @@ -7,6 +7,7 @@ import ytmusicapi from spotify_to_ytmusic.settings import DEFAULT_PATH, EXAMPLE_PATH, Settings +from spotify_to_ytmusic.utils.browser import has_browser def setup(file: Optional[Path] = None): @@ -32,7 +33,8 @@ def setup(file: Optional[Path] = None): def setup_youtube(): settings = Settings() - settings["youtube"]["headers"] = json.dumps(ytmusicapi.setup_oauth()) + credentials = ytmusicapi.setup_oauth(open_browser=has_browser()) + settings["youtube"]["headers"] = json.dumps(credentials) settings.save() diff --git a/spotify_to_ytmusic/spotify.py b/spotify_to_ytmusic/spotify.py index 1657e5e..06de0ee 100644 --- a/spotify_to_ytmusic/spotify.py +++ b/spotify_to_ytmusic/spotify.py @@ -7,6 +7,7 @@ from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth from spotify_to_ytmusic.settings import CACHE_DIR, Settings +from spotify_to_ytmusic.utils.browser import has_browser class Spotify: @@ -33,6 +34,7 @@ def __init__(self): redirect_uri="http://localhost", scope="user-library-read", cache_handler=cache_handler, + open_browser=has_browser(), ) self.api = spotipy.Spotify(auth_manager=auth) else: diff --git a/spotify_to_ytmusic/utils/__init__.py b/spotify_to_ytmusic/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/spotify_to_ytmusic/utils/browser.py b/spotify_to_ytmusic/utils/browser.py new file mode 100644 index 0000000..ee58427 --- /dev/null +++ b/spotify_to_ytmusic/utils/browser.py @@ -0,0 +1,10 @@ +import webbrowser + + +def has_browser() -> bool: + try: + webbrowser.get() + return True + + except webbrowser.Error: + return False diff --git a/spotify_to_ytmusic/match.py b/spotify_to_ytmusic/utils/match.py similarity index 100% rename from spotify_to_ytmusic/match.py rename to spotify_to_ytmusic/utils/match.py diff --git a/spotify_to_ytmusic/ytmusic.py b/spotify_to_ytmusic/ytmusic.py index ccd6d5c..a8ba645 100644 --- a/spotify_to_ytmusic/ytmusic.py +++ b/spotify_to_ytmusic/ytmusic.py @@ -4,7 +4,7 @@ from ytmusicapi import YTMusic -from spotify_to_ytmusic.match import get_best_fit_song_id +from spotify_to_ytmusic.utils.match import get_best_fit_song_id from spotify_to_ytmusic.settings import Settings path = os.path.dirname(os.path.realpath(__file__)) + os.sep diff --git a/tests/test_cli.py b/tests/test_cli.py index a7a8498..d021e84 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -66,6 +66,7 @@ def test_setup(self): return_value=json.loads(Settings()["youtube"]["headers"]), ), mock.patch.object(setup, "DEFAULT_PATH", tmp_path), + mock.patch("spotify_to_ytmusic.setup.has_browser", return_value=False), mock.patch.object(settings_module, "DEFAULT_PATH", tmp_path), mock.patch.object(Settings, "filepath", tmp_path), ):