Preliminary interception logging done!

This commit is contained in:
Cooper Hammond 2019-06-21 09:45:55 -07:00
parent 76a624d32f
commit 80cb034ce1
4 changed files with 131 additions and 23 deletions

View file

@ -50,7 +50,7 @@ class CLI
end end
def act_on_args def act_on_args
if @args["help"]? if @args["help"]? || @args.keys.size == 0
help help
exit exit
elsif @args["version"]? elsif @args["version"]?

View file

@ -33,6 +33,7 @@ class Song
end end
if !@metadata if !@metadata
puts "Searching for metadata ..."
@metadata = @spotify_searcher.find_item("track", { @metadata = @spotify_searcher.find_item("track", {
"name" => @song_name, "name" => @song_name,
"artist" => @artist_name "artist" => @artist_name
@ -48,34 +49,35 @@ class Song
data = @metadata.as(JSON::Any) data = @metadata.as(JSON::Any)
filename = data["track_number"].to_s + " - #{data["name"].to_s}.mp3" 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") url = Youtube.find_url(@song_name, @artist_name, search_terms: "lyrics")
if !url 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) + %("#{@song_name}" by "#{@artist_name}\n) +
"Check your input and try again.") "Check your input and try again.")
end end
puts "Downloading video ..."
Ripper.download_mp3(url.as(String), filename) Ripper.download_mp3(url.as(String), filename)
temp_albumart_filename = ".tempalbumart.jpg" # temp_albumart_filename = ".tempalbumart.jpg"
HTTP::Client.get(data["album"]["images"][0]["url"].to_s) do |response| # HTTP::Client.get(data["album"]["images"][0]["url"].to_s) do |response|
File.write(temp_albumart_filename, response.body_io) # File.write(temp_albumart_filename, response.body_io)
end # end
tagger = Tags.new(filename) # tagger = Tags.new(filename)
tagger.add_album_art(temp_albumart_filename) # tagger.add_album_art(temp_albumart_filename)
tagger.add_text_tag("title", data["name"].to_s) # tagger.add_text_tag("title", data["name"].to_s)
tagger.add_text_tag("artist", data["artists"][0]["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("album", data["album"]["name"].to_s)
tagger.add_text_tag("genre", # tagger.add_text_tag("genre",
@spotify_searcher.find_genre(data["artists"][0]["id"].to_s)) # @spotify_searcher.find_genre(data["artists"][0]["id"].to_s))
tagger.add_text_tag("track", data["track_number"].to_s) # tagger.add_text_tag("track", data["track_number"].to_s)
tagger.add_text_tag("disc", data["disc_number"].to_s) # tagger.add_text_tag("disc", data["disc_number"].to_s)
tagger.save() # tagger.save()
File.delete(temp_albumart_filename) # File.delete(temp_albumart_filename)
end end

85
src/interact/logger.cr Normal file
View file

@ -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

View file

@ -1,3 +1,5 @@
require "./logger"
module Ripper module Ripper
extend self extend self
@ -11,7 +13,7 @@ module Ripper
# Ripper.download_mp3("https://youtube.com/watch?v=0xnciFWAqa0", # Ripper.download_mp3("https://youtube.com/watch?v=0xnciFWAqa0",
# "Queen/A Night At The Opera/Bohemian Rhapsody.mp3") # "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") ydl_loc = BIN_LOC.join("youtube-dl")
# remove the extension that will be added on by ydl # remove the extension that will be added on by ydl
@ -22,6 +24,7 @@ module Ripper
options = { options = {
"--output" => %("#{output_filename}.%(ext)s"), # auto-add correct ext "--output" => %("#{output_filename}.%(ext)s"), # auto-add correct ext
# "--quiet" => "", # "--quiet" => "",
"--verbose" => "",
"--ffmpeg-location" => BIN_LOC, "--ffmpeg-location" => BIN_LOC,
"--extract-audio" => "", "--extract-audio" => "",
"--audio-format" => "mp3", "--audio-format" => "mp3",
@ -33,11 +36,29 @@ module Ripper
command += " #{option} #{options[option]}" command += " #{option} #{options[option]}"
end end
if system(command)
return true l = Logger.new(command, ".ripper.log")
else o = RipperOutputCensor.new
return false
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
end end
end