Download songs using YouTube URL (#135)

* Download from YouTubr URL

* Slugify title only for YouTube URL's
This commit is contained in:
Ritiek Malhotra
2017-09-27 09:56:47 +05:30
committed by GitHub
parent 17e6d1fa2e
commit 97a8c21eb9
2 changed files with 25 additions and 9 deletions

View File

@@ -72,11 +72,16 @@ def get_arguments():
def is_spotify(raw_song): def is_spotify(raw_song):
"""Check if the input song is a Spotify link.""" """Check if the input song is a Spotify link."""
if (len(raw_song) == 22 and raw_song.replace(" ", "%20") == raw_song) or \ status = len(raw_song) == 22 and raw_song.replace(" ", "%20") == raw_song
(raw_song.find('spotify') > -1): status = status or raw_song.find('spotify') > -1
return True return status
else:
return False 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): def sanitize_title(title):

View File

@@ -14,6 +14,7 @@ import sys
import os import os
import time import time
def generate_songname(tags): def generate_songname(tags):
"""Generate a string of the format '[artist] - [song]' for the given spotify song.""" """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']) 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): def go_pafy(raw_song):
"""Parse track from YouTube.""" """Parse track from YouTube."""
track_url = generate_youtube_url(raw_song) if misc.is_youtube(raw_song):
if track_url is None: track_info = pafy.new(raw_song)
return None
else: 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): def get_youtube_title(content, number=None):
@@ -341,10 +348,14 @@ def grab_single(raw_song, number=None):
islist = True islist = True
else: else:
islist = False islist = False
content = go_pafy(raw_song) content = go_pafy(raw_song)
if content is None: if content is None:
return return
if misc.is_youtube(raw_song):
raw_song = slugify(content.title).replace('-', ' ')
# print '[number]. [artist] - [song]' if downloading from list # print '[number]. [artist] - [song]' if downloading from list
# otherwise print '[artist] - [song]' # otherwise print '[artist] - [song]'
print(get_youtube_title(content, number)) print(get_youtube_title(content, number))