forked from maddox/tvdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvdb_old.rb
More file actions
181 lines (145 loc) · 5.33 KB
/
tvdb_old.rb
File metadata and controls
181 lines (145 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
####### The Tvdb API
### http://thetvdb.com
# tvdb = Tvdb.new
# results = tvdb.search("Scrubs")
#
# series = results.first
# series.banners
#
# series.banners.each { |e| puts e.inspect }
# series.episodes.each { |e| puts e.inspect }
require 'rubygems'
require 'net/http'
require 'xmlsimple'
class TvdbOld
# TODO add API key to constructor for new api
def initialize
@host = 'http://thetvdb.com/interfaces'
end
def http_get(url)
Net::HTTP.get_response(URI.parse(URI.encode(url))).body.to_s
end
# returns array of Series objects that match search terms
def search(series_name)
series = []
response = XmlSimple.xml_in(http_get("#{@host}/GetSeries.php?seriesname=#{series_name}"), { 'ForceArray' => false })
case response["Item"].class.name
when "Array"
response["Item"].each { |item| series << Series.new(item)}
when "Hash"
series << Series.new(response["Item"])
end
series
end
def get_episodes(series_id)
episodes = []
response = XmlSimple.xml_in(http_get("#{@host}/GetEpisodes.php?seriesid=#{series_id}"), { 'ForceArray' => false })
response["Item"].delete_at 0
case response["Item"].class.name
when "Array"
response["Item"].each { |item| episodes << Episode.new(episode_updates(item["id"])) if item["EpisodeName"]}
when "Hash"
episodes << Episode.new(response["Item"])
end
episodes
end
def episode_updates(item_id)
response = XmlSimple.xml_in(http_get("#{@host}/EpisodeUpdates.php?idlist=#{item_id}"), { 'ForceArray' => false })
response["Item"][1]
end
def get_episode(series_id, season_num, episode_num)
response = XmlSimple.xml_in(http_get("#{@host}/GetEpisodes.php?seriesid=#{series_id}&season=#{season_num}&episode=#{episode_num}"), { 'ForceArray' => false })
response["Item"][1]
end
def get_banners(series_id)
banners = []
response = XmlSimple.xml_in(http_get("#{@host}/GetBanners.php?seriesid=#{series_id}"), { 'ForceArray' => false })
case response["Item"].class.name
when "Array"
response["Item"].each { |item| banners << Banner.new(item) if item["BannerType"]}
when "Hash"
banners << Banner.new(response["Item"])
end
banners
end
class Series
attr_accessor :id, :status, :runtime, :airs_time, :airs_day_of_week,
:genre, :name, :overview, :network, :seasons, :banners
def initialize(details)
@client = Tvdb.new
@seasons = {}
@id = details["id"]
@status = details["Status"] unless details["Status"].class == Hash
@runtime = details["Runtime"] unless details["Runtime"].class == Hash
@airs_time = details["Airs_Time"] unless details["Airs_Time"].class == Hash
@airs_day_of_week = details["Airs_DayOfWeek"]
@genre = details["Genre"] unless details["Genre"].class == Hash
@name = details["SeriesName"]
@overview = details["Overview"] unless details["Overview"].class == Hash
@network = details["Network"] unless details["Network"].class == Hash
@banners = {}
@banners["text"] = []
@banners["graphical"] = []
@banners["season"] = {}
@banners["seasonwide"] = {}
end
def retrieve_all_episodes
@episodes ||= @client.get_episodes(@id)
end
def retrieve_banners
banners = @client.get_banners(@id)
banners.each do |banner|
case banner.banner_type
when /text/i
@banners["text"] << "http://www.thetvdb.com/banners/" + banner.path
when /graphical/i
@banners["graphical"] << "http://www.thetvdb.com/banners/" + banner.path
when /seasonwide/i
@banners["seasonwide"][banner.season] = [] if @banners["seasonwide"][banner.season] == nil
@banners["seasonwide"][banner.season] << "http://www.thetvdb.com/banners/" + banner.path
when /season/i
@banners["season"][banner.season] = [] unless @banners["season"][banner.season]
@banners["season"][banner.season] << "http://www.thetvdb.com/banners/" + banner.path
end
end
end
def fill_all_meta
retrieve_all_episodes.each do |episode|
if @seasons.key? episode.season_number
@seasons[episode.season_number][episode.number] = episode
else
@seasons[episode.season_number] = {episode.number => episode}
end
end
end
def episode(season_num, episode_num)
# TODO check the @episodes cache first.
begin
Episode.new(@client.episode_updates(@client.get_episode(@id, season_num, episode_num)["id"]))
rescue
nil
end
end
end
class Episode
attr_accessor :id, :season_number, :number, :name, :overview, :air_date, :thumb
def initialize(details)
@id = details["id"]
@season_number = details["SeasonNumber"].to_s
@number = details["EpisodeNumber"].to_s
@name = details["EpisodeName"].to_s
@overview = details["Overview"].to_s
@air_date = details["FirstAired"].to_s
@thumb = "http://thetvdb.com/banners/" + details["filename"] if details["filename"].to_s != ""
end
end
class Banner
attr_accessor :type, :banner_type, :season, :path
def initialize(details)
@type = details["Type"]
@banner_type = details["BannerType"]
@season = details["Season"]
@path = details["BannerPath"]
end
end
end