From 616ea3b011d71682499122f981509ece5db15f11 Mon Sep 17 00:00:00 2001 From: rhnvrm Date: Tue, 29 Dec 2015 15:43:45 +0530 Subject: [PATCH] working demo --- .gitignore | 1 + README.md | 2 +- spotify-dl.py | 10 +++++++++- spotify.py | 29 ++++++++++++++++++++++++++++- test_download.py | 12 ++++++++++++ tokens.py | 2 +- 6 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 test_download.py diff --git a/.gitignore b/.gitignore index f05602d7..a08721b2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ .cache-sathyabhat songs.txt __pycache__/ +*.pyc \ No newline at end of file diff --git a/README.md b/README.md index c38fd4f5..deb3bf07 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Pre-requisite: You need Python 3+ 5. Create your YouTube api & fetch the keys from [Google Developer Console](https://console.developers.google.com/apis/api/youtube/overview). Paste the keys in tokens.py. 6. Run the script using `python spotify-dl` 7. Click on the URL prompted to authenticate. Once logged in, paste the URL back in -8. Once done, songs.txt should have list of YouTube URLs. Pass them to youtube-dl to have them downloaded. Please check [youtube-dl](https://rg3.github.io/youtube-dl/) documentation for more details +8. Once done, songs.txt should have list of YouTube URLs. Pass them to youtube-dl to have them downloaded. Please check [youtube-dl](https://rg3.github.io/youtube-dl/) documentation for more details. You also need ffmpeg or avconv installed. (`sudo apt-get install -y libav-tools`) ##To Do diff --git a/spotify-dl.py b/spotify-dl.py index c077944b..2fa51b0b 100644 --- a/spotify-dl.py +++ b/spotify-dl.py @@ -6,14 +6,22 @@ from spotify import save_songs_to_file from youtube import fetch_youtube_url import spotipy +import argparse if __name__ == '__main__': log.info('Starting spotify-dl') + + parser = argparse.ArgumentParser(prog='spotify-dl') + parser.add_argument('-d', '--download', action='store_true', help='Download using youtube-dl') + args = parser.parse_args() + token = authenticate() sp = spotipy.Spotify(auth=token) songs = fetch_saved_tracks(sp) url = [] for s in songs: url.append(fetch_youtube_url(s)) - save_songs_to_file(url) \ No newline at end of file + save_songs_to_file(url) + if(args.download == True): + download_songs(url) \ No newline at end of file diff --git a/spotify.py b/spotify.py index 6b962e71..1b799086 100644 --- a/spotify.py +++ b/spotify.py @@ -1,8 +1,13 @@ +from __future__ import unicode_literals + import spotipy.util as util from scaffold import * from tokens import * +import youtube_dl + + def authenticate(): return util.prompt_for_user_token(username,scope, CLIENT_ID, CLIENT_SECRET, REDIRECT_URL) @@ -29,4 +34,26 @@ def fetch_saved_tracks(sp): def save_songs_to_file(songs): with open('songs.txt', 'a') as f: f.write('\n'.join(songs)) - f.close() \ No newline at end of file + f.close() + +def download_songs(songs): + + ydl_opts = { + 'format': 'bestaudio/best', + 'postprocessors': [{ + 'key': 'FFmpegExtractAudio', + 'preferredcodec': 'mp3', + 'preferredquality': '192', + }], + } + + + with youtube_dl.YoutubeDL(ydl_opts) as ydl: + for item in songs: + try: + ydl.download([item]) + break + except Exception: + print('Failed to download: ' +item ) + continue + diff --git a/test_download.py b/test_download.py new file mode 100644 index 00000000..b96cf788 --- /dev/null +++ b/test_download.py @@ -0,0 +1,12 @@ +from spotify import * +import argparse + +x = ['https://www.youtube.com/watch?v=5n4hlzrjWv4','https://www.youtube.com/watch?v=2YD7t4EOVfs'] + + +parser = argparse.ArgumentParser(prog='spotify-dl') +parser.add_argument('-d', '--download', action='store_true', help='Download using youtube-dl') +args = parser.parse_args() + +if(args.download == True): + download_songs(x) \ No newline at end of file diff --git a/tokens.py b/tokens.py index 8773d6d8..d9490ec0 100644 --- a/tokens.py +++ b/tokens.py @@ -4,7 +4,7 @@ #spotify tokens. get them from https://developer.spotify.com/my-applications/#!/applications CLIENT_ID = '' CLIENT_SECRET = '' -REDIRECT_URL = '' +REDIRECT_URL = '' # Youtube Token. Get from https://console.developers.google.com/apis/api/youtube/overview YOUTUBE_DEV_KEY = '' \ No newline at end of file