Skip to content

Commit

Permalink
Add feature: remove playlists with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
sigma67 committed Apr 26, 2020
1 parent 280d85f commit 8094b06
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions YouTube.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from ytmusicapi import YTMusic
from datetime import datetime
import os
import re
import argparse
import difflib
from SpotifyExport import Spotify
Expand Down Expand Up @@ -90,6 +91,21 @@ def remove_songs(self, playlistId):
if len(items) > 0:
self.api.remove_playlist_items(playlistId, items)

def remove_playlists(self, pattern):
playlists = self.api.get_playlists()
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.")


def get_args():
parser = argparse.ArgumentParser(description='Transfer spotify playlist to YouTube Music.')
Expand All @@ -99,13 +115,19 @@ def get_args():
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("-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).")
return parser.parse_args()


def main():
args = get_args()
ytmusic = YTMusicTransfer()

if args.remove:
ytmusic.remove_playlists(args.playlist)
return

date = ""
if args.date:
date = " " + datetime.today().strftime('%m/%d/%Y')
Expand All @@ -117,7 +139,6 @@ def main():

name = args.name + date if args.name else playlist['name'] + date
info = playlist['description'] if (args.info is None) else args.info
ytmusic = YTMusicTransfer()

if args.update:
playlistId = ytmusic.get_playlist_id(args.update)
Expand Down

0 comments on commit 8094b06

Please sign in to comment.