From 6c32571ccc2422b8ba647e5b8cf1f09d7c3781b2 Mon Sep 17 00:00:00 2001 From: Cooper Hammond Date: Fri, 14 Jun 2019 18:01:22 -0700 Subject: [PATCH] tagger edits --- shard.lock | 4 ---- src/interact/ripper.cr | 4 ++-- src/interact/tagger.cr | 13 ++++++++----- src/search/spotify.cr | 21 +++++++++++++-------- src/search/youtube.cr | 16 +++++++++------- 5 files changed, 32 insertions(+), 26 deletions(-) diff --git a/shard.lock b/shard.lock index 8226e24..d04a714 100644 --- a/shard.lock +++ b/shard.lock @@ -1,9 +1,5 @@ version: 1.0 shards: - cr-taglib: - github: teknomunk/cr-taglib - commit: 5bfca75aaac31c0e4eee4c6e0d72c624af773c09 - ydl_binaries: github: cooperhammond/ydl-binaries commit: 8a622bf9d2c0b848db8d8db2f46f09334a1f03f8 diff --git a/src/interact/ripper.cr b/src/interact/ripper.cr index 747a8e4..376199e 100644 --- a/src/interact/ripper.cr +++ b/src/interact/ripper.cr @@ -11,7 +11,7 @@ module Ripper # Ripper.download_mp3("https://youtube.com/watch?v=0xnciFWAqa0", # "Queen/A Night At The Opera/Bohemian Rhapsody.mp3") # ``` - def download_mp3(video_url : String, output_filename : String) + def download_mp3(video_url : String, output_filename : String) : Nil ydl_loc = BIN_LOC.join("youtube-dl") # remove the extension that will be added on by ydl @@ -32,7 +32,7 @@ module Ripper options.keys.each do |option| command += " #{option} #{options[option]}" end - + system(command) end diff --git a/src/interact/tagger.cr b/src/interact/tagger.cr index ae4adfa..fb1d58c 100644 --- a/src/interact/tagger.cr +++ b/src/interact/tagger.cr @@ -15,11 +15,11 @@ class Tags end - def add_text_tag(key : String, value : String) + def add_text_tag(key : String, value : String) : Nil @query_args.push(%(-metadata #{key}="#{value}")) end - def add_album_art(image_location : String) + def add_album_art(image_location : String) : Nil if !File.exists?(image_location) raise "Image file not found at location: #{image_location}" end @@ -33,10 +33,13 @@ class Tags @query_args.push(%(-metadata:s:v title="Album cover")) end - def save - @query_args.push(%("#{@filename}")) + def save : Nil + @query_args.push(%("_#{@filename}")) command = @BIN_LOC.to_s + "/ffmpeg " + @query_args.join(" ") - puts command + system command + + File.delete(@filename) + File.rename("_" + @filename, @filename) end end diff --git a/src/search/spotify.cr b/src/search/spotify.cr index 4130f86..3fdfad5 100644 --- a/src/search/spotify.cr +++ b/src/search/spotify.cr @@ -16,7 +16,7 @@ class SpotifySearcher # ``` # SpotifySearcher.new().authorize("XXXXXXXXXX", "XXXXXXXXXX") # ``` - def authorize(client_id : String, client_secret : String) + def authorize(client_id : String, client_secret : String) : self auth_url = "https://accounts.spotify.com/api/token" headers = HTTP::Headers{ @@ -51,7 +51,8 @@ class SpotifySearcher # }) # => {track metadata} # ``` - def find_item(item_type : String, item_parameters : Hash, offset=0, limit=20) + def find_item(item_type : String, item_parameters : Hash, offset=0, + limit=20) : JSON::Any? query = __generate_query(item_type, item_parameters, offset, limit) @@ -70,14 +71,17 @@ class SpotifySearcher points = __rank_items(items, item_parameters) - return items[points[0][1]] - + begin + return items[points[0][1]] + rescue IndexException + return nil + end end # Generates url to run a GET request against to the Spotify open API # Returns a `String.` private def __generate_query(item_type : String, item_parameters : Hash, - offset : Int32, limit : Int32) + offset : Int32, limit : Int32) : String query = "" # parameter keys to exclude in the api request. These values will be put @@ -108,7 +112,8 @@ class SpotifySearcher # Ranks the given items based off of the info from parameters. # Meant to find the item that the user desires. # Returns an `Array` of `Array(Int32)` or [[3, 1], [...], ...] - private def __rank_items(items : Array, parameters : Hash) + private def __rank_items(items : Array, + parameters : Hash) : Array(Array(Int32)) points = [] of Array(Int32) index = 0 @@ -152,7 +157,7 @@ class SpotifySearcher # If the strings are the exact same, return 3 pts. # If *item1* includes *item2*, return 1 pt. # Else, return 0 pts. - private def __points_compare(item1 : String, item2 : String) + private def __points_compare(item1 : String, item2 : String) : Int32 item1 = item1.downcase.gsub(/[^a-z0-9]/, "") item2 = item2.downcase.gsub(/[^a-z0-9]/, "") @@ -171,7 +176,7 @@ class SpotifySearcher # __query_encode("album", "A Night At The Opera") # => "album:A+Night+At+The+Opera" # ``` - private def __param_encode(key : String, value : String) + private def __param_encode(key : String, value : String) : String return key.gsub(" ", "+") + ":" + value.gsub(" ", "+") + "+" end diff --git a/src/search/youtube.cr b/src/search/youtube.cr index d2691ef..4c82655 100644 --- a/src/search/youtube.cr +++ b/src/search/youtube.cr @@ -30,7 +30,8 @@ module Youtube # Youtube.find_url("Bohemian Rhapsody", "Queen") # => "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # ``` - def find_url(song_name : String, artist_name : String, search_terms = "", download_first = false) + def find_url(song_name : String, artist_name : String, search_terms = "", + download_first = false) : Nil query = (song_name + " " + artist_name + " " + search_terms).strip.gsub(" ", "+") url = "https://www.youtube.com/results?search_query=" + query @@ -56,7 +57,8 @@ module Youtube # Will rank videos according to their title and the user input # Returns an `Array` of Arrays each layed out like # [, ]. - private def __rank_videos(song_name : String, artist_name : String, query : String, nodes : Array(XML::Node)) + private def __rank_videos(song_name : String, artist_name : String, + query : String, nodes : Array(XML::Node)) : Array(Array(Int32)) points = [] of Hash(String, Int32) index = 0 @@ -94,7 +96,7 @@ module Youtube # If after the items have been blanked, *item1* includes *item2*, # return 1 pts. # Else, return 0 pts. - private def __points_compare(item1 : String, item2 : String) + private def __points_compare(item1 : String, item2 : String) : Int32 if item2.includes?(item1) return 3 end @@ -114,7 +116,7 @@ module Youtube # *video_name* is the title of the video, and *query* is what the user the # program searched for. *query* is needed in order to make sure we're not # subtracting points from something that's naturally in the title - private def __count_buzzphrases(query : String, video_name : String) + private def __count_buzzphrases(query : String, video_name : String) : Int32 good_phrases = 0 bad_phrases = 0 @@ -124,7 +126,7 @@ module Youtube if query.downcase.gsub(/[^a-z0-9]/, "").includes?(gold_phrase) next elsif video_name.downcase.gsub(/[^a-z0-9]/, "").includes?(gold_phrase) - bad_phrases += 1 + good_phrases += 1 end end @@ -143,7 +145,7 @@ module Youtube # Finds valid video links from a `HTTP::Client.get` request # Returns an `Array` of `XML::Node` - private def __get_video_link_nodes(doc : String) + private def __get_video_link_nodes(doc : String) : Array(XML::Node) nodes = XML.parse(doc).xpath_nodes("//a") valid_nodes = [] of XML::Node @@ -158,7 +160,7 @@ module Youtube # Tests if the provided `XML::Node` has a valid link to a video # Returns a `Bool` - private def __video_link_node?(node : XML::Node) + private def __video_link_node?(node : XML::Node) : Bool # If this passes, then the node links to a playlist, not a video if node["href"]? return false if node["href"].includes?("&list=")