From f9baee22797499dc104b7d284aab6d1a0a0f9b44 Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Sun, 16 Feb 2025 19:50:21 +0200 Subject: [PATCH 1/5] some cleanups: * retab python file * remove cache file * ignore changes to env file * add missing requirement --- .cache | 1 - .gitignore | 2 ++ requirements.txt | 1 + spotify_to_mp3.py | 10 +++++----- 4 files changed, 8 insertions(+), 6 deletions(-) delete mode 100644 .cache create mode 100644 .gitignore 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/requirements.txt b/requirements.txt index c39903e..338ae0a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ youtube-search-python pytube spotipy +python-dotenv diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index a3fc253..2425ba7 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -41,11 +41,11 @@ def you_tube_downloader(url, path): path = args.path user_name = args.uname 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:") + print("\nGreat, thank you.") + user_name = input("Now please enter the user name you want to fetch playlists for (The download will start automatically):") From 516d4d5d8a4bd64c2cbabe5a58308f4d89e28689 Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Sun, 23 Feb 2025 15:21:48 +0200 Subject: [PATCH 2/5] fix "ERROR: post() got an unexpected keyword argument 'proxies'" according to: https://stackoverflow.com/questions/79277656/error-post-got-an-unexpected-keyword-argument-proxies --- requirements.txt | 1 + spotify_to_mp3.py | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 338ae0a..929011d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ youtube-search-python pytube spotipy python-dotenv +httpx < 0.28 diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index 2425ba7..f7f206d 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -85,8 +85,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: From afa9c3da8997ea4153e0d526985cd00513f3cf60 Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Sun, 23 Feb 2025 15:27:51 +0200 Subject: [PATCH 3/5] fix "http error 403 Forbidden" issue according to: https://stackoverflow.com/questions/79226520/pytube-throws-http-error-403-forbidden-since-a-few-days --- requirements.txt | 2 +- spotify_to_mp3.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/requirements.txt b/requirements.txt index 929011d..e05a728 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +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 f7f206d..5e589e9 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 From bf4c06e82dbb1e16a2f7a47b6fbb5d34e3089e1e Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Sun, 23 Feb 2025 16:30:03 +0200 Subject: [PATCH 4/5] add the option to specify one playlist to download --- spotify_to_mp3.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/spotify_to_mp3.py b/spotify_to_mp3.py index 5e589e9..2406be0 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -37,15 +37,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:") + 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):") + user_name = input("Now please enter the user name you want to fetch playlists for (The download will start automatically): ") @@ -72,6 +77,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) From ce9e226313846182167e69b988243ea63f00a663 Mon Sep 17 00:00:00 2001 From: Yuval Lifshitz Date: Sun, 23 Feb 2025 17:14:09 +0200 Subject: [PATCH 5/5] use ffmpeg to convert mp4 to mp3 --- README.md | 3 ++- spotify_to_mp3.py | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) 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/spotify_to_mp3.py b/spotify_to_mp3.py index 2406be0..77a33bc 100644 --- a/spotify_to_mp3.py +++ b/spotify_to_mp3.py @@ -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")