Rewrite 7digital in haskell
This commit is contained in:
104
bin/7digital.rb
104
bin/7digital.rb
@@ -1,104 +0,0 @@
|
||||
#!/usr/bin/env ruby
|
||||
# This Source Code Form is subject to the terms of the Mozilla Public License,
|
||||
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
|
||||
# obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
# frozen_string_literal: true
|
||||
|
||||
require 'pathname'
|
||||
require 'open3'
|
||||
require 'zip'
|
||||
|
||||
# Renames music files in a directory according to the file's tags.
|
||||
# Expects two arguments:
|
||||
# - Path to the zip file with music files.
|
||||
# - Music directory to extract songs into.
|
||||
|
||||
class Song
|
||||
attr_reader :track, :extension
|
||||
attr_accessor :title, :album
|
||||
|
||||
def initialize(extension)
|
||||
@extension = extension
|
||||
end
|
||||
|
||||
def track=(track)
|
||||
@track = track.strip.split('/').first.rjust(2, '0')
|
||||
end
|
||||
|
||||
def to_s
|
||||
@track + ' - ' + @title + @extension
|
||||
end
|
||||
end
|
||||
|
||||
def find_unnamed_directory(parent_path)
|
||||
parent_path.children.filter { |child| child.basename.to_s.start_with? '_' }.first
|
||||
end
|
||||
|
||||
def extract_and_rename_archive(album_archive, music_directory)
|
||||
music_directory.mkpath
|
||||
|
||||
Zip::File.open album_archive.to_path do |zip_file|
|
||||
zip_file.each do |entry|
|
||||
puts "Inflating #{entry.name}"
|
||||
extract_target = music_directory + File.basename(entry.name)
|
||||
|
||||
entry.extract extract_target
|
||||
end
|
||||
end
|
||||
|
||||
music_directory
|
||||
end
|
||||
|
||||
def probe_song(song_path)
|
||||
song = Song.new song_path.extname
|
||||
|
||||
Open3.popen3 'ffprobe', song_path.to_s do |_stdin, _stdout, stderr, _wait_pid|
|
||||
while (line = stderr.gets)
|
||||
key, value = line.split ':', 2
|
||||
next if value.nil?
|
||||
|
||||
case key.strip.downcase
|
||||
when 'title'
|
||||
song.title = value.strip if song.title.nil?
|
||||
when 'track'
|
||||
song.track = value if song.track.nil?
|
||||
when 'album'
|
||||
song.album = value.strip if song.album.nil?
|
||||
end
|
||||
end
|
||||
end
|
||||
song
|
||||
end
|
||||
|
||||
unless ARGV.length == 2
|
||||
$stderr.puts 'Usage: 7digital.rb MUSIC_ARCHIVE.zip DIRECTORY'
|
||||
exit 1
|
||||
end
|
||||
|
||||
exit_code = 0
|
||||
album_archive = Pathname.new ARGV[0]
|
||||
music_directory = Pathname.new ARGV[1]
|
||||
metadata = {}
|
||||
|
||||
extract_and_rename_archive album_archive, music_directory
|
||||
|
||||
Dir.each_child music_directory do |filename|
|
||||
song_path = music_directory + filename
|
||||
|
||||
if filename == 'MissingFiles.txt'
|
||||
IO.copy_stream File.open(song_path, 'r'), $stderr
|
||||
exit_code = 2
|
||||
else
|
||||
metadata[song_path] = probe_song(song_path)
|
||||
end
|
||||
end
|
||||
|
||||
metadata.each_pair do |from, to|
|
||||
album_path = music_directory + to.album
|
||||
|
||||
album_path.mkpath
|
||||
File.rename(from, album_path + to.to_s)
|
||||
end
|
||||
|
||||
exit exit_code
|
||||
Reference in New Issue
Block a user