Class: AppleTvConverter::TvDbFetcher

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/apple_tv_converter/tv_db_fetcher.rb

Class Method Summary collapse

Class Method Details

.get_poster(media) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/apple_tv_converter/tv_db_fetcher.rb', line 88

def self.get_poster(media)
  local_file = File.join(AppleTvConverter.data_path, 'cache', 'tvdb', "#{media.tvdb_id}.jpg")

  unless File.exists?(local_file)
    artwork_filename = media.tvdb_movie[:show][:series]['poster'] || ''
    artwork_filename = media.tvdb_movie_data('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

.search(media, interactive = true, language = 'en') ⇒ Object



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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/apple_tv_converter/tv_db_fetcher.rb', line 13

def self.search(media, interactive = true, language = 'en')
  get_updates_from_server

  if media.tvdb_id
    show_id = media.tvdb_id
  else
    data = load_config_file('show_ids') || {}

    # http://thetvdb.com/api/GetSeries.php?seriesname=
    unless data.has_key?(media.show)
      show_ids = get_and_parse_data_from_server('show_ids', '/GetSeries.php', { :query => { :seriesname => media.show } }, ['Data', 'Series']) do |loaded_data|
        loaded_data = [loaded_data].flatten

        data[media.show] = if loaded_data.length > 1 && interactive
          choice = 0
          puts "\n  *"

          while true
            puts %Q[  | Several shows found, choose the intended one:]

            loaded_data.each_with_index do |item, index|
              puts "  | #{(index + 1).to_s.rjust(loaded_data.length.to_s.length)} - #{item['SeriesName']} (id: #{item['seriesid']})"
              puts "  | #{' '.rjust(loaded_data.length.to_s.length)}   AKA: #{item['AliasNames']}" if item['AliasNames']
            end

            printf "  |\n  *- What's your choice (1..#{loaded_data.length})? "
            choice = STDIN.gets.chomp.to_i

            break if choice.between?(1, loaded_data.length)

            puts "  | Invalid choice!"
            puts "  |"
          end

          loaded_data[choice - 1]['seriesid']
        else
          loaded_data.first['seriesid']
        end

        # Return the new list sorted by show name
        Hash[data.sort]
      end
    end

    show_id = data[media.show]
  end

  if show_id.to_i > 0
    # <mirrorpath_zip>/api/<apikey>/series/<seriesid>/all/<language>.zip
    show_data = get_data(show_id, "/#{api_key}/series/#{show_id}/all/#{language}.zip", { :zip => true }) do |data|
      show_data = xml_document_to_hash(XML::Document.string(data[language.to_s].gsub(/>\s*</im, '><')))
      banners = xml_document_to_hash(XML::Document.string(data['banners'].gsub(/>\s*</im, '><'))) rescue { 'Banner' => [] }
      actors = xml_document_to_hash(XML::Document.string(data['actors'].gsub(/>\s*</im, '><'))) rescue { 'Actor' => [] }

      {
        :series => show_data['Series'],
        :episodes => [show_data['Episode']].flatten,
        :banners => [banners['Banner']].flatten,
        :actors => [actors['Actor']].flatten
      }

    end

    return {
      :episode => show_data[:episodes].detect do |ep|
        # For season 1, check the absolute number first (for cartoons, etc.), and then check the usual season/episode combo
        (media.season.to_i == 1 && ep['absolute_number'].to_i == media.number.to_i) || (ep['SeasonNumber'].to_i == media.season.to_i && ep['EpisodeNumber'].to_i == media.number.to_i)
      end,
      :show => show_data
    }
  end

  return false
end

.xml_node_to_hash(xml) ⇒ Object



228
229
230
231
232
233
234
235
# File 'lib/apple_tv_converter/tv_db_fetcher.rb', line 228

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