-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
196 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# SPDX-FileCopyrightText: 2024-present Fyssion <fyssioncodes@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
import argparse | ||
import logging | ||
|
||
from rich.logging import RichHandler | ||
|
||
from . import spotify_to_yt | ||
from . import yt_to_spotify | ||
|
||
|
||
log = logging.getLogger('playlist_sync') | ||
|
||
logging.getLogger().setLevel(logging.INFO) | ||
sh = RichHandler(show_time=False) | ||
sh.setFormatter(logging.Formatter("[%(name)s] %(message)s")) | ||
logging.getLogger().addHandler(sh) | ||
|
||
|
||
def main(): | ||
parser = argparse.ArgumentParser(prog='playlist_sync', description='Sync playlists between streaming services') | ||
parser.set_defaults(callback=None) | ||
parsers = parser.add_subparsers(title='subcommands') | ||
|
||
spotify_to_yt.initialize(parsers) | ||
yt_to_spotify.initialize(parsers) | ||
|
||
args = parser.parse_args() | ||
|
||
if args.callback: | ||
args.callback(args) | ||
else: | ||
parser.print_help() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# SPDX-FileCopyrightText: 2024-present Fyssion <fyssioncodes@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
|
||
import argparse | ||
|
||
from playlist_sync.playlist import Playlist | ||
|
||
from .utils import add_spotify_auth_to_parser, initialize_spotify, initialize_ytmusic, resolve_spotify_auth_from_args | ||
|
||
|
||
def main(args: argparse.Namespace): | ||
resolve_spotify_auth_from_args(args) | ||
|
||
sp = initialize_spotify(args.client_id, args.client_secret) | ||
yt = initialize_ytmusic() | ||
|
||
playlist = Playlist.fetch_from(sp, args._from) | ||
playlist.sync_to(yt, args.to) | ||
|
||
|
||
def initialize(parsers: argparse._SubParsersAction): | ||
parser = parsers.add_parser( | ||
name='spotify-to-yt', | ||
description='Sync a playlist from Spotify to YT Music', | ||
) | ||
|
||
parser.set_defaults(callback=main) | ||
parser.add_argument('-f', '--from', required=True, help='spotify url of the playlist to sync from', metavar='FROM_URL', dest='_from') | ||
parser.add_argument('-t', '--to', required=True, help='yt playlist id of the playlist to sync to', metavar='TO_ID') | ||
add_spotify_auth_to_parser(parser) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# SPDX-FileCopyrightText: 2024-present Fyssion <fyssioncodes@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
|
||
import argparse | ||
import importlib | ||
import logging | ||
import pathlib | ||
import sys | ||
|
||
import spotipy | ||
import ytmusicapi | ||
from spotipy.oauth2 import SpotifyOAuth | ||
|
||
from playlist_sync.services.spotify import Spotify | ||
from playlist_sync.services.ytmusic import YTMusic | ||
|
||
|
||
log = logging.getLogger('playlist_sync') | ||
|
||
|
||
def initialize_spotify(client_id: str, client_secret: str) -> Spotify: | ||
|
||
auth_manager = SpotifyOAuth( | ||
client_id=client_id, | ||
client_secret=client_secret, | ||
open_browser=False, | ||
redirect_uri='http://localhost:5000/', | ||
scope='playlist-read-private,playlist-read-collaborative', | ||
) | ||
return Spotify(spotipy.Spotify(auth_manager=auth_manager)) | ||
|
||
|
||
def initialize_ytmusic() -> YTMusic: | ||
if not pathlib.Path('./browser.json').is_file(): | ||
ytmusicapi.setup(filepath='browser.json') | ||
|
||
return YTMusic(ytmusicapi.YTMusic('browser.json')) | ||
|
||
|
||
def add_spotify_auth_to_parser(parser: argparse.ArgumentParser): | ||
parser.add_argument('--client-id', help='Spotify Client ID') | ||
parser.add_argument('--client-secret', help='Spotify Client secret') | ||
|
||
|
||
def resolve_spotify_auth_from_args(args: argparse.Namespace): | ||
if args.client_id is None or args.client_secret is None: | ||
# check for a config.py | ||
try: | ||
config = importlib.import_module('config') | ||
except ImportError: | ||
log.error('''Could not find Spotify client ID and secret. | ||
Either provide them as arguments in the CLI or in a config.py file''') | ||
sys.exit(1) | ||
|
||
args.client_id = config.spotify_client_id | ||
args.client_secret = config.spotify_client_secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# SPDX-FileCopyrightText: 2024-present Fyssion <fyssioncodes@gmail.com> | ||
# | ||
# SPDX-License-Identifier: MIT | ||
|
||
from __future__ import annotations | ||
|
||
import argparse | ||
|
||
from playlist_sync.playlist import Playlist | ||
|
||
from .utils import add_spotify_auth_to_parser , initialize_spotify, initialize_ytmusic, resolve_spotify_auth_from_args | ||
|
||
|
||
def main(args: argparse.Namespace): | ||
resolve_spotify_auth_from_args(args) | ||
|
||
sp = initialize_spotify(args.client_id, args.client_secret) | ||
yt = initialize_ytmusic() | ||
|
||
playlist = Playlist.fetch_from(yt, args._from) | ||
playlist.sync_to(sp, args.to) | ||
|
||
|
||
def initialize(parsers: argparse._SubParsersAction): | ||
parser = parsers.add_parser( | ||
name='yt-to-spotify', | ||
description='Sync a playlist from YT Music to Spotify', | ||
) | ||
|
||
parser.set_defaults(callback=main) | ||
parser.add_argument('-f', '--from', required=True, help='yt playlist id of the playlist to sync from', dest='_from') | ||
parser.add_argument('-t', '--to', required=True, help='spotify url of the playlist to sync to') | ||
add_spotify_auth_to_parser(parser) |