Skip to content

Instantly share code, notes, and snippets.

@mindreframer
Created November 30, 2014 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mindreframer/3bb34e0d2ad857d02ede to your computer and use it in GitHub Desktop.
Save mindreframer/3bb34e0d2ad857d02ede to your computer and use it in GitHub Desktop.
give files index based on order in a table of content file
# quick script to give files an index calculated from a table of contents file
class Renamer
COMMON_STRING = 'Series Name by Author Name'
def run
actual.content.each do |file|
clean_file = file.gsub(COMMON_STRING, '')
to = wished.wished_file_for_file(clean_file)
cmd = "mv '#{file}' #{to}"
puts cmd
`#{cmd}`
end
end
def actual
@actual ||= ActualContent.new
end
def wished
@wished ||= WishedContent.new
end
end
class ActualContent
def content
@content ||= Dir['*.flv']
end
end
class Ranker
COMMON_WORDS = %w(
and
with
on
flv
a
)
def self.similarity(w1, w2)
words1 = words_from_string(w1)
words2 = words_from_string(w2)
res = (words1 & words2).size
res
end
def self.words_from_string(str)
all = str.split(/[-\s\.]/).map{|x|
a = x.gsub(/[^\w-]/, '')
a = a.downcase
a = nil if a == ''
a
}.compact.uniq.sort
(all - COMMON_WORDS)
end
end
class WishedContent
# @public
def wished_file_for_file(file)
wished_files.sort_by do |wished_file|
-Ranker.similarity(file, wished_file)
end.first
end
def lines
@lines ||= File.read('Table of Contents.txt').split("\n")
end
def wished_files
@wished_files ||= lines.each_with_index.map do |el, i|
el = el.strip
filename_with_index(el, i) if i % 2 == 0
end.compact
end
def filename_with_index(file, index)
file_index = ((index + 1) / 2) + 1
clean_name = to_url(file.strip)
file_name = file_index.to_s.rjust(2, '0') + '-' + clean_name + ".flv"
end
def to_url(str)
str.downcase.strip.gsub(' ', '-').gsub('&', 'and').gsub(/[^\w-]/, '')
end
end
Renamer.new.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment