Skip to content

Instantly share code, notes, and snippets.

@chtzvt
Last active March 18, 2022 20:22
Show Gist options
  • Save chtzvt/d7180d67db2615e84bc2fb2b986dfb9f to your computer and use it in GitHub Desktop.
Save chtzvt/d7180d67db2615e84bc2fb2b986dfb9f to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'fileutils'
require 'json'
# Depends on rubyzip
require 'zip'
Dir['./*'].each do |f|
match = /(Volume. )([0-9]+[.0-9]*)( Chapter. )([0-9]+[.0-9]*)/.match(f)
next if match.nil?
caps = match.captures
vol_num = caps[1].gsub(/[^0-9.]/, '').rjust(3, '0')[-3..-1]
chap_num = '%06.2f' % caps[3].gsub(/[^0-9.]/, '').to_f
FileUtils.mkdir_p("V#{vol_num}")
FileUtils.mv(f, "V#{vol_num}/C#{chap_num}")
end
Dir['./*/**/**'].each do |f|
match = /(?i)([a-z]+[0-9]+)(-)([a-z0-9]{64})(\.[a-z]{0,5})/.match(f)
next if match.nil?
caps = match.captures
file_dir = File.dirname(f)
file_order = caps[0].gsub(/[^0-9]/, '').rjust(4, '0')[-4..-1]
file_ext = caps[3]
FileUtils.mv(f, "#{file_dir}/#{file_order}#{file_ext}")
end
# This is a simple example which uses rubyzip to
# recursively generate a zip file from the contents of
# a specified directory. The directory itself is not
# included in the archive, rather just its contents.
#
# Usage:
# directory_to_zip = "/tmp/input"
# output_file = "/tmp/out.zip"
# zf = ZipFileGenerator.new(directory_to_zip, output_file)
# zf.write()
class ZipFileGenerator
# Initialize with the directory to zip and the location of the output archive.
def initialize(input_dir, output_file)
@input_dir = input_dir
@output_file = output_file
end
# Zip the input directory.
def write
entries = Dir.entries(@input_dir) - %w[. ..]
::Zip::File.open(@output_file, create: true) do |zipfile|
write_entries entries, '', zipfile
end
end
private
# A helper method to make the recursion work.
def write_entries(entries, path, zipfile)
entries.each do |e|
zipfile_path = path == '' ? e : File.join(path, e)
disk_file_path = File.join(@input_dir, zipfile_path)
if File.directory? disk_file_path
recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
else
put_into_archive(disk_file_path, zipfile, zipfile_path)
end
end
end
def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
zipfile.mkdir zipfile_path
subdir = Dir.entries(disk_file_path) - %w[. ..]
write_entries subdir, zipfile_path, zipfile
end
def put_into_archive(disk_file_path, zipfile, zipfile_path)
zipfile.add(zipfile_path, disk_file_path)
end
end
metadata = JSON.parse(File.read('./details.json')) if File.exist?('./details.json')
Dir['./*'].each do |f|
match = /V([0-9]+)/.match(f)
next if match.nil?
vol_num = match.captures[0]
FileUtils.cp('./cover.jpg', f) if File.exist?('cover.jpg')
FileUtils.cp('./details.json', f) if File.exist?('./details.json')
filename = "#{metadata['title']} - Volume #{vol_num} - #{metadata['author']}.cbz"
puts "Creating: #{filename}"
zf = ZipFileGenerator.new(f, filename)
zf.write()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment