module ShowRobot
Constants
- DATASOURCES
- EPISODE_PATTERNS
- VERSION
- YEAR_PATTERNS
Public Class Methods
add_datasource(sym, klass)
click to toggle source
# File lib/showrobot/db.rb, line 35 def add_datasource sym, klass DATASOURCES[sym] = klass end
config()
click to toggle source
# File lib/showrobot/config.rb, line 18 def self.config @config end
configure(*args)
click to toggle source
# File lib/showrobot/config.rb, line 22 def self.configure *args if args.length == 2 @config[args[0].to_sym] = args[1] elsif args[0].instance_of? Hash args[0].each { |k, v| @config[k.to_sym] = v } else raise "Invalid arguments to ShowRobot.configure: #{args}" end end
create_datasource(sym)
click to toggle source
# File lib/showrobot/db.rb, line 39 def create_datasource sym datasource_for(sym).new end
datasource_for(sym)
click to toggle source
# File lib/showrobot/db.rb, line 43 def datasource_for sym DATASOURCES[sym.to_sym] end
fetch(type, url)
click to toggle source
Fetches the given site and processes it as the given type
# File lib/showrobot/utility/fetch.rb, line 11 def fetch type, url if not URI(url).scheme.nil? # determine the location of the cache file cache_file = File.join(ShowRobot.config[:cache_dir], url.gsub(/([^a-zA-Z0-9_\.-]+)/) { '%' + $1.unpack('H2' * $1.size).join('%').upcase }.tr(' ', '+') + '.cache') # if USE_CACHE is true, attempt to find the file out of the cache contents = if ShowRobot.config[:use_cache] && File.exists?(cache_file) puts "Found cache entry for [ #{url} ]" if ShowRobot.config[:verbose] File.read cache_file else # not in cache, fetch from web open(url).read end # if USE_CACHE and the cache file doesn't exist, write to it if ShowRobot.config[:use_cache] and not File.exists? cache_file puts "Creating cache entry for [ #{url} ]" if ShowRobot.config[:verbose] # create the cache directory if it doesn't exist FileUtils.mkdir_p File.dirname(cache_file) if not File.directory? File.dirname(cache_file) # write to the cache file File.open(cache_file, 'w') { |f| f.write contents } end else contents = IO.read(url) end # dispatch on requested type case type when :xml XML::Parser.string(contents).parse when :yml YAML::load(contents) else raise "Invalid datatype to fetch: [ #{type.to_s} ]" end end
file_get_year(fileName)
click to toggle source
# File lib/showrobot/utility/file_get_year.rb, line 12 def file_get_year fileName if not YEAR_PATTERNS.find { |pattern| pattern.match fileName }.nil? $1 end end
load_config(file = @config[:config_file])
click to toggle source
Configure via hash
def self.configure(opts = {})
opts.each { |k, v| @config[k.to_sym] = v } if not opts.nil?
end
# File lib/showrobot/config.rb, line 36 def self.load_config(file = @config[:config_file]) begin config = YAML::load(IO.read(file)) rescue Errno::ENOENT puts :warning, "YAML configuration file could not be found. Using defaults." rescue Psych::SyntaxError puts :warning, "YAML configuration file contains invalid syntax. Using defaults" end configure config end
log(level, message)
click to toggle source
# File lib/showrobot/log.rb, line 3 def self.log level, message puts "[#{level}] #{message}" if ShowRobot.config[:verbose] File.open(config[:basepath] + '/log', 'a') do |file| file.printf "[%s](%-8s) %s\n", Time.new.strftime("%y/%m/%d %H:%M:%S"), level, message end end
parse_filename(fileName)
click to toggle source
parse the name of a file into best-guess parts
# File lib/showrobot/utility/parse_filename.rb, line 13 def parse_filename fileName # try parsing as a tv show - needs to have season/episode information if not EPISODE_PATTERNS.find { |pattern| pattern.match fileName }.nil? { :name_guess => fileName[0, fileName.index($&)], :year => ShowRobot.file_get_year(fileName), :season => $~['season'].to_i, :episode => $~['episode'].to_i, :type => :tv } else # probably a movie { :name_guess => fileName } end end
url_encode(s)
click to toggle source
# File lib/showrobot/db.rb, line 47 def url_encode(s) s.to_s.gsub(/[^a-zA-Z0-9_\-.]/n){ sprintf("%%%02X", $&.unpack("C")[0]) } end
Private Instance Methods
fetch(type, url)
click to toggle source
Fetches the given site and processes it as the given type
# File lib/showrobot/utility/fetch.rb, line 11 def fetch type, url if not URI(url).scheme.nil? # determine the location of the cache file cache_file = File.join(ShowRobot.config[:cache_dir], url.gsub(/([^a-zA-Z0-9_\.-]+)/) { '%' + $1.unpack('H2' * $1.size).join('%').upcase }.tr(' ', '+') + '.cache') # if USE_CACHE is true, attempt to find the file out of the cache contents = if ShowRobot.config[:use_cache] && File.exists?(cache_file) puts "Found cache entry for [ #{url} ]" if ShowRobot.config[:verbose] File.read cache_file else # not in cache, fetch from web open(url).read end # if USE_CACHE and the cache file doesn't exist, write to it if ShowRobot.config[:use_cache] and not File.exists? cache_file puts "Creating cache entry for [ #{url} ]" if ShowRobot.config[:verbose] # create the cache directory if it doesn't exist FileUtils.mkdir_p File.dirname(cache_file) if not File.directory? File.dirname(cache_file) # write to the cache file File.open(cache_file, 'w') { |f| f.write contents } end else contents = IO.read(url) end # dispatch on requested type case type when :xml XML::Parser.string(contents).parse when :yml YAML::load(contents) else raise "Invalid datatype to fetch: [ #{type.to_s} ]" end end
file_get_year(fileName)
click to toggle source
# File lib/showrobot/utility/file_get_year.rb, line 12 def file_get_year fileName if not YEAR_PATTERNS.find { |pattern| pattern.match fileName }.nil? $1 end end
parse_filename(fileName)
click to toggle source
parse the name of a file into best-guess parts
# File lib/showrobot/utility/parse_filename.rb, line 13 def parse_filename fileName # try parsing as a tv show - needs to have season/episode information if not EPISODE_PATTERNS.find { |pattern| pattern.match fileName }.nil? { :name_guess => fileName[0, fileName.index($&)], :year => ShowRobot.file_get_year(fileName), :season => $~['season'].to_i, :episode => $~['episode'].to_i, :type => :tv } else # probably a movie { :name_guess => fileName } end end