Module: ShowRobot

Defined in:
lib/showrobot/db.rb,
lib/showrobot/log.rb,
lib/showrobot/config.rb,
lib/showrobot/version.rb,
lib/showrobot/db/tvrage.rb,
lib/showrobot/video/avi.rb,
lib/showrobot/db/thetvdb.rb,
lib/showrobot/media_file.rb,
lib/showrobot/utility/fetch.rb,
lib/showrobot/utility/file_get_year.rb,
lib/showrobot/utility/parse_filename.rb

Defined Under Namespace

Classes: AVIFile, Datasource, MediaFile, TVRage, TheTVDB

Constant Summary collapse

DATASOURCES =
{}
VERSION =
'0.0.1'
YEAR_PATTERNS =
[
	# in parens
	/\((\d{4})\)/,
	# in square brackets
	/\[(\d{4})\]/,
	# ... really, just any 4 numbers in a row.
	/\D(\d{4})\D/
]
EPISODE_PATTERNS =
[
	# S01E02
	/s(?<season>\d{1,4}).?(?<episode>\d{1,4})/i,
	# season01episode02
	/season\s*(?<season>\d{1,4})\sepisode\s*(?<episode>\d{1,4})/i,
	# sxe
	/\D(?<season>\d{1,4})x(?<episode>\d{1,4})\D/
]

Class Method Summary collapse

Class Method Details

.add_datasource(sym, klass) ⇒ Object



35
36
37
# File 'lib/showrobot/db.rb', line 35

def add_datasource sym, klass
	DATASOURCES[sym] = klass
end

.configObject



18
19
20
# File 'lib/showrobot/config.rb', line 18

def self.config
	@config
end

.configure(*args) ⇒ Object



22
23
24
25
26
27
28
29
30
# 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) ⇒ Object



39
40
41
# File 'lib/showrobot/db.rb', line 39

def create_datasource sym
	datasource_for(sym).new
end

.datasource_for(sym) ⇒ Object



43
44
45
# File 'lib/showrobot/db.rb', line 43

def datasource_for sym
	DATASOURCES[sym.to_sym]
end

.fetch(type, url) ⇒ Object

Fetches the given site and processes it as the given type



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ Object



12
13
14
15
16
# 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 = ) ⇒ Object

Configure via hash def self.configure(opts = {}) opts.each { |k, v| @config = v } if not opts.nil? end



36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Object



3
4
5
6
7
8
# 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) ⇒ Object

parse the name of a file into best-guess parts



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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) ⇒ Object



47
48
49
# 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