diff --git a/.cache b/.cache deleted file mode 100644 index a55e263..0000000 --- a/.cache +++ /dev/null @@ -1 +0,0 @@ -{"access_token": "BQCBnE2nkUb2Lj9RPZoktrxaeYmLTZfpcosULGdog_qsEHzG6ZJnUNtE6mGIxPjEmmG1eDiEHFvFwhgSpVBOuIYLWZxWBSc0NN0L38kpK9OZy82pOmc", "token_type": "Bearer", "expires_in": 3600, "expires_at": 1664217057} \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b44f310 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.env +.cache diff --git a/README.md b/README.md index 0b0116b..21b3cc3 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ Next install the required packages ``` pip3 install --user -r requirements.txt ``` +Last, install `ffmpeg`. See: https://ffmpeg.org/download.html # Spotify Credentials @@ -81,4 +82,4 @@ $ chmod +x main.sh $ ./main.sh
``` -# Have Fun! \ No newline at end of file +# Have Fun! diff --git a/requirements.txt b/requirements.txt index c39903e..e05a728 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ youtube-search-python -pytube +pytubefix spotipy +python-dotenv +httpx < 0.28 diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index a3fc253..77a33bc 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -7,7 +7,7 @@ from spotipy.oauth2 import SpotifyClientCredentials from youtubesearchpython import CustomSearch, VideoSortOrder import os -from pytube import YouTube +from pytubefix import YouTube import sys import argparse from dotenv import load_dotenv, find_dotenv @@ -20,10 +20,11 @@ def you_tube_downloader(url, path): yt = YouTube(url) - audio_file = yt.streams.filter(only_audio=True).first().download(path) - base, ext = os.path.splitext(audio_file) - new_file = base + '.mp3' - os.rename(audio_file, new_file) + video_file = yt.streams.filter(only_audio=True).first().download(path) + base, ext = os.path.splitext(video_file) + audio_file = base + '.mp3' + os.system("ffmpeg -y -stats -v 1 -i '"+video_file+"' -q:a 0 -map a '"+audio_file+"'") + os.remove(video_file) print("Done") @@ -37,15 +38,20 @@ def you_tube_downloader(url, path): parser = argparse.ArgumentParser(description='If you are having problems running the script please go over the README file, make sure you have installed the necessary packages in the requirments.txt file and make sure you have updated the keys.env file with the correct credentials. Command line example: $ python3 spotify_to_mp3.py --path --uname ') parser.add_argument("--path", help="The path for the downloaded playlis folders", default='None') parser.add_argument("--uname", help="The spotify username to download playlists from", default='None') +parser.add_argument("--playlist", help="The spotify playlist to download (optional, if not provided all playlists will be downloaded)", default='None') args = parser.parse_args() path = args.path user_name = args.uname +in_playlist = args.playlist if path == "None": - print("\n----- Welcome! -----\n") - print("To get started please input some details") - path = input("\n1. Please enter the directory path for the downloads:") - print("\nGreat, thank you.") - user_name = input("Now please enter the user name you want to fetch playlists for (The download will start automatically):") + print("\n----- Welcome! -----\n") + print("To get started please input some details") + path = input("\n1. Please enter the directory path for the downloads: ") + in_playlist = input("\n2. Please enter a specific playlist to download (just press [enter] do download all playlists): ") + if in_playlist == "": + in_playlist = None + print("\nGreat, thank you.") + user_name = input("Now please enter the user name you want to fetch playlists for (The download will start automatically): ") @@ -72,6 +78,9 @@ def you_tube_downloader(url, path): while playlists: for playlist in playlists['items']: + if (in_playlist is not None and in_playlist != playlist['name']): + print("skipping " + playlist['name']) + continue print("PLAY LIST: " + playlist['name']) playlist_path = path + "/" + user_name + "/" + playlist['name'] playlist_details = sp.playlist_items(playlist['uri'], fields=None, limit=100, offset=0, market=None) @@ -85,8 +94,8 @@ def you_tube_downloader(url, path): song_url = custom_search.result()['result'][0]['link'] print(custom_search.result()['result'][0]['link']) you_tube_downloader(song_url, playlist_path) - except: - print("could not download song") + except Exception as e: + print("could not download url. error: " + str(e)) if playlists['next']: playlists = sp.next(playlists) else: