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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
|
package GCPlugins::GCTVepisodes::GCTvdb;
###################################################
#
# Copyright 2005-2007 Tian
#
# This file is part of GCstar.
#
# GCstar is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# GCstar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GCstar; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#
###################################################
use strict;
use GCPlugins::GCTVepisodes::GCTVepisodesCommon;
{
package GCPlugins::GCTVepisodes::GCPluginTvdb;
use base qw(GCPlugins::GCTVepisodes::GCTVepisodesPluginsBase);
use XML::Simple;
use Encode;
use LWP::Simple qw($ua);
sub parse
{
my ($self, $page) = @_;
return if $page =~ /^<!DOCTYPE html/;
my $xml;
my $xs = XML::Simple->new;
if ($self->{pass} eq 1)
{
# Searching on series name
$xml = $xs->XMLin(
$page,
ForceArray => ['Series'],
KeyAttr => []
);
foreach my $series ( @{$xml->{Series}})
{
$self->{itemIdx}++;
$self->{itemsList}[$self->{itemIdx}]->{nextUrl} = "http://www.thetvdb.com/api/A8CC4AF70D0385F3/series/".$series->{id}."/all/".$self->siteLanguage().".xml";
$self->{itemsList}[$self->{itemIdx}]->{name} = $series->{SeriesName};
$self->{itemsList}[$self->{itemIdx}]->{firstaired} = $series->{FirstAired};
}
}
else
{
if ($self->{parsingList})
{
# Searching on episodes
$xml = $xs->XMLin(
$page,
ForceArray => ['Episode'],
KeyAttr => [],
);
foreach my $episode (@{$xml->{Episode}})
{
$self->{itemIdx}++;
$self->{itemsList}[$self->{itemIdx}]->{name} = $episode->{EpisodeName}
if (!ref($episode->{EpisodeName}));
$self->{itemsList}[$self->{itemIdx}]->{season} = $episode->{SeasonNumber};
$self->{itemsList}[$self->{itemIdx}]->{episode} = $episode->{EpisodeNumber};
$self->{itemsList}[$self->{itemIdx}]->{director} = $episode->{Director}
if (!ref($episode->{Director}));
$self->{itemsList}[$self->{itemIdx}]->{writer} = $episode->{Writer}
if (!ref($episode->{Writer}));
$self->{itemsList}[$self->{itemIdx}]->{overview} = $episode->{Overview}
if (!ref($episode->{Overview}));
$self->{itemsList}[$self->{itemIdx}]->{image} = "http://thetvdb.com/banners/".$episode->{filename}
if (!ref($episode->{filename}));
$self->{itemsList}[$self->{itemIdx}]->{series} = $xml->{Series}->{SeriesName}
if (!ref($xml->{Series}->{SeriesName}));
$self->{itemsList}[$self->{itemIdx}]->{firstaired} = $xml->{Series}->{FirstAired}
if (!ref($xml->{Series}->{FirstAired}));
$self->{itemsList}[$self->{itemIdx}]->{actors} = $xml->{Series}->{Actors}
if (!ref($xml->{Series}->{Actors}));
$self->{itemsList}[$self->{itemIdx}]->{genre} = $xml->{Series}->{Genre}
if (!ref($xml->{Series}->{Genre}));
$self->{itemsList}[$self->{itemIdx}]->{runtime} = $xml->{Series}->{Runtime}
if (!ref($xml->{Series}->{Runtime}));
$self->{itemsList}[$self->{itemIdx}]->{url} = "http://www.thetvdb.com/?tab=episode&seriesid=".$episode->{seriesid}."&seasonid=".$episode->{seasonid}."&id=".$episode->{id}."&lid=".$self->siteLanguageCode();
}
}
elsif ($self->{pass} != 2)
{
# Process a given url
$xml = $xs->XMLin(
$page,
ForceArray => ['Episode'],
KeyAttr => [],
);
# Need to grab the generic series info too
my $response = $ua->get('http://www.thetvdb.com/api/A8CC4AF70D0385F3/series/'.$xml->{Episode}[0]->{seriesid}.'/'.$self->siteLanguage().'.xml');
my $result;
eval {
$result = $response->decoded_content;
};
my $seriesxml = $xs->XMLin(
$result,
ForceArray => ['Series'],
KeyAttr => [],
);
$self->{curInfo}->{season} = $xml->{Episode}[0]->{SeasonNumber};
$self->{curInfo}->{episode} = $xml->{Episode}[0]->{EpisodeNumber};
$self->{curInfo}->{name} = $xml->{Episode}[0]->{EpisodeName};
$self->{curInfo}->{series} = $seriesxml->{Series}[0]->{SeriesName};
$self->{curInfo}->{director} = $xml->{Episode}[0]->{Director}
if (!ref($xml->{Episode}[0]->{Director}));
$self->{curInfo}->{director} =~ s/^\|//;
$self->{curInfo}->{director} =~ s/\|$//;
$self->{curInfo}->{firstaired} = $xml->{Episode}[0]->{FirstAired}
if (!ref($xml->{Episode}[0]->{FirstAired}));
$self->{curInfo}->{writer} = $xml->{Episode}[0]->{Writer}
if (!ref($xml->{Episode}[0]->{Writer}));
$self->{curInfo}->{writer} =~ s/^\|//;
$self->{curInfo}->{writer} =~ s/\|$//;
if (!ref($seriesxml->{Series}[0]->{Actors}))
{
my $actorString = $seriesxml->{Series}[0]->{Actors};
$actorString =~ s/^\|//;
$actorString =~ s/\|$//;
for my $actor (split(/\|/, $actorString))
{
push @{$self->{curInfo}->{actors}}, [$actor];
}
}
if (!ref($seriesxml->{Series}[0]->{Genre}))
{
my $genreString = $seriesxml->{Series}[0]->{Genre};
$genreString =~ s/^\|//;
$genreString =~ s/\|$//;
for my $genre (split(/\|/, $genreString))
{
push @{$self->{curInfo}->{genre}}, [$genre];
}
}
$self->{curInfo}->{time} = $seriesxml->{Series}[0]->{Runtime};
$self->{curInfo}->{image} = "http://thetvdb.com/banners/".$xml->{Episode}[0]->{filename}
if (!ref($xml->{Episode}[0]->{filename}));
$self->{curInfo}->{synopsis} = $xml->{Episode}[0]->{Overview}
if (!ref($xml->{Episode}[0]->{Overview}));
$self->{curInfo}->{webPage} = "http://www.thetvdb.com/?tab=episode&seriesid=".$xml->{Episode}[0]->{seriesid}."&seasonid=".$xml->{Episode}[0]->{seasonid}."&id=".$xml->{Episode}[0]->{id}."&lid=".$self->siteLanguageCode();
}
else
{
$self->{curInfo}->{season} = $self->{itemsList}[$self->{wantedIdx}]->{season};
$self->{curInfo}->{episode} = $self->{itemsList}[$self->{wantedIdx}]->{episode};
$self->{curInfo}->{name} = $self->{itemsList}[$self->{wantedIdx}]->{name};
$self->{curInfo}->{series} = $self->{itemsList}[$self->{wantedIdx}]->{series};
$self->{curInfo}->{director} = $self->{itemsList}[$self->{wantedIdx}]->{director};
$self->{curInfo}->{director} =~ s/^\|//;
$self->{curInfo}->{director} =~ s/\|$//;
$self->{curInfo}->{firstaired} = $self->{itemsList}[$self->{wantedIdx}]->{firstaired};
$self->{curInfo}->{writer} = $self->{itemsList}[$self->{wantedIdx}]->{writer};
$self->{curInfo}->{writer} =~ s/^\|//;
$self->{curInfo}->{writer} =~ s/\|$//;
my $actorString = $self->{itemsList}[$self->{wantedIdx}]->{actors};
$actorString =~ s/^\|//;
$actorString =~ s/\|$//;
for my $actor (split(/\|/, $actorString))
{
push @{$self->{curInfo}->{actors}}, [$actor];
}
my $genreString = $self->{itemsList}[$self->{wantedIdx}]->{genre};
$genreString =~ s/^\|//;
$genreString =~ s/\|$//;
for my $genre (split(/\|/, $genreString))
{
push @{$self->{curInfo}->{genre}}, [$genre];
}
$self->{curInfo}->{time} = $self->{itemsList}[$self->{wantedIdx}]->{runtime};
$self->{curInfo}->{image} = $self->{itemsList}[$self->{wantedIdx}]->{image};
$self->{curInfo}->{synopsis} = $self->{itemsList}[$self->{wantedIdx}]->{overview};
$self->{curInfo}->{webPage} = $self->{itemsList}[$self->{wantedIdx}]->{url};
}
}
}
sub new
{
my $proto = shift;
my $class = ref($proto) || $proto;
my $self = $class->SUPER::new();
bless ($self, $class);
$self->{curName} = undef;
$self->{curUrl} = undef;
return $self;
}
sub preProcess
{
my ($self, $html) = @_;
return $html;
}
sub getSearchUrl
{
my ($self, $word) = @_;
return "http://www.thetvdb.com/api/GetSeries.php?seriesname=$word&language=".$self->siteLanguage();
}
sub getItemUrl
{
my ($self, $url) = @_;
if (!$url)
{
# If we're not passed a url, return a hint so that gcstar knows what type
# of addresses this plugin handles
$url = "http://www.thetvdb.com";
}
elsif (index($url, "api") < 0)
{
# Url isn't for the tvdb api, so we need to find the episode id
# and return a url corresponding to the api page for this movie
$url =~ /[\?&]id=([0-9]+)*/;
my $id = $1;
$url = "http://www.thetvdb.com/api/A8CC4AF70D0385F3/episodes/$id/".$self->siteLanguage().".xml";
}
return $url;
}
sub changeUrl
{
my ($self, $url) = @_;
# Make sure the url is for the api, not the main movie page
return $self->getItemUrl($url);
}
sub getNumberPasses
{
return 2;
}
sub getName
{
return "Tvdb";
}
sub needsLanguageTest
{
return 1;
}
sub testURL
{
my ($self, $url) = @_;
$url =~ /[\?&]lid=([0-9]+)*/;
my $id = $1;
return ($id == $self->siteLanguageCode());
}
sub getReturnedFields
{
my $self = shift;
if ($self->{pass} == 1)
{
$self->{hasField} = {
name => 1,
firstaired => 1,
};
}
else
{
$self->{hasField} = {
name => 1,
season => 1,
episode => 1,
};
}
}
sub getAuthor
{
return 'Zombiepig';
}
sub getLang
{
return 'EN';
}
sub isPreferred
{
return 1;
}
sub getSearchCharset
{
my $self = shift;
# Need urls to be double character encoded
return "utf8";
}
sub getCharset
{
my $self = shift;
return "UTF-8";
}
sub decodeEntitiesWanted
{
return 0;
}
sub siteLanguage
{
my $self = shift;
return 'en';
}
sub convertCharset
{
my ($self, $value) = @_;
return $value;
}
sub siteLanguageCode
{
my $self = shift;
return 7;
}
}
1;
|