class AppleTvConverter::Metadata::TvDb

Public Class Methods

get_poster(data) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 126
def self.get_poster(data)
  local_file = File.join(AppleTvConverter.data_path, 'cache', 'tvdb', "#{data[:show][:series]['id']}.jpg")

  unless File.exists?(local_file)
    artwork_filename = data[:show][:series]['poster'] || ''
    artwork_filename = data[:episode]['filename'] || '' if artwork_filename.blank?
    artwork_filename = "http://thetvdb.com/banners/#{artwork_filename}" if !artwork_filename.blank?

    AppleTvConverter.copy artwork_filename, local_file unless artwork_filename.blank?
  end

  local_file
end

Private Class Methods

api_key() click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 142
def self.api_key ; return '67FBF9F0670DBDF2' ; end
delete_config_file(filename) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 169
def self.delete_config_file(filename)
  full_filename = File.join(local_cache_base_path, filename =~ /\.yml$/ ? filename : "#{filename}.yml")
  File.delete(full_filename) if File.exists?(full_filename)
end
get_and_parse_data_from_server(filename, url, url_options, response_indexes = []) { |data| ... } click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 232
def self.get_and_parse_data_from_server(filename, url, url_options, response_indexes = [])
  cache = url_options.delete(:cache) || true
  data = get_data_from_server(url, url_options)

  if data
    begin
      response_indexes.each { |idx| data = data[idx] }

      data = yield(data) if block_given?

      save_config_file filename, data if cache
    rescue
      data = nil
    end
  end
end
get_data(filename, url, url_options, response_indexes = []) { |data| ... } click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 207
def self.get_data(filename, url, url_options, response_indexes = [])
  AppleTvConverter.logger.debug "-> Getting data: #{filename}"
  data = load_config_file(filename)

  unless data
    data = get_data_from_server(url, url_options)

    if data
      begin
        response_indexes.each { |idx| data = data[idx] }

        data = yield(data) if block_given?

        save_config_file filename, data
      rescue
        data = nil
      end
    end
  else
    # ap ['found on cache', filename, data]
  end

  return data
end
get_data_from_server(url, options = {}) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 175
def self.get_data_from_server(url, options = {})
  AppleTvConverter.logger.debug "  -> Getting from server: #{url}"
  cache = options.delete(:cache) || true
  zip = options.delete(:zip) || false
  response = self.get(url, options).parsed_response

  if zip
    filename = File.join(local_cache_base_path, 'zip_file.zip')

    begin
      File.open(filename, 'wb') { |f| f.write response }
      response = {}

      Zip::ZipFile.open(filename) do |zipfile|
        zipfile.each do |entry|
          unless entry.name.downcase["__macosx"]
            zip_data = zipfile.read(entry)
            response[entry.name.to_s.gsub(/\.xml$/i, '')] = zip_data
          end
        end
      end
    rescue => e
      ap [e, e.backtrace]

    ensure
      FileUtils.rm_f filename if File.exists?(filename)
    end
  end

  return response
end
get_updates_from_server(options = {}) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 249
def self.get_updates_from_server(options = {})
  get_and_parse_data_from_server('updates', '/Updates.php', { :query => { :type => 'all', :time => load_config_file('update')}, :cache => false }, ['Items']) do |data|
    if data
      # Delete each show's cached data
      data['Series'].each do |show_id|
        delete_config_file show_id
        delete_config_file "#{show_id}.jpg"
      end

      # Save the new timestamp
      save_config_file 'update', data['Time']
      @server_update_timestamp = data['Time']
    end
  end
end
load_config_file(filename) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 159
def self.load_config_file(filename)
  full_filename = File.join(local_cache_base_path, filename =~ /\.yml$/ ? filename : "#{filename}.yml")
  File.exists?(full_filename) ? YAML.load_file(full_filename) : nil
end
local_cache_base_path() click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 143
def self.local_cache_base_path
  return File.expand_path(File.join(AppleTvConverter.data_path, 'cache', 'tvdb'))
end
save_config_file(filename, data) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 164
def self.save_config_file(filename, data)
  full_filename = File.join(local_cache_base_path, filename =~ /\.yml$/ ? filename : "#{filename}.yml")
  File.open(full_filename, 'w') { |f| f.write data.to_yaml }
end
server_update_timestamp() click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 146
def self.server_update_timestamp
  @server_update_timestamp ||= load_config_file('update')

  unless @server_update_timestamp
    # http://thetvdb.com/api//Updates.php?type=none
    @server_update_timestamp = get_data_from_server('/Updates.php', { :query => { :type => 'none' }})["Items"]["Time"] rescue nil
    @server_update_timestamp = @server_update_timestamp.to_i unless @server_update_timestamp.nil?
    save_config_file 'update', @server_update_timestamp
  end

  @server_update_timestamp
end
xml_document_to_hash(document) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 265
def self.xml_document_to_hash(document)
  def self.xml_node_to_hash(xml)
    return nil if xml.children.empty?
    return xml.children.first.to_s if xml.children.count == 1 && xml.children.first.text?

    # Append a sequential number to the name to prevent replacing items that should be in an array
    child_number = 0
    Hash[*(xml.children.map { |child| child_number += 1 ; ["#{child.name}::#{child_number}", xml_node_to_hash(child)] }.compact.flatten(1))]
  end

  intermediate_hash = xml_node_to_hash(document.root)

  return Hash[*(intermediate_hash.group_by do |obj|
    obj.first.gsub(/::\d+$/, '')
  end.map do |key, value|
    # Remove the 'key' entries
    value = value.flatten(1).delete_if { |v| v.to_s =~ /#{key}::\d+/ }

    # Remove the sequential number from the keys
    value.map! do |element|
      Hash[*(element.map do |ikey, ivalue|
        [ikey.gsub(/::\d+$/, ''), ivalue]
      end.flatten(1))]
    end

    # If there's only one entry, remove the array
    value = value.first if value.count == 1

    [key, value]
  end.flatten(1))]
end
xml_node_to_hash(xml) click to toggle source
# File lib/apple_tv_converter/metadata/tv_db.rb, line 266
def self.xml_node_to_hash(xml)
  return nil if xml.children.empty?
  return xml.children.first.to_s if xml.children.count == 1 && xml.children.first.text?

  # Append a sequential number to the name to prevent replacing items that should be in an array
  child_number = 0
  Hash[*(xml.children.map { |child| child_number += 1 ; ["#{child.name}::#{child_number}", xml_node_to_hash(child)] }.compact.flatten(1))]
end