From 97a8c21eb9e745a34e7b07763ff56811f3ac600d Mon Sep 17 00:00:00 2001 From: Ritiek Malhotra Date: Wed, 27 Sep 2017 09:56:47 +0530 Subject: [PATCH] Download songs using YouTube URL (#135) * Download from YouTubr URL * Slugify title only for YouTube URL's --- core/misc.py | 15 ++++++++++----- spotdl.py | 19 +++++++++++++++---- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/core/misc.py b/core/misc.py index dcf310c..3a6db49 100755 --- a/core/misc.py +++ b/core/misc.py @@ -72,11 +72,16 @@ def get_arguments(): def is_spotify(raw_song): """Check if the input song is a Spotify link.""" - if (len(raw_song) == 22 and raw_song.replace(" ", "%20") == raw_song) or \ - (raw_song.find('spotify') > -1): - return True - else: - return False + status = len(raw_song) == 22 and raw_song.replace(" ", "%20") == raw_song + status = status or raw_song.find('spotify') > -1 + return status + +def is_youtube(raw_song): + """Check if the input song is a YouTube link.""" + status = len(raw_song) == 11 and raw_song.replace(" ", "%20") == raw_song + status = status and not raw_song.lower() == raw_song + status = status or 'youtube.com/watch?v=' in raw_song + return status def sanitize_title(title): diff --git a/spotdl.py b/spotdl.py index f1d9712..25c753c 100755 --- a/spotdl.py +++ b/spotdl.py @@ -14,6 +14,7 @@ import sys import os import time + def generate_songname(tags): """Generate a string of the format '[artist] - [song]' for the given spotify song.""" raw_song = u'{0} - {1}'.format(tags['artists'][0]['name'], tags['name']) @@ -147,11 +148,17 @@ def generate_youtube_url(raw_song, tries_remaining=5): def go_pafy(raw_song): """Parse track from YouTube.""" - track_url = generate_youtube_url(raw_song) - if track_url is None: - return None + if misc.is_youtube(raw_song): + track_info = pafy.new(raw_song) else: - return pafy.new(track_url) + track_url = generate_youtube_url(raw_song) + + if track_url is None: + track_info = None + else: + track_info = pafy.new(track_url) + + return track_info def get_youtube_title(content, number=None): @@ -341,10 +348,14 @@ def grab_single(raw_song, number=None): islist = True else: islist = False + content = go_pafy(raw_song) if content is None: return + if misc.is_youtube(raw_song): + raw_song = slugify(content.title).replace('-', ' ') + # print '[number]. [artist] - [song]' if downloading from list # otherwise print '[artist] - [song]' print(get_youtube_title(content, number))