-
Notifications
You must be signed in to change notification settings - Fork 75
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
5 changed files
with
128 additions
and
98 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,31 @@ | ||
import sys | ||
import settings | ||
|
||
if __name__ == "__main__": | ||
if sys.argv[1] == "youtube": | ||
from ytmusicapi import YTMusic | ||
api = YTMusic() | ||
settings['youtube']['headers'] = api.setup() | ||
settings.save() | ||
|
||
from settings import Settings | ||
import ytmusicapi | ||
|
||
settings = Settings() | ||
|
||
def setup(): | ||
choice = input("Choose which API to set up\n" | ||
"(1) Spotify\n" | ||
"(2) YouTube\n" | ||
"(3) both") | ||
choices = ["1","2","3"] | ||
if choice not in choices: | ||
sys.exit("Invalid choice") | ||
|
||
if choice == choices[0]: | ||
setup_spotify() | ||
elif choice == choices[1]: | ||
setup_youtube() | ||
else: | ||
setup_spotify() | ||
setup_youtube() | ||
|
||
def setup_youtube(): | ||
settings['youtube']['headers'] = ytmusicapi.setup_oauth() | ||
settings.save() | ||
|
||
def setup_spotify(): | ||
pass | ||
#settings['spotipy'] |
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,82 @@ | ||
import argparse | ||
import sys | ||
from datetime import datetime | ||
|
||
from spotify_to_ytmusic.Setup import setup | ||
from spotify_to_ytmusic.SpotifyExport import Spotify | ||
from spotify_to_ytmusic.YouTube import YTMusicTransfer | ||
|
||
|
||
def get_args(): | ||
parser = argparse.ArgumentParser(description='Transfer spotify playlist to YouTube Music.') | ||
parser.add_argument("playlist", type=str, help="Provide a playlist Spotify link.") | ||
parser.add_argument("-u", "--update", action='store_true', help="Delete all entries in the provided Google Play Music playlist and update the playlist with entries from the Spotify playlist.") | ||
parser.add_argument("-n", "--name", type=str, help="Provide a name for the YouTube Music playlist. Default: Spotify playlist name") | ||
parser.add_argument("-i", "--info", type=str, help="Provide description information for the YouTube Music Playlist. Default: Spotify playlist description") | ||
parser.add_argument("-d", "--date", action='store_true', help="Append the current date to the playlist name") | ||
parser.add_argument("-p", "--public", action='store_true', help="Make the playlist public. Default: private") | ||
parser.add_argument("-r", "--remove", action='store_true', help="Remove playlists with specified regex pattern.") | ||
parser.add_argument("-a", "--all", action='store_true', help="Transfer all public playlists of the specified user (Spotify User ID).") | ||
parser.add_argument("--setup", help="Set up credentials") | ||
return parser.parse_args() | ||
|
||
|
||
def main(): | ||
args = get_args() | ||
ytmusic = YTMusicTransfer() | ||
|
||
if args.setup: | ||
setup() | ||
sys.exit() | ||
|
||
if args.all: | ||
s = Spotify() | ||
pl = s.getUserPlaylists(args.playlist) | ||
print(str(len(pl)) + " playlists found. Starting transfer...") | ||
count = 1 | ||
for p in pl: | ||
print("Playlist " + str(count) + ": " + p['name']) | ||
count = count + 1 | ||
try: | ||
playlist = Spotify().getSpotifyPlaylist(p['external_urls']['spotify']) | ||
videoIds = ytmusic.search_songs(playlist['tracks']) | ||
playlist_id = ytmusic.create_playlist(p['name'], p['description'], | ||
'PUBLIC' if args.public else 'PRIVATE', | ||
videoIds) | ||
print(playlist_id) | ||
except Exception as ex: | ||
print("Could not transfer playlist " + p['name'] + ". Exception" + str(ex)) | ||
return | ||
|
||
if args.remove: | ||
ytmusic.remove_playlists(args.playlist) | ||
return | ||
|
||
date = "" | ||
if args.date: | ||
date = " " + datetime.today().strftime('%m/%d/%Y') | ||
try: | ||
playlist = Spotify().getSpotifyPlaylist(args.playlist) | ||
except Exception as ex: | ||
print("Could not get Spotify playlist. Please check the playlist link.\n Error: " + repr(ex)) | ||
return | ||
|
||
name = args.name + date if args.name else playlist['name'] + date | ||
info = playlist['description'] if (args.info is None) else args.info | ||
|
||
if args.update: | ||
playlistId = ytmusic.get_playlist_id(name) | ||
videoIds = ytmusic.search_songs(playlist['tracks']) | ||
ytmusic.remove_songs(playlistId) | ||
ytmusic.add_playlist_items(playlistId, videoIds) | ||
|
||
else: | ||
videoIds = ytmusic.search_songs(playlist['tracks']) | ||
playlistId = ytmusic.create_playlist(name, info, 'PUBLIC' if args.public else 'PRIVATE', videoIds) | ||
|
||
print("Success: created playlist \"" + name + "\"\n" + | ||
"https://music.youtube.com/playlist?list=" + playlistId) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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 |
---|---|---|
@@ -1,25 +1,22 @@ | ||
import configparser | ||
import sys | ||
import types | ||
import os | ||
from pathlib import Path | ||
from typing import Optional | ||
|
||
config = configparser.ConfigParser(interpolation=None) | ||
filepath = os.path.dirname(os.path.realpath(__file__)) + '/settings.ini' | ||
config.read(filepath) | ||
|
||
class Settings: | ||
|
||
class Settings(types.ModuleType): | ||
config: configparser.ConfigParser | ||
def __init__(self, filepath: Optional[Path] = None): | ||
self.config = configparser.ConfigParser(interpolation=None) | ||
self.filepath = filepath if filepath else Path(__file__).parent.joinpath('settings.ini') | ||
self.config.read(self.filepath) | ||
|
||
def __getitem__(self, key): | ||
return config[key] | ||
return self.config[key] | ||
|
||
def __setitem__(self, section, key, value): | ||
config.set(section, key, value) | ||
self.config.set(section, key, value) | ||
|
||
def save(self): | ||
with open(filepath, 'w') as f: | ||
config.write(f) | ||
|
||
|
||
|
||
sys.modules[__name__] = Settings("settings") | ||
with open(self.filepath, 'w') as f: | ||
self.config.write(f) |