From 80cb034ce144dd1be52118c872b85586bd8f3403 Mon Sep 17 00:00:00 2001 From: Cooper Hammond Date: Fri, 21 Jun 2019 09:45:55 -0700 Subject: [PATCH] Preliminary interception logging done! --- src/bottle/cli.cr | 2 +- src/glue/song.cr | 36 +++++++++--------- src/interact/logger.cr | 85 ++++++++++++++++++++++++++++++++++++++++++ src/interact/ripper.cr | 31 ++++++++++++--- 4 files changed, 131 insertions(+), 23 deletions(-) create mode 100644 src/interact/logger.cr diff --git a/src/bottle/cli.cr b/src/bottle/cli.cr index 059a467..fcff393 100644 --- a/src/bottle/cli.cr +++ b/src/bottle/cli.cr @@ -50,7 +50,7 @@ class CLI end def act_on_args - if @args["help"]? + if @args["help"]? || @args.keys.size == 0 help exit elsif @args["version"]? diff --git a/src/glue/song.cr b/src/glue/song.cr index 5497aea..33be62c 100644 --- a/src/glue/song.cr +++ b/src/glue/song.cr @@ -33,6 +33,7 @@ class Song end if !@metadata + puts "Searching for metadata ..." @metadata = @spotify_searcher.find_item("track", { "name" => @song_name, "artist" => @artist_name @@ -48,34 +49,35 @@ class Song data = @metadata.as(JSON::Any) filename = data["track_number"].to_s + " - #{data["name"].to_s}.mp3" + puts "Searching for url ..." url = Youtube.find_url(@song_name, @artist_name, search_terms: "lyrics") if !url - raise("There was no link found on youtube for\n" + + raise("There was no url found on youtube for\n" + %("#{@song_name}" by "#{@artist_name}\n) + "Check your input and try again.") end - + puts "Downloading video ..." Ripper.download_mp3(url.as(String), filename) - temp_albumart_filename = ".tempalbumart.jpg" - HTTP::Client.get(data["album"]["images"][0]["url"].to_s) do |response| - File.write(temp_albumart_filename, response.body_io) - end + # temp_albumart_filename = ".tempalbumart.jpg" + # HTTP::Client.get(data["album"]["images"][0]["url"].to_s) do |response| + # File.write(temp_albumart_filename, response.body_io) + # end - tagger = Tags.new(filename) - tagger.add_album_art(temp_albumart_filename) - tagger.add_text_tag("title", data["name"].to_s) - tagger.add_text_tag("artist", data["artists"][0]["name"].to_s) - tagger.add_text_tag("album", data["album"]["name"].to_s) - tagger.add_text_tag("genre", - @spotify_searcher.find_genre(data["artists"][0]["id"].to_s)) - tagger.add_text_tag("track", data["track_number"].to_s) - tagger.add_text_tag("disc", data["disc_number"].to_s) + # tagger = Tags.new(filename) + # tagger.add_album_art(temp_albumart_filename) + # tagger.add_text_tag("title", data["name"].to_s) + # tagger.add_text_tag("artist", data["artists"][0]["name"].to_s) + # tagger.add_text_tag("album", data["album"]["name"].to_s) + # tagger.add_text_tag("genre", + # @spotify_searcher.find_genre(data["artists"][0]["id"].to_s)) + # tagger.add_text_tag("track", data["track_number"].to_s) + # tagger.add_text_tag("disc", data["disc_number"].to_s) - tagger.save() - File.delete(temp_albumart_filename) + # tagger.save() + # File.delete(temp_albumart_filename) end diff --git a/src/interact/logger.cr b/src/interact/logger.cr new file mode 100644 index 0000000..5eae799 --- /dev/null +++ b/src/interact/logger.cr @@ -0,0 +1,85 @@ +class Logger + + @done_signal = "---DONE---" + + @command : String + + def initialize(command : String, @log_name : String, @sleept = 0.01) + # Have the command output its information to a log and after the command is + # finished, append an end signal to the document + @command = "#{command} > #{@log_name} " # standard output to log + @command += "2> #{@log_name} && " # errors to log + @command += "echo #{@done_signal} >> #{@log_name}" # + end + + # Run @command in the background and pipe its output to the log file, with + # something constantly monitoring the log file and yielding each new line to + # the block call. Useful for changing the output of binaries you don't have + # much control over. + # Note that the created temp log will be deleted unless the command fails + # its exit or .start is called with delete_file: false + # + # ``` + # l = Logger.new(".temp.log", %(echo "CIA spying" && sleep 2 && echo "new veggie tales season")) + # l.start do |output, index| + # case output + # when "CIA spying" + # puts "i sleep" + # when .includes?("veggie tales") + # puts "real shit" + # end + # end + # ``` + def start(delete_file=true, &block) : Bool + # Delete the log if it already exists + File.delete(@log_name) if File.exists?(@log_name) + + # Run the command in the background + called = future { + system(@command) + } + + # Wait for the log file to be written to + while !File.exists?(@log_name) + sleep @sleept + end + + log = File.open(@log_name) + log_content = read_file(log) + index = 0 + + while true + temp_content = read_file(log) + + # make sure that there is new data + if temp_content.size > 0 && log_content != temp_content + log_content = temp_content + + # break the loop if the command has completed + break if log_content[0] == @done_signal + + # give the line and index to the block + yield log_content[0], index + index += 1 + end + end + + status = called.get() + if status == true && delete_file == true + log.delete() + end + + return called.get() + end + + # Reads each line of the file into an Array of Strings + private def read_file(file : IO) : Array(String) + content = [] of String + + file.each_line do |line| + content.push(line) + end + + return content + end +end \ No newline at end of file diff --git a/src/interact/ripper.cr b/src/interact/ripper.cr index 779480f..7c760ac 100644 --- a/src/interact/ripper.cr +++ b/src/interact/ripper.cr @@ -1,3 +1,5 @@ +require "./logger" + module Ripper extend self @@ -11,7 +13,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) : Bool + def download_mp3(video_url : String, output_filename : String) ydl_loc = BIN_LOC.join("youtube-dl") # remove the extension that will be added on by ydl @@ -22,6 +24,7 @@ module Ripper options = { "--output" => %("#{output_filename}.%(ext)s"), # auto-add correct ext # "--quiet" => "", + "--verbose" => "", "--ffmpeg-location" => BIN_LOC, "--extract-audio" => "", "--audio-format" => "mp3", @@ -33,11 +36,29 @@ module Ripper command += " #{option} #{options[option]}" end - if system(command) - return true - else - return false + + l = Logger.new(command, ".ripper.log") + o = RipperOutputCensor.new + + return l.start do |line, index| + o.censor_output(line, index) end end + private class RipperOutputCensor + @dl_status_index = 0 + + def censor_output(line : String, index : Int32) + case line + when .includes? "[download]" + if @dl_status_index != 0 + print "\e[1A" + print "\e[0K\r" + end + puts line + @dl_status_index += 1 + end + end + + end end \ No newline at end of file