Skip to content

Commit

Permalink
implement browser availability check
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Oct 23, 2023
1 parent 42690d8 commit f78806e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion spotify_to_ytmusic/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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()


Expand Down
2 changes: 2 additions & 0 deletions spotify_to_ytmusic/spotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Empty file.
10 changes: 10 additions & 0 deletions spotify_to_ytmusic/utils/browser.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import webbrowser


def has_browser() -> bool:
try:
webbrowser.get()
return True

except webbrowser.Error:
return False
File renamed without changes.
2 changes: 1 addition & 1 deletion spotify_to_ytmusic/ytmusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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),
):
Expand Down

0 comments on commit f78806e

Please sign in to comment.