Class: Tvdbr::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/tvdbr/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Client

Creates an instance of the TVDB interface Tvdb.new(‘some_key’)



11
12
13
14
# File 'lib/tvdbr/client.rb', line 11

def initialize(api_key)
  @api_key = api_key
  check_api_key!
end

Instance Method Details

Returns a list of image locations for the series



97
98
99
# File 'lib/tvdbr/client.rb', line 97

def banner_urls(series_id)
  self.get_with_key("/series/#{series_id}/banners.xml")['Banners']['Banner']
end

#each_updated_episode(options = {}, &block) ⇒ Object

Yields the block for every updated episode tvdb.each_updated_episode(:since => 1.day.ago) { |s| Episode.find_by_tvdb_id(s.id).title = s.title }



39
40
41
42
43
44
45
# File 'lib/tvdbr/client.rb', line 39

def each_updated_episode(options={}, &block)
  updates = self.find_updates_since(options[:since])
  updates[:episodes].each do |episode_id|
    episode = self.find_episode_by_id(episode_id)
    block.call(episode) if episode && episode.name
  end if updates[:episodes].respond_to?(:each)
end

#each_updated_series(options = {}, &block) ⇒ Object

Yields the block for every updated series tvdb.each_updated_series(:since => 1.day.ago) { |s| Media.find_by_tvdb_id(s.id).title = s.title }



29
30
31
32
33
34
35
# File 'lib/tvdbr/client.rb', line 29

def each_updated_series(options={}, &block)
  updates = self.find_updates_since(options[:since])
  updates[:series].each do |series_id|
    series = self.find_series_by_id(series_id)
    block.call(series) if series && series.title
  end if updates[:series].respond_to?(:each)
end

#fetch_series_from_data(options = {}) ⇒ Object

Fetches a series object based on the given attributes hash tvdb.fetch_series_from_data(:title => “Dexter”, :kind => “TvShow”, :starring => “xxxx, xxxx”)

> { “SeriesName” => “Dexter”, … } or nil



20
21
22
23
24
25
# File 'lib/tvdbr/client.rb', line 20

def fetch_series_from_data(options={})
  return self.find_series_by_title(options[:title]) if options[:starring].nil?
  series_results = self.find_all_series_by_title(options[:title])
  expected_actors = options[:starring].split(",")
  series_results.compact.find { |series| series.actor_match?(expected_actors) }
end

#find_all_series_by_title(title) ⇒ Object

Returns all series matching the given title tvdb.find_all_series_by_title(“Dexter”)

> [{ “SeriesName” => “Dexter”, … }, …]



50
51
52
53
54
55
# File 'lib/tvdbr/client.rb', line 50

def find_all_series_by_title(title)
  result = self.class.get("/GetSeries.php", :query => { :seriesname => title, :language => "en" })['Data']
  return [] if result.blank? || result['Series'].blank?
  result = result['Series'].is_a?(Array) ? result['Series'] : [result['Series']]
  result.first(5).map { |s| self.find_series_by_id(s['seriesid']) }
end

#find_episode_by_id(episode_id, options = {}) ⇒ Object

Returns Episode data for a given episode id tvdb.find_episode_by_id(12345) tvdb.find_episode_by_id(12345, :raw => true)



82
83
84
85
86
87
88
# File 'lib/tvdbr/client.rb', line 82

def find_episode_by_id(episode_id, options={})
  episode_url = "/episodes/#{episode_id}"
  result = self.get_with_key(episode_url)['Data']
  return nil unless result && result['Episode']
  return result["Episode"] if options[:raw]
  Episode.new(self, result["Episode"])
end

#find_series_by_id(series_id, options = {}) ⇒ Object

Returns series data for a given series_id tvdb.find_series_by_id(1234, :all => true) tvdb.find_series_by_id(1234, :raw => true)



68
69
70
71
72
73
74
75
76
77
# File 'lib/tvdbr/client.rb', line 68

def find_series_by_id(series_id, options={})
  series_url = "/series/#{series_id}"
  series_url << "/all" if options[:all]
  series_url << "/en.xml"
  result = self.get_with_key(series_url)['Data']
  return nil unless result && result['Series']
  return result if options[:all]
  return result["Series"] if options[:raw]
  Series.new(self, result["Series"])
end

#find_series_by_title(title) ⇒ Object

Returns the first series returned for a title tvdb.find_series_by_title(“Dexter”)

> { “SeriesName” => “Dexter”, … }



61
62
63
# File 'lib/tvdbr/client.rb', line 61

def find_series_by_title(title)
  self.find_all_series_by_title(title).first
end

#find_updates_since(time) ⇒ Object

Returns a list of series and episode updates since given time tvdb.find_updates_since(1.day.ago)

> { :series => [1,2,3], :episodes => [1,2,3], :time => ‘<stamp>’ }



104
105
106
107
108
# File 'lib/tvdbr/client.rb', line 104

def find_updates_since(time)
  stamp = time.to_i # Get timestamp
  result = self.class.get("/Updates.php?type=all&time=#{stamp}")['Items']
  { :series => result['Series'], :episodes => result['Episode'], :time => result['Time'] }
end

#mirror_urlsObject

Returns the list of TVDB mirror_urls

> [“thetvdb.com”, …]



92
93
94
# File 'lib/tvdbr/client.rb', line 92

def mirror_urls
  Array(self.get_with_key('/mirrors.xml')['Mirrors']['Mirror']['mirrorpath'])
end