Created
October 26, 2009 20:56
-
-
Save millenomi/219034 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
module Episodic; end | |
module Episodic::EpisodeAPI | |
@produce_debug_output = false | |
def self.debug | |
@produce_debug_output = true | |
end | |
def self.is_debugging | |
@produce_debug_output | |
end | |
def log(x) | |
puts "Episode API: #{x}" if EpisodeAPI.is_debugging | |
end | |
def <=>(x) | |
if self.name == x.name and self.ordinal and x.ordinal | |
self.ordinal <=> x.ordinal | |
elsif self.name and x.name | |
self.name <=> x.name | |
else | |
0 | |
end | |
end | |
def self.info_from_bracketed_filename(name) | |
# regular bracketed heuristic: | |
# [abc][def]_the_name_xyz_123_[some][stuff].abc | |
# Remove the extension. | |
extension = File.extname(name) | |
bracketed = unless extension == '' | |
name[0..(name.length - extension.length - 1)] | |
else | |
name.dup | |
end | |
after_removing_extension = bracketed.dup if @produce_debug_output | |
# Remove bracketed stuff. | |
bracketed.gsub!(/\[.*?\]/, '') | |
bracketed.gsub!('[', '') | |
bracketed.gsub!(']', '') # clears dupe unmatched brackets | |
bracketed.gsub!(/\(.*?\)\s*$/, '') | |
after_removing_brackets = bracketed.dup if @produce_debug_output | |
# Turn this-and-that-01 into this and that 01 | |
spaces_regexp = nil | |
unless bracketed.match(/ /) | |
[/_/, /-/, /\./].each do |regexp| | |
if bracketed.scan(regexp).length > 1 | |
spaces_regexp = regexp | |
break | |
end | |
end | |
bracketed.gsub!(spaces_regexp, ' ') | |
end | |
after_removing_spaces = bracketed.dup if @produce_debug_output | |
# Catch SxxEyy and 1x2 numbers. We remove everything after that. so: | |
# Some Show S01E13 XviD HDTV => Some Show (noting epno and season) | |
season = nil | |
epno = nil | |
if m = bracketed.match(/\s+[sS](\d+)[eE](\d+)(\s|$)/) | |
season = m[1].to_i | |
epno = m[2].to_i | |
bracketed[m.begin(0)..(bracketed.length - 1)] = '' | |
elsif m = bracketed.match(/\s+(\d+)[xX](\d+)(\s|$)/) | |
season = m[1].to_i | |
epno = m[2].to_i | |
bracketed[m.begin(0)..(bracketed.length - 1)] = '' | |
end | |
after_idiomatic_season_removal = bracketed.dup | |
# Pick up an episode number from the end | |
unless epno | |
bracketed.gsub!(/\s+\d+\s*$/) do |match| | |
epno = match.strip.to_i | |
'' | |
end | |
end | |
after_removing_epno = bracketed.dup if @produce_debug_output | |
# Remove any leftover tail separators (Hello - 01 => Hello -) and whitespace | |
bracketed.gsub!(/-+\s*$/, '') | |
bracketed = bracketed.strip | |
after_edges_cleanup = bracketed.dup | |
w = { :name => bracketed, :ordinal => epno, :grouping_ordinal => season } | |
if @produce_debug_output | |
w[:debug] = { | |
:after_removing_extension => after_removing_extension, | |
:after_removing_brackets => after_removing_brackets, | |
:after_removing_spaces => after_removing_spaces, | |
:after_edges_cleanup => after_edges_cleanup, | |
:after_removing_epno => after_removing_epno, | |
:after_idiomatic_season_removal => after_idiomatic_season_removal, | |
:spaces_regexp => spaces_regexp | |
} | |
end | |
w | |
end | |
def set_guessing_from_media(media_path) | |
self.media = media_path | |
basename = File.basename(media_path) | |
info = EpisodeAPI.info_from_bracketed_filename(basename) | |
self.name = info[:name] unless self.name | |
self.ordinal = info[:ordinal] unless self.ordinal | |
end | |
end | |
# -------------- | |
class Episodic::Series | |
def initialize(metadata) | |
@name = metadata[:name].dup if metadata[:name] | |
@episodes = [] | |
end | |
attr_reader :episodes | |
attr_accessor :name | |
end | |
class Episodic::Episode | |
include Episodic::EpisodeAPI | |
KEYS = [:name, :ordinal, :media, :series, :length, :length_unit] | |
attr_accessor(*KEYS) | |
def initialize(metadata = {}) | |
KEYS.each do |k| | |
setter = "#{k}=".to_s | |
self.send setter, metadata[k] if metadata[k] | |
end | |
end | |
end | |
# -------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment