Skip to content

Commit

Permalink
working demo
Browse files Browse the repository at this point in the history
  • Loading branch information
rhnvrm committed Dec 29, 2015
1 parent b3e537d commit 616ea3b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
.cache-sathyabhat
songs.txt
__pycache__/
*.pyc
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion spotify-dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
save_songs_to_file(url)
if(args.download == True):
download_songs(url)
29 changes: 28 additions & 1 deletion spotify.py
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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()
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

12 changes: 12 additions & 0 deletions test_download.py
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 1 addition & 1 deletion tokens.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''

0 comments on commit 616ea3b

Please sign in to comment.