Created
August 15, 2013 04:19
-
-
Save toroidal-code/6238224 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'highline/import' | |
require 'colorize' | |
l_tag_regex = /(^\[[\w-]+\]\s*|_-_THORA\s*)/ #/^\[[\w-]+\]\s*/ #Remove sub group tag. THORA gets a special case | |
t_tag_regex = /\s*\[\w{8}\]$/ # match [A71160AA]-formatted trailing tags (CRC32) | |
e_tag_regex = /((\[|\()(?!\d{4}\)).*?(\]|\)))/ # Don't match (year), but match [1080p,etc.] or (1080p, etc.); | |
# will also exclude match of [year) #TODO: Fix this | |
class Rename | |
attr_accessor :orig, :new | |
def initialize(filename) | |
@path = File.expand_path(File.dirname(filename)) << File::SEPARATOR | |
@orig = File.basename(filename) | |
if File.file? filename | |
@ext = File.extname filename | |
@orig.slice! @ext | |
else | |
@ext = String.new | |
end | |
@new = String.new @orig | |
end | |
end | |
#/\s*\([\w\s-]*\)/ | |
#((\[|\().*?(\]|\))) | |
if ARGV.empty? | |
work_dir = ask "Dir or File: " | |
work_dir.gsub!(/\\/, "") | |
else | |
work_dir = ARGV.first | |
end | |
if File.directory? work_dir | |
Dir.chdir(work_dir) | |
file_list = [ File.basename(work_dir)] # we need to change the directory's name at the same time | |
file_path_list = ["../"] | |
file_list.concat Dir.glob('*') # add all the files in the directory | |
file_ext_list = file_list.collect do |file| | |
if File.file? file | |
ext = File.extname(file) # grab the file's extension | |
file.slice! ext # remove the extension from the file's name | |
ext # collect the extension into separate array | |
else | |
"" | |
end | |
end | |
file_path_list << file_list.collect do |file| | |
"" | |
end | |
elsif File.file? work_dir # We're not a directory, let's try being a file | |
Dir.chdir(File.dirname(work_dir)) # Switch to the dir holding the file | |
file_list = [File.basename(work_dir).sub(File.extname(work_dir), "")] | |
file_ext_list = [File.extname(work_dir)] | |
file_path_list = [""] #File.dirname(File.expand_path(work_dir))] | |
else | |
puts "Error! We need a file or directory to function properly".colorize(:red) | |
exit | |
end | |
def process(files, type, match) | |
files.map do |file| | |
if type == :delim | |
file.gsub!(match, ' ') | |
elsif type == :rem | |
file.gsub!(match, '') | |
elsif type == :f_tags | |
file <<= " " << '[' << match << ']' | |
end | |
end | |
end | |
renames = Marshal.load( Marshal.dump(file_list)) | |
detected_delim = %w(_ .).sort_by{|separator| renames.first.count(separator)}.last | |
delim = ask("Deliminator? detected: ") { |q| q.default = detected_delim } | |
if ask("Remove deliminators (y/n)? ") { |q| q.default = "y" }.eql? 'y' || 'Y' | |
process(renames, :delim, delim) | |
end | |
# if renames.any? { |file| file.match(l_tag_regex) } and | |
# ask("Move translator tag: '#{renames[renames.index{|e| e =~ l_tag_regex }].match(l_tag_regex)}' (y/n)? ") { |q| q.default = 'y' }.eql? 'y' || 'Y' | |
# process(renames, :trans, l_tag_regex) | |
# end | |
if renames.any? { |file| file.match(t_tag_regex)} and | |
ask("Remove trailing tag: '#{renames[renames.index{|e| e =~ t_tag_regex }].match(t_tag_regex)}' (y/n)? ") { |q| q.default = 'y' }.eql? 'y' || 'Y' | |
process(renames, :rem, t_tag_regex) | |
end | |
if renames.first.match(e_tag_regex) and ask("Remove other tags (y/n)? ") { |q| q.default = 'y' }.eql? 'y' || 'Y' | |
process(renames, :rem, e_tag_regex) | |
end | |
if ask("Remove anything else (y/n)? ") { |q| q.default = "n"}.eql? 'y' || 'Y' | |
rem_string = ask("Enter text to remove: ") | |
process(renames, :rem, rem_string) | |
end | |
if ask("Auto format (y/n)? ") { |q| q.default = "y" }.eql? 'y' || 'Y' | |
file_list.zip(renames) do |orig, new| | |
orig.match(/TV/i) { process([new], :f_tags, "TV")} | |
orig.match(/DVD/i) { process([new], :f_tags, "DVD")} | |
orig.match(/(BD|Blu)/i) { process([new], :f_tags, "BD")} | |
orig.match(/(1920|1080)/) { process([new], :f_tags, "1080p") } | |
orig.match(/(1280|720)/) { process([new], :f_tags, "720p") } | |
orig.match(/(Coalgirls|THORA|FFF|Hi10|h10|10bit)/i) { process([new], :f_tags, "Hi10")} | |
orig.match(/AAC/i) { process([new], :f_tags, "AAC") } | |
orig.match(/FLAC/i) { process([new], :f_tags, "FLAC") } | |
orig.match(/DTS/i) { process([new], :f_tags, "DTS") } | |
orig.match(/AC3/i) { process([new], :f_tags, "AC3") } | |
orig.match(/CEN/i) { process([new], :f_tags, "CENSORED")} | |
#puts orig | |
orig.match(l_tag_regex) do |m| | |
mm = m.captures.first | |
if mm.include? ' ' | |
mm.gsub!(delim, ' ') | |
process(renames, :rem, mm) if delim == '_' | |
end | |
c = m.captures.first.delete("[]_ -") | |
process([new], :f_tags, c) | |
end | |
end | |
end | |
if not (f_tags = ask("Enter format tags: ")).eql? '' | |
process(renames, :f_tags, f_tags) | |
end | |
process(renames, :delim, /[ ]{2,}/) # Remove any extraneous spaces | |
process(renames, :rem, /^\s+/) | |
file_list = file_list.zip(file_path_list, file_ext_list) | |
file_list.collect! do |file, path, ext| | |
path + file << ext | |
end | |
renames = renames.zip(file_path_list, file_ext_list) | |
renames.collect! do |file, path, ext| | |
path + file << ext | |
end | |
file_list = file_list.zip(renames) | |
puts "Files to rename:".colorize(:blue) | |
file_list.each do |orig, replace| | |
puts "'#{orig}'".colorize(:green) << ' => ' << "'#{replace}'".colorize(:red) | |
end | |
if (ask("Are you sure (y/n)? ") { |q| q.default = "n" }).eql? 'y' || 'Y' | |
file_list.each {|orig, replace| File.rename orig, replace} | |
end | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment