Skip to content

Commit

Permalink
fix: Ask for api re-authorization on playlist creation if using browser
Browse files Browse the repository at this point in the history
  • Loading branch information
rpop0 committed Nov 16, 2024
1 parent 0ab8e13 commit cc7e631
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions spotify_to_ytmusic/settings.ini.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[youtube]
headers = headers_json_from_browser
user_id =
auth_type = browser

[spotify]
client_id = id_from_developer_console
Expand Down
1 change: 1 addition & 0 deletions spotify_to_ytmusic/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ def __setitem__(self, section, key, value):
def save(self):
with open(self.filepath, "w") as f:
self.config.write(f)
self.config.read(self.filepath)
4 changes: 3 additions & 1 deletion spotify_to_ytmusic/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,16 @@ def setup_youtube():
settings = Settings()
credentials = ytmusicapi.setup_oauth(open_browser=has_browser())
settings["youtube"]["headers"] = json.dumps(credentials.as_dict())
settings["youtube"]["auth_type"] = "oauth"
settings.save()

def setup_youtube_browser():
settings = Settings()
print('Please see https://ytmusicapi.readthedocs.io/en/stable/setup/browser.html for instructions.')
credentials = ytmusicapi.setup()
settings["youtube"]["headers"] = credentials
settings.save
settings["youtube"]["auth_type"] = "browser"
settings.save()


def setup_spotify():
Expand Down
11 changes: 8 additions & 3 deletions spotify_to_ytmusic/ytmusic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@

from spotify_to_ytmusic.utils.match import get_best_fit_song_id
from spotify_to_ytmusic.settings import Settings
from spotify_to_ytmusic.setup import setup_youtube_browser

path = os.path.dirname(os.path.realpath(__file__)) + os.sep


class YTMusicTransfer:
def __init__(self):
settings = Settings()
headers = settings["youtube"]["headers"]
self.settings = Settings()
headers = self.settings["youtube"]["headers"]
assert headers.startswith("{"), "ytmusicapi headers not set or invalid"
self.api = YTMusic(headers, settings["youtube"]["user_id"])
self.api = YTMusic(headers, self.settings["youtube"]["user_id"])

def create_playlist(self, name, info, privacy="PRIVATE", tracks=None):
if self.settings["youtube"]["auth_type"] == "browser":
setup_youtube_browser()
self.api = YTMusic(self.settings["youtube"]["headers"], self.settings["youtube"]["user_id"])

return self.api.create_playlist(name, info, privacy, video_ids=tracks)

def rate_song(self, id, rating):
Expand Down

0 comments on commit cc7e631

Please sign in to comment.