def createYTPlaylist(playlistname, description, listSongs): #Parameters: YTMusic playlist name, description, and the list of songs to be uploaded to the playlist #Returns: A YTMusic playlist with the designated name/description, with the list of songs in the playlist ytmusic = YTMusic('headers_auth.json') failed = [] playlistID = ytmusic.create_playlist(playlistname, description) printProgressBar(0, len(listSongs), prefix='uploading songs...:', suffix='Complete', length=50) i = 0 for song in listSongs: try: printProgressBar(i + 1, len(listSongs), prefix='uploading songs...:', suffix='Complete', length=50) search_results = ytmusic.search(song, "songs") ytmusic.add_playlist_items(playlistID, [search_results[0]['videoId']]) i += 1 except Exception as e: failed.append("failed on song: " + song + " from err. " + str(e)) print("completed")
class ytmUploader: """class to use ytmusicapi (https://github.com/sigma67/ytmusicapi) to upload spotifyplaylists""" def __init__(self, yt_channel_id, auth_filepath='headers_auth.json'): self.ytmusic = YTMusic(auth_filepath) self.yt_channel_id = yt_channel_id def uploadSpotifyPlaylist(self, spotifyPL): plTitle = spotifyPL.get('playlistName') print(f"Retrieving songs for album {plTitle}") formerPlaylistID = self.getPlaylistID(plTitle) # convert Spotify songs to ytmusic video ids # Has to be done regardless, to update if exists videoIDs = self.ytVideoIDs(spotifyPL) if formerPlaylistID: # Delete first print(f"Deleting: {plTitle}") self.ytmusic.delete_playlist(formerPlaylistID) # Create playlist print(f"Creating: {plTitle}") plID = self.ytmusic.create_playlist(title=plTitle, description=f"{plTitle} from Spotify - magic from playmaker script", privacy_status="PUBLIC", video_ids=list(videoIDs)) def ytVideoIDs(self, spotifyPL): ids = [] """ plTitle = spotifyPL.get('playlistName') if plTitle == 'RapCaviar': return rapCaviarIDs elif plTitle == 'Most Necessary': return mostNecessaryIDs elif plTitle == 'Get Turnt': return getTurntIDs """ for song in spotifyPL.get('tracks'): songname = song[0] ytSongs = self.ytmusic.search(songname, "songs") if ytSongs: topMatchingSong = ytSongs[0] ids.append(topMatchingSong.get('videoId')) return ids def getPlaylistID(self, plTitle): userInfo = self.ytmusic.get_user(self.yt_channel_id) if 'playlists' in userInfo: for song in userInfo.get('playlists').get('results'): if song["title"] == plTitle: return song["playlistId"] return ""
def export_song_data(name, data, dry_run): if not os.path.exists(AUTH_FILE): YTMusic.setup(filepath=AUTH_FILE) ytmusic = YTMusic(AUTH_FILE) song_ids = data_to_song_ids(ytmusic, data) print("Found %d/%d tracks on YouTube Music." % (len(song_ids), len(data))) if song_ids and not dry_run: playlist_id = ytmusic.create_playlist(name, "Auto-generated playlist") ytmusic.add_playlist_items(playlist_id, song_ids) print("Created playlist \"%s\"." % name)
def main(): print('Initializing...') playlist_name = datetime.now().strftime("%A, %d. %B %Y %I:%M%p") ytmusic = YTMusic('headers_auth.json') print('Retrieving songs...') songs = ytmusic.get_library_songs(limit=10000) song_ids = list(map(lambda x: x['videoId'], songs)) print('Creating playlist...') playlist = ytmusic.create_playlist(playlist_name, 'Automatically created playlist') print('Adding songs to playlist...') ytmusic.add_playlist_items(playlist, song_ids) print('Done.')
def playlist(self, title, path, description="Created by yootto", encoding="utf_8", enable_reload_online_cache=False): """ Upload playlist file(.m3u, .m3u8). :param title: Playlist title :param path: A playlist file path :param description: Playlist description (default value: "Created by yootto") :param encoding: Charactor encoding using in playlist file (default value: "utf_8") :param enable_reload_online_cache: Reload songs catalog from YouTube Music. """ ytmusic = object() try: ytmusic = YTMusic(self.conf["auth_file_path"]) except Exception as identifier: return "Can not connect YouTube Music API: {}".format(identifier) cache = [] if enable_reload_online_cache: try: cache = ytmusic.get_library_upload_songs(100000) print( store_online_cache( self.conf["online_catalog_cache_file_path"], cache)) except Exception as identifier: return "Error: {}".format(identifier) else: cache = load_online_cache( self.conf["online_catalog_cache_file_path"]) playlist = load_playlist(path, encoding) if len(playlist) == 0: return "Can not read playlist or playlist dose not have tracks." print("Creating...") for s in cache: for p in playlist: if not compare_online_to_file(s, p): continue p.video_id = s["videoId"] video_ids = [] for p in playlist: if not hasattr(p, 'video_id') or p.video_id is None: print( "Error: '{}' is not found in YouTube Music or local cache". format(p.filename)) continue video_ids.append(p.video_id) res = ytmusic.create_playlist(title, description, "PRIVATE", video_ids) if type(res) is str: return "Playlist created: {}".format(title) return "Failed: {}".format(res)
def music(self, path="./", disable_create_playlist=False): """ Upload audio file. :param path: A audio file path or directory path including audio files. (default value: "./") :param disable_create_playlist: Disable auto create playlist function. """ ytmusic = object() try: ytmusic = YTMusic(self.conf["auth_file_path"]) except Exception as identifier: return "Can not connect YouTube Music API: {}".format(identifier) files = get_music_file(path) print("{} files found".format(len(files))) print("Start upload...") success_cnt, conflict_cnt, error_cnt = 0, 0, 0 tags = list() for f in tqdm(files): try: while True: ret = ytmusic.upload_song(f) if type( ret ) is requests.models.Response and ret.status_code == 409: print("Conflicted upload {}".format(f)) conflict_cnt += 1 elif type( ret ) is requests.models.Response and ret.status_code == 503: print("YouTube Music say 503...Susspending 15 sec...") time.sleep(15) continue elif type(ret) is str and ret == "STATUS_SUCCEEDED": tags.append(get_tag_from_file(f)) success_cnt += 1 else: print("Error upload {f} / {err}".format(f=f, err=ret)) error_cnt += 1 break except: print("Error upload {}".format(f)) error_cnt += 1 return "success: {suc} / fail: {err}".format(suc=success_cnt, err=error_cnt) all_len = len(tags) processed_cnt = 0 retry_cnt = 0 cache = load_online_cache(self.conf["online_catalog_cache_file_path"]) while len(tags) > processed_cnt: print("waiting... ({n}/{all})".format(n=processed_cnt, all=len(tags))) time.sleep(10) songs = ytmusic.get_library_upload_songs(all_len, "recently_added") last_cnt = processed_cnt for s in songs: for t in tags: if hasattr(t, "video_id"): continue if not compare_online_to_file(s, t): continue t.video_id = s["videoId"] try: cache.append(s) songs.remove(s) except Exception as identifier: logger.error("Upload error: %s", identifier) logger.error("Failed proccesing song: %s", s) processed_cnt += 1 if last_cnt == processed_cnt: retry_cnt += 1 else: retry_cnt = 0 if retry_cnt > 30: logger.error("Can not get meta data. Unprocessed data: %s", songs) break print( store_online_cache(self.conf["online_catalog_cache_file_path"], cache)) if not disable_create_playlist and len(tags) != 0: video_ids = list() for t in tags: if hasattr(t, "video_id"): video_ids.append(t.video_id) if len(video_ids) > 0: title = datetime.datetime.now().strftime( self.conf["auto_create_playlist_format"]) res = ytmusic.create_playlist(title, "Auto created by yootto", "PRIVATE", video_ids) if type(res) is str: print("Playlist created: {}".format(title)) return "success: {suc} / fail: {err}".format(suc=success_cnt, err=error_cnt)
class YTMusicTransfer: def __init__(self): self.api = YTMusic(settings['youtube']['headers'], settings['youtube']['user_id']) def create_playlist(self, name, info, privacy="PRIVATE", tracks=None): return self.api.create_playlist(name, info, privacy, video_ids=tracks) def get_best_fit_song_id(self, results, song): match_score = {} title_score = {} for res in results: if res['resultType'] not in ['song', 'video']: continue durationMatch = None if 'duration' in res and res['duration']: durationItems = res['duration'].split(':') duration = int(durationItems[0]) * 60 + int(durationItems[1]) durationMatch = 1 - abs( duration - song['duration']) * 2 / song['duration'] title = res['title'] # for videos, if res['resultType'] == 'video': titleSplit = title.split('-') if len(titleSplit) == 2: title = titleSplit[1] artists = ' '.join([a['name'] for a in res['artists']]) title_score[res['videoId']] = difflib.SequenceMatcher( a=title.lower(), b=song['name'].lower()).ratio() scores = [ title_score[res['videoId']], difflib.SequenceMatcher(a=artists.lower(), b=song['artist'].lower()).ratio() ] if durationMatch: scores.append(durationMatch * 5) #add album for songs only if res['resultType'] == 'song' and res['album'] is not None: scores.append( difflib.SequenceMatcher(a=res['album']['name'].lower(), b=song['album'].lower()).ratio()) match_score[res['videoId']] = sum(scores) / len(scores) * max( 1, int(res['resultType'] == 'song') * 1.5) if len(match_score) == 0: return None #don't return songs with titles <45% match max_score = max(match_score, key=match_score.get) return max_score def search_songs(self, tracks): videoIds = [] songs = list(tracks) notFound = list() for i, song in enumerate(songs): name = re.sub(r' \(feat.*\..+\)', '', song['name']) query = song['artist'] + ' ' + name query = query.replace(" &", "") result = self.api.search(query, ignore_spelling=True) if len(result) == 0: notFound.append(query) else: targetSong = self.get_best_fit_song_id(result, song) if targetSong is None: notFound.append(query) else: videoIds.append(targetSong) if i > 0 and i % 10 == 0: print(f"YouTube tracks: {i}/{len(songs)}") with open(path + 'noresults_youtube.txt', 'w', encoding="utf-8") as f: f.write("\n".join(notFound)) f.write("\n") f.close() return videoIds def add_playlist_items(self, playlistId, videoIds): videoIds = OrderedDict.fromkeys(videoIds) self.api.add_playlist_items(playlistId, videoIds) def get_playlist_id(self, name): pl = self.api.get_library_playlists(10000) try: playlist = next(x for x in pl if x['title'].find(name) != -1)['playlistId'] return playlist except: raise Exception("Playlist title not found in playlists") def remove_songs(self, playlistId): items = self.api.get_playlist(playlistId, 10000)['tracks'] if len(items) > 0: self.api.remove_playlist_items(playlistId, items) def remove_playlists(self, pattern): playlists = self.api.get_library_playlists(10000) p = re.compile("{0}".format(pattern)) matches = [pl for pl in playlists if p.match(pl['title'])] print("The following playlists will be removed:") print("\n".join([pl['title'] for pl in matches])) print("Please confirm (y/n):") choice = input().lower() if choice[:1] == 'y': [self.api.delete_playlist(pl['playlistId']) for pl in matches] print(str(len(matches)) + " playlists deleted.") else: print("Aborted. No playlists were deleted.")
class YTMusicTransfer: def __init__(self): self.api = YTMusic() def create_playlist(self, name, info, privacy="PRIVATE", tracks=None): return self.api.create_playlist(name, info, privacy, video_ids=tracks) def get_best_fit_song(self, results, song): match_score = {} title_score = {} for res in results: if res['resultType'] not in ['song', 'video']: continue durationMatch = None if res['duration']: durationItems = res['duration'].split(':') duration = int(durationItems[0]) * 60 + int(durationItems[1]) durationMatch = 1 - abs(duration - song['duration']) * 2 / song['duration'] title = res['title'] # for videos, if res['resultType'] == 'video': titleSplit = title.split('-') if len(titleSplit) == 2: title = titleSplit[1] artists = ' '.join([a['name'] for a in res['artists']]) title_score[res['videoId']] = difflib.SequenceMatcher(a=title.lower(), b=song['name'].lower()).ratio() scores = [title_score[res['videoId']], difflib.SequenceMatcher(a=artists.lower(), b=song['artist'].lower()).ratio()] if durationMatch: scores.append(durationMatch * 5) #add album for songs only if res['resultType'] == 'song' and res['album'] is not None: scores.append(difflib.SequenceMatcher(a=res['album']['name'].lower(), b=song['album'].lower()).ratio()) match_score[res['videoId']] = sum(scores) / (len(scores) + 1) * max(1, int(res['resultType'] == 'song') * 1.5) if len(match_score) == 0: return None #don't return songs with titles <45% match max_score = max(match_score, key=match_score.get) return [el for el in results if el['resultType'] in ['song', 'video'] and el['videoId'] == max_score][0] def search_songs(self, tracks): videos = [] songs = list(tracks) notFound = list() for i, song in enumerate(songs): query = song['artist'] + ' ' + song['name'] query = query.replace(" &", "") try: result = self.api.search(query) except: print(f'Fail for {song["artist"]} - {song["name"]}') if len(result) == 0: notFound.append(query) else: targetSong = self.get_best_fit_song(result, song) if targetSong is None: notFound.append(query) else: video = self.format_song(targetSong) videos.append(video) if i > 0 and i % 10 == 0: print(str(i) + ' searched') print(notFound) return videos def format_song(self, video): video['_id'] = video['videoId'] video['durationDisplay'] = video['duration'] if len(video['durationDisplay'].split(':')) == 3: video['duration'] = int(video['duration'].split(':')[0]) * 3600 + int(video['duration'].split(':')[1]) * 60 + int(video['duration'].split(':')[2]) if len(video['durationDisplay'].split(':')) == 2: video['duration'] = int(video['duration'].split(':')[0]) * 60 + int(video['duration'].split(':')[1]) video['thumbnail'] = video['thumbnails'][-1]['url'] video['artist'] = video['artists'][0]['name'] video['album'] = video['album']['name'] if 'album' in video and 'name' in video['album'] else None return video def add_playlist_items(self, playlistId, videoIds): videoIds = OrderedDict.fromkeys(videoIds) self.api.add_playlist_items(playlistId, videoIds) def get_playlist_id(self, name): pl = self.api.get_library_playlists(10000) try: playlist = next(x for x in pl if x['title'].find(name) != -1)['playlistId'] return playlist except: raise Exception("Playlist title not found in playlists") def remove_songs(self, playlistId): items = self.api.get_playlist(playlistId, 10000)['tracks'] if len(items) > 0: self.api.remove_playlist_items(playlistId, items) def remove_playlists(self, pattern): playlists = self.api.get_library_playlists(10000) p = re.compile("{0}".format(pattern)) matches = [pl for pl in playlists if p.match(pl['title'])] print("The following playlists will be removed:") print("\n".join([pl['title'] for pl in matches])) print("Please confirm (y/n):") choice = input().lower() if choice[:1] == 'y': [self.api.delete_playlist(pl['playlistId']) for pl in matches] print(str(len(matches)) + " playlists deleted.") else: print("Aborted. No playlists were deleted.")
import time ytmusic = YTMusic('./src/headers_auth.json') playlists = ytmusic.get_library_playlists() lib_playlists = {} playlist_ids = [] for i in playlists: playlist_ids.append(i['playlistId']) lib_playlists[i['title']] = i # print(playlist_ids) playlist = ytmusic.get_playlist(playlist_ids[2]) # print(playlist) new_playlist = ytmusic.create_playlist('Imported Songs', 'Youtube API Test!!') filename = "songs.csv" rows = [] # reading csv file with open(filename, 'r') as csvfile: # creating a csv reader object csvreader = csv.reader(csvfile) # extracting field names through first row fields = next(csvreader) # extracting each data row one by one for row in csvreader: rows.append(row[0])
"Accept": "*/*", "X-Requested-With": "XMLHttpRequest", "Referer": "http://wklq.tunegenie.com/onair/" + date + "/", "Accept-Encoding": "gzip, deflate", "Accept-Language": "en-US,en;q=0.9,fil;q=0.8" } r = requests.get(url, headers=headers) rJson = r.json() songList = rJson['response'] ytmusic = YTMusic("headers_auth.json") playlistTitle = "WKLQ Playlist from " + date playlistDescription = "" playlistPrivacyStatus = "PUBLIC" songsToAdd = [] for song in songList: searchTerm = (song['artist'] + " " + song['song']) songsToAdd.append(searchTerm) videoResults = GetSongVideoIds(ytmusic, songsToAdd) newPlaylist = ytmusic.create_playlist(playlistTitle, playlistDescription, playlistPrivacyStatus, videoResults['videoIds']) print("playlistId:", newPlaylist) print("Found", videoResults['searchCount'], "songs") print(videoResults['uniqueCount'], "songs were unique") print("YTMusic matched", videoResults['matchedCount'], "songs")
class SpotTube(): def __init__(self, clientId=None, clientSecret=None, redirectURI=None): self._clientId = clientId self._clientSecret = clientSecret self._redirectURI = redirectURI self._sp = None self.username = None scope = "playlist-modify-public playlist-modify-public playlist-read-private playlist-read-collaborative" try: access_token = util.prompt_for_user_token( client_id=self._clientId, client_secret=self._clientSecret, scope=scope, redirect_uri=self._redirectURI, cache_path=".cache") except Exception: print( "Não foi possível autenticar em Spotify, verifique se suas credenciais estão corretas.\n" ) sys.exit() else: self._sp = spotipy.Spotify(access_token) self.username = self._sp.me()["display_name"] print(f"Conectado em Spotify como {self.username}") print("\nAutenticando em Youtube Music...\n") sleep(3) try: self._yt = YTMusic("headers_auth.json") except Exception: print( "Não foi possível autenticar em Youtube Music, verifique se o arquivo headers.json está configurado corretamente\n" ) sys.exit() else: print("Conectado em Youtube Music\n") def main(self): userPlaylists = self.get_all_user_spotify_playlists() selected = self.select_playlist(userPlaylists) musics = self.get_musics_from_spotify_playlist(selected[0]) videoIds = self.search_on_youtube(musics) self.add_musics_on_youtube(videoIds, selected[1]) self.saveCredentials(client_id=self._clientId, client_secret=self._clientSecret, redirect_uri=self._redirectURI) def get_all_user_spotify_playlists(self): userPlaylists = {} playlists = self._sp.user_playlists(self._sp.me()["id"])["items"] print() for i in range(len(playlists)): if playlists[i]["owner"]["display_name"] == self.username: userPlaylists[playlists[i]["id"]] = playlists[i]["name"] return userPlaylists def select_playlist(self, playlists): choice = None options = [] name = [] print("Essas são as suas playlists que encontrei: \n") while True: for i, j in enumerate(playlists.items()): print(f"{i+1}ª playlist: {j[1]}") options.append(i + 1) name.append({"name": j[1], "id": j[0]}) try: choice = int(input("\nSelecione uma playlist: ")) if choice not in options: os.system("cls") print("Selecione uma playlist válida.\n") continue except ValueError: os.system("cls") print("Selecione uma playlist válida.\n") else: os.system("cls") playlistName = name[choice - 1]["name"] print("Voce selecionou " + playlistName) choice = name[choice - 1]["id"] del name del options break return (choice, playlistName) def get_musics_from_spotify_playlist(self, playlistId): tracks = [] songs = self._sp.playlist_tracks(playlistId)["items"] for i in range(len(songs)): tracks.append(songs[i]["track"]["name"]) if len(songs) >= 100: songs = self._sp.playlist_tracks(playlistId, offset=100 + 1)["items"] for i in range(len(songs)): tracks.append(songs[i]["track"]["name"]) return tracks return tracks def search_on_youtube(self, tracks): print("Buscando as músicas...\n") videoIds = {} for music in tracks: youtube = self._yt.search(query=music, filter="songs", ignore_spelling=True, limit=5)[0] videoIds[youtube["title"]] = youtube["videoId"] return videoIds def add_musics_on_youtube(self, ids, playlistName): names = [i for i in ids.keys()] all_ids = [i for i in ids.values()] [print(track) for track in names] print("\n") print( "Adicionando músicas em sua playlist do Yt Music.\nAguarde alguns instantes..." ) sleep(5) playlist = self._yt.create_playlist(title=playlistName, description="", privacy_status="PRIVATE") self._yt.add_playlist_items(playlistId=playlist, videoIds=all_ids) print("\nAs músicas foram adicionadas!") browser = input( "Pressione 1 para abrir no navegador ou pressione qualquer tecla para terminar: " ) if browser == '1': webbrowser.open( f"https://music.youtube.com/playlist?list={playlist}") def saveCredentials(self, client_id, client_secret, redirect_uri): with open(".env", "w") as file: file.write(f"clientID={client_id}\n") file.write(f"clientSecret={client_secret}\n") file.write(f"URI={redirect_uri}\n")
class youtube_music_tasker: def __init__(self, auth_json: str): self.api = YTMusic(auth_json) # Return: # [ # { # "id": "playlistid1", # "title": "playlist_title1", # "thumbnail": "url_to_playlist1_1st_thumbnail" # }, # { # "id": "playlistid2", # "title": "playlist_title2", # "thumbnail": "url_to_playlist2_1st_thumbnail" # } # ] # def show_playlist(self): list_of_playlist = [] try: library_playlists = self.api.get_library_playlists( limit=50) # Hopefully, no one has 50+ playlists. for pl in library_playlists: # Only showing non-empty well-formed playlists if 'count' in pl and int( pl['count'] ) > 0 and 'playlistId' in pl and 'title' in pl and 'thumbnails' in pl: playlist = {} playlist['id'] = pl['playlistId'] playlist['title'] = pl['title'] if len(pl['thumbnails']) > 0: playlist['thumbnail'] = pl['thumbnails'][0]['url'] else: playlist['thumbnail'] = DEFAULT_IMG_URL list_of_playlist.append(playlist) except Exception as e: print("Unexpected Error in show_playlist:", e) return json.dumps(list_of_playlist) # Return: # [ # { # "title": "name", # "artist": "someone", # "album": "the album" # }, # { # "title": "name", # "artist": "any", # "album": "any" # } # ] # def show_song_in_playlist(self, playlist_id: str): list_of_song = [] try: pl_detail = self.api.get_playlist(playlistId=playlist_id) if 'tracks' in pl_detail: for track in pl_detail['tracks']: if 'title' in track: new_track = { 'title': track['title'], 'artist': 'any', 'album': 'any' } if 'artists' in track and len(track['artists']) > 0: new_track['artist'] = track['artists'][0]['name'] if 'album' in track and track[ 'album'] != None and 'name' in track['album']: new_track['album'] = track['album']['name'] list_of_song.append(new_track) except Exception as e: print("Unexpected Error in show_song_in_playlist:", e) return json.dumps(list_of_song) # access: 'PRIVATE', 'PUBLIC', 'UNLISTED' # Return: A tuple of (create_status, playlist_id, add_status) def new_playlist(self, playlist_name: str, desc: str = "A playlist created by PlaySync on " + str(datetime.today().strftime('%Y-%m-%d')), access: str = 'PRIVATE', tracks=[]): try: playlist_id = self.api.create_playlist(title=playlist_name, description=desc, privacy_status=access) if type(playlist_id) == str: # It is an id if len(tracks) > 0: status = self.api.add_playlist_items(playlist_id, tracks) return (0, playlist_id, status ) # Creation successful, add status attached else: return (0, playlist_id, "NULL" ) # Creation successful, didn't add else: # Status message, means error in creation return (-1, 0, playlist_id) except Exception as e: print("Unexpected Error in new_playlist:", e) return (-2, 0, e) # Didn't crash gracefully def search_song(self, song_title: str, song_artist: str = "", song_misc: str = ""): song_list = [] try: search_results = self.api.search(query=song_title + song_artist + song_misc, limit=10) for song_found in search_results: if (song_found['resultType'] in ['song', 'video']): new_song = { 'id': song_found['videoId'], 'title': song_found['title'], 'artist': 'None', 'album': 'None', 'duration': 'Unknown' } if len(song_found['artists']) > 0: new_song['artist'] = song_found['artists'][0]['name'] if 'album' in song_found: new_song['artist'] = song_found['album']['name'] if 'duration' in song_found: new_song['duration'] = song_found['duration'] song_list.append(new_song) except Exception as e: print("Unexpected Error in search_song:", e) return json.dumps(song_list) def add_songs(self, playlist_id: str, tracks=[]): try: status = self.api.add_playlist_items(playlist_id, tracks) return (0, playlist_id, status ) # Creation successful, add status attached except Exception as e: print("Unexpected Error in add_songs:", e) return (-2, 0, 0) # Didn't crash gracefully def del_songs(self, playlist_id: str, tracks=[]): try: if len(tracks) > 0: status = self.api.remove_playlist_items(playlist_id, videos=tracks) return status except Exception as e: return "UNCAUGHT ERROR" + str(e) return "NULL" def del_playlist(self, playlist_id: str): try: status = self.api.delete_playlist(playlist_id) return status except Exception as e: return "UNCAUGHT ERROR" + str(e)
from ytmusicapi import YTMusic ytmusic = YTMusic('headers_auth.json') playlistId = ytmusic.create_playlist("Yandex Music", "Imported from Yandex Music") fp = open('yandexTracks.txt', 'r') fSkipped = open('youtubeSkippedDuringImport.txt', 'a') for track in fp: print(track) search_results = ytmusic.search(track.strip()) if ('videoId' not in search_results[0]): fSkipped.writelines([track.strip(), '\n']) continue ytmusic.add_playlist_items(playlistId, [search_results[0]['videoId']]) ytmusic.rate_song(search_results[0]['videoId'], 'LIKE') fp.close() fSkipped.close()
spotifyPlaylist = sp.playlist(spotifyPlayistId) tracks = spotifyPlaylist['tracks'] trackResults = tracks['items'] while tracks['next']: tracks = sp.next(tracks) trackResults.extend(tracks['items']) songArtistSearchList = [] for track in trackResults: trackInfo = track['track'] title = trackInfo['name'] artist = trackInfo['artists'][0]['name'] songArtistSearchList.append(f"{title} {artist}") songsToAdd = GetSongVideoIds(ytmusic, songArtistSearchList) playlistTitle = spotifyPlaylist['name'] playlistDescription = "Imported from a spotify playlist" newPlaylist = ytmusic.create_playlist( playlistTitle, description="", privacy_status="PUBLIC" ) if UpdatePlaylist( ytmusic, newPlaylist, songsToAdd, description=playlistDescription ): print(f"Successfully created {playlistTitle} playlist.") else: print("There was an error creating the playlist.")