Add ability to specifiy youtube URL source

Added a new flag (-u) to specify a youtube URL source when downloading a
single song.
This commit is contained in:
Who23 2020-09-04 20:49:07 -04:00
parent ca02d0bdc7
commit e8a71b2530
4 changed files with 36 additions and 10 deletions

View file

@ -54,6 +54,7 @@ Arguments:
-s, --song <song> Specify song name to download
-A, --album <album> Specify the album name to download
-p, --playlist <playlist> Specify the playlist name to download
-u, --url <url> Specify the youtube url to download from
Examples:
$ irs --song "Bohemian Rhapsody" --artist "Queen"

View file

@ -20,6 +20,7 @@ class CLI
[["-s", "--song"], "song", "string"],
[["-A", "--album"], "album", "string"],
[["-p", "--playlist"], "playlist", "string"],
[["-u", "--url"], "url", "string"],
]
@args : Hash(String, String)
@ -48,6 +49,7 @@ class CLI
#{Style.blue "-s, --song <song>"} Specify song name to download
#{Style.blue "-A, --album <album>"} Specify the album name to download
#{Style.blue "-p, --playlist <playlist>"} Specify the playlist name to download
#{Style.blue "-u, --url <url>"} Specify the youtube url to download from (for single songs only)
#{Style.bold "Examples:"}
$ #{Style.green %(irs --song "Bohemian Rhapsody" --artist "Queen")}
@ -82,7 +84,7 @@ class CLI
elsif @args["song"]? && @args["artist"]?
s = Song.new(@args["song"], @args["artist"])
s.provide_client_keys(Config.client_key, Config.client_secret)
s.grab_it
s.grab_it(@args["url"]?)
s.organize_it(Config.music_directory)
exit
elsif @args["album"]? && @args["artist"]?

View file

@ -24,7 +24,9 @@ class Song
],
"url" => [
" Searching for URL ...\r",
Style.green(" + ") + Style.dim("URL found \n")
Style.green(" + ") + Style.dim("URL found \n"),
" Validating URL ...\r",
Style.green(" + ") + Style.dim("URL validated \n")
],
"download" => [
" Downloading video:\n",
@ -47,11 +49,12 @@ class Song
end
# Find, downloads, and tags the mp3 song that this class represents.
#
# Optionally takes a youtube URL to download from
#
# ```
# Song.new("Bohemian Rhapsody", "Queen").grab_it
# ```
def grab_it
def grab_it(url : (String | Nil) = nil)
outputter("intro", 0)
if !@spotify_searcher.authorized? && !@metadata
@ -81,14 +84,23 @@ class Song
data = @metadata.as(JSON::Any)
@filename = data["track_number"].to_s + " - #{data["name"].to_s}.mp3"
outputter("url", 0)
url = Youtube.find_url(@song_name, @artist_name, search_terms: "lyrics")
if !url
raise("There was no url found on youtube for " +
%("#{@song_name}" by "#{@artist_name}. ) +
"Check your input and try again.")
outputter("url", 0)
url = Youtube.find_url(@song_name, @artist_name, search_terms: "lyrics")
if !url
raise("There was no url found on youtube for " +
%("#{@song_name}" by "#{@artist_name}. ) +
"Check your input and try again.")
end
outputter("url", 1)
else
outputter("url", 2)
if !Youtube.is_valid_url(url)
raise("The url '#{url}' is an invalid youtube URL " +
"Check the URL and try again")
end
outputter("url", 3)
end
outputter("url", 1)
outputter("download", 0)
Ripper.download_mp3(url.as(String), @filename)

View file

@ -23,6 +23,17 @@ module Youtube
alias NODES_CLASS = Array(Hash(String, String))
# Checks if the given URL is a valid youtube URL
#
# ```
# Youtube.is_valid_url("https://www.youtube.com/watch?v=NOTANACTUALVIDEOID")
# => false
# ```
def is_valid_url(url : String) : Bool
response = HTTP::Client.get(url)
return !(response.status_code == 404)
end
# Finds a youtube url based off of the given information.
# The query to youtube is constructed like this:
# "<song_name> <artist_name> <search terms>"