-
Notifications
You must be signed in to change notification settings - Fork 0
/
TvdbDownloader.cs
1256 lines (1196 loc) · 56.7 KB
/
TvdbDownloader.cs
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* TvdbLib: A library to retrieve information and media from http://thetvdb.com
*
* Copyright (C) 2008 Benjamin Gmeiner
*
* This program 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 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using TvdbLib.Exceptions;
using TvdbLib.ICSharpCode.SharpZipLib.Zip;
using TvdbLib.SharpZipLib.Zip;
using TvdbLib.Xml;
using TvdbLib.Data;
namespace TvdbLib
{
/// <summary>
/// TvdbDownloader allows simple downloading of all informations stored
/// on http://thetvdb.com. Unlike the class Tvdb TvdbDownloader doesn't
/// include any logic like caching.
/// </summary>
public class TvdbDownloader
{
#region private properties
private String m_apiKey;
private WebClient m_webClient;
private TvdbXmlReader m_xmlHandler;
#endregion
/// <summary>
/// TvdbDownloader constructor
/// </summary>
/// <param name="_apiKey">The api key used for downloading data from thetvdb -> see http://thetvdb.com/wiki/index.php/Programmers_API</param>
public TvdbDownloader(String _apiKey)
{
m_apiKey = _apiKey;
m_webClient = new WebClient();//initialise webclient for downloading xml files
m_webClient.Encoding = Encoding.UTF8;
m_xmlHandler = new TvdbXmlReader();//xml handler (extract xml information into objects)
}
/// <summary>
/// Download the episodes for the given series
/// </summary>
/// <param name="_seriesId">the id of the series</param>
/// <param name="_language">the language in which the episodes should be downloaded</param>
/// <returns>An episode object or null if no episodes could be found</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
public List<TvdbEpisode> DownloadEpisodes(int _seriesId, TvdbLanguage _language)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateSeriesEpisodesLink(m_apiKey, _seriesId, _language);
xml = m_webClient.DownloadString(link);
List<TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(xml);
return epList;
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve episodes fo " + _seriesId +
", you may use an invalid api key or the series doesn't exists");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve episodes for" + _seriesId +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// <para>Download all available banners (only a list of available banners, not the actual images!)for the specified series.</para>
/// <para>You can load the actual images by calling LoadBanner() (or LoadThumb(), LoadVignette()) on the banner object</para>
/// </summary>
/// <param name="_seriesId">Id of series</param>
/// <returns>List of all banners for the given series</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
public List<TvdbBanner> DownloadBanners(int _seriesId)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateSeriesBannersLink(m_apiKey, _seriesId);
xml = m_webClient.DownloadString(link);
List<TvdbBanner> banners = m_xmlHandler.ExtractBanners(xml);
return banners;
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve banners fo " + _seriesId +
", you may use an invalid api key or the series doesn't exists");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve banners for" + _seriesId +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// <para>Download series from tvdb (specified by series id and language)</para>
/// </summary>
/// <param name="_seriesId">id of series</param>
/// <param name="_language">language of series</param>
/// <param name="_loadEpisodes">load episodes</param>
/// <param name="_loadActors">load actors</param>
/// <param name="_loadBanners">load banners</param>
/// <returns>The series object or null if the series couldn't be found</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
public TvdbSeries DownloadSeries(int _seriesId, TvdbLanguage _language, bool _loadEpisodes, bool _loadActors, bool _loadBanners)
{
//download the xml data from this request
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateSeriesLink(m_apiKey, _seriesId, _language, _loadEpisodes, false);
xml = m_webClient.DownloadString(link);
//extract all series the xml file contains
List<TvdbSeries> seriesList = m_xmlHandler.ExtractSeries(xml);
//if a request is made on a series id, one and only one result
//should be returned, otherwise there obviously was an error
if (seriesList != null && seriesList.Count == 1)
{
TvdbSeries series = seriesList[0];
if (_loadEpisodes)
{
//add episode info to series
List<TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(xml);
if (epList != null)
{
foreach (KeyValuePair<TvdbLanguage, TvdbSeriesFields> kvp in series.SeriesTranslations)
{
if (kvp.Key.Abbriviation.Equals(_language.Abbriviation))
{
series.SeriesTranslations[kvp.Key].Episodes = epList;
series.SeriesTranslations[kvp.Key].EpisodesLoaded = true;
series.SetLanguage(_language);
break;
}
}
}
}
//also load actors
if (_loadActors)
{
List<TvdbActor> actors = DownloadActors(_seriesId);
if (actors != null)
{
series.TvdbActorsLoaded = true;
series.TvdbActors = actors;
}
}
//also load banner paths
if (_loadBanners)
{
List<TvdbBanner> banners = DownloadBanners(_seriesId);
if (banners != null)
{
series.Banners = banners;
series.BannersLoaded = true;
}
}
return series;
}
else
{
Log.Warn("More than one series returned when trying to retrieve series " + _seriesId);
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
", you may use an invalid api key or the series doesn't exists");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Download the series in the given language
/// </summary>
/// <param name="_seriesId">id of series</param>
/// <param name="_language">language of series</param>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
/// <returns>the series object</returns>
public TvdbSeries DownloadSeriesZipped(int _seriesId, TvdbLanguage _language)
{
//download the xml data from this request
byte[] xml = null;
String link = "";
try
{
link = TvdbLinkCreator.CreateSeriesLinkZipped(m_apiKey, _seriesId, _language);
xml = m_webClient.DownloadData(link);
ZipInputStream zip = new ZipInputStream(new MemoryStream(xml));
ZipEntry entry = zip.GetNextEntry();
String seriesString = null;
String actorsString = null;
String bannersString = null;
while (entry != null)
{
Log.Debug("Extracting " + entry.Name);
byte[] buffer = new byte[zip.Length];
int count = zip.Read(buffer, 0, (int)zip.Length);
if (entry.Name.Equals(_language.Abbriviation + ".xml"))
{
seriesString = Encoding.UTF8.GetString(buffer);
}
else if (entry.Name.Equals("banners.xml"))
{
bannersString = Encoding.UTF8.GetString(buffer);
}
else if (entry.Name.Equals("actors.xml"))
{
actorsString = Encoding.UTF8.GetString(buffer);
}
entry = zip.GetNextEntry();
}
zip.Close();
//extract all series the xml file contains
List<TvdbSeries> seriesList = m_xmlHandler.ExtractSeries(seriesString);
//if a request is made on a series id, one and only one result
//should be returned, otherwise there obviously was an error
if (seriesList != null && seriesList.Count == 1)
{
TvdbSeries series = seriesList[0];
//add episode info to series
List<TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(seriesString);
if (epList != null)
{
foreach (KeyValuePair<TvdbLanguage, TvdbSeriesFields> kvp in series.SeriesTranslations)
{
if (kvp.Key.Abbriviation.Equals(_language.Abbriviation))
{
series.SeriesTranslations[kvp.Key].Episodes = epList;
series.SeriesTranslations[kvp.Key].EpisodesLoaded = true;
series.SetLanguage(_language);
break;
}
}
}
//also load actors
List<TvdbActor> actors = m_xmlHandler.ExtractActors(actorsString);
if (actors != null)
{
series.TvdbActorsLoaded = true;
series.TvdbActors = actors;
}
//also load banner paths
List<TvdbBanner> banners = m_xmlHandler.ExtractBanners(bannersString);
if (banners != null)
{
series.BannersLoaded = true;
series.Banners = banners;
}
return series;
}
else
{
Log.Warn("More than one series returned when trying to retrieve series " + _seriesId);
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + Encoding.Unicode.GetString(xml), ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
", you may an invalid api key or the series doesn't exists");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Download a series search for the id of an external site
/// </summary>
/// <param name="_site">The site that provides the external id</param>
/// <param name="_id">The id that identifies the series on the external site</param>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
/// <returns>the series object that corresponds to the given site and id</returns>
public TvdbSearchResult DownloadSeriesSearchByExternalId(ExternalId _site, String _id)
{
//download the xml data from this request
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateGetSeriesByIdLink(m_apiKey, _site, _id);
xml = m_webClient.DownloadString(link);
//extract all series the xml file contains
List<TvdbSearchResult> seriesList = m_xmlHandler.ExtractSeriesSearchResults(xml);
//if a request is made on a series id, one and only one result
//should be returned, otherwise there obviously was an error
if (seriesList != null && seriesList.Count == 1)
{
TvdbSearchResult series = seriesList[0];
return series;
}
else
{
Log.Warn("More than one series returned when trying to retrieve series by id " + _id);
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve " + _id +
", you may use an invalid api key or the series doesn't exists");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _id +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
internal TvdbSeriesFields DownloadSeriesFields(int _seriesId, TvdbLanguage _language)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateSeriesLink(m_apiKey, _seriesId, _language, false, false);
xml = m_webClient.DownloadString(link);
//extract all series the xml file contains
List<TvdbSeriesFields> seriesList = m_xmlHandler.ExtractSeriesFields(xml);
if (seriesList != null && seriesList.Count == 1)
{
return seriesList[0];
}
else
{
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
", you may use an invalid api key or the series doesn't exists");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Download the given episode from tvdb
/// </summary>
/// <param name="_episodeId">Id of episode</param>
/// <param name="_language">Language in which the episode should be downloaded</param>
/// <returns>The episode object</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbContentNotFoundException">The episode/series/banner couldn't be located on the tvdb server.</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public TvdbEpisode DownloadEpisode(int _episodeId, TvdbLanguage _language)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateEpisodeLink(m_apiKey, _episodeId, _language, false);
xml = m_webClient.DownloadString(link);
List<TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(xml);
if (epList != null && epList.Count == 1)
{
return epList[0];
}
else
{
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbContentNotFoundException("Couldn't download episode " + _episodeId + "(" + _language +
"), maybe the episode doesn't exist");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _episodeId +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// <para>Download the episode (specified by series id, season number, episode number, language and episode order) from http://thetvdb.com.</para>
/// <para>It is possible to retrieve episodes by aired order (aka default order), DVD order and absolute order. For a detailled description of these
/// options see: http://thetvdb.com/wiki/index.php/Category:Episodes</para>
/// </summary>
/// <param name="_seriesId">series id</param>
/// <param name="_seasonNr">season nr</param>
/// <param name="_episodeNr">episode nr</param>
/// <param name="_language">language</param>
/// <param name="_order">order</param>
/// <returns>The episode object or null if the episode could't be found</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbContentNotFoundException">The episode/series/banner couldn't be located on the tvdb server.</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public TvdbEpisode DownloadEpisode(int _seriesId, int _seasonNr, int _episodeNr, TvdbEpisode.EpisodeOrdering _order, TvdbLanguage _language)
{
String xml = "";
String link = "";
String order = null;
switch (_order)
{
case TvdbEpisode.EpisodeOrdering.AbsoluteOrder:
order = "absolute";
break;
case TvdbEpisode.EpisodeOrdering.DefaultOrder:
order = "default";
break;
case TvdbEpisode.EpisodeOrdering.DvdOrder:
order = "dvd";
break;
}
try
{
link = TvdbLinkCreator.CreateEpisodeLink(m_apiKey, _seriesId, _seasonNr, _episodeNr, order, _language);
xml = m_webClient.DownloadString(link);
List<TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(xml);
if (epList != null && epList.Count == 1)
{
return epList[0];
}
else
{
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbContentNotFoundException("Couldn't download episode " + _seriesId + "/" +
_order + "/" + _seasonNr + "/" + _episodeNr + "/" + _language.Abbriviation +
", maybe the episode or the ordering doesn't exist");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId + "/" +
_order + "/" + _seasonNr + "/" + _episodeNr + "/" + _language.Abbriviation +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Download the episode specified from http://thetvdb.com
/// </summary>
/// <param name="_seriesId">series id</param>
/// <param name="_airDate">when did the episode air</param>
/// <param name="_language">language</param>
/// <returns>Episode</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbContentNotFoundException">The episode/series/banner couldn't be located on the tvdb server.</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public TvdbEpisode DownloadEpisode(int _seriesId, DateTime _airDate, TvdbLanguage _language)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateEpisodeLink(m_apiKey, _seriesId, _airDate, _language);
xml = m_webClient.DownloadString(link);
if (!xml.Contains("No Results from SP"))
{
List<TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(xml);
if (epList != null && epList.Count == 1)
{
epList[0].Banner.SeriesId = _seriesId;
return epList[0];
}
else
{
return null;
}
}
else
{
return null;
}
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbContentNotFoundException("Couldn't download episode for series " + _seriesId + " from " +
_airDate.ToShortDateString() + "(" + _language.Abbriviation +
"), maybe the episode doesn't exist");
}
else
{
throw new TvdbNotAvailableException("Couldn't download episode for series " + _seriesId + " from " +
_airDate.ToShortDateString() + "(" + _language.Abbriviation +
"), maybe the episode doesn't exist");
}
}
}
/// <summary>
/// Download the preferred language of the user.
/// </summary>
/// <param name="_userId">Id of user</param>
/// <returns>The preferred language for this user as set on http://thetvdb.com</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbUserNotFoundException">The user doesn't exist</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
public TvdbLanguage DownloadUserPreferredLanguage(String _userId)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateUserLanguageLink(_userId);
xml = m_webClient.DownloadString(link);
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbUserNotFoundException("Couldn't connect to Thetvdb.com to retrieve preferred language for user " + _userId +
", are you sure this is the correct user id?");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve preferred languae for user " + _userId +
", check your internet connection and the status of http://thetvdb.com");
}
}
List<TvdbLanguage> langList = m_xmlHandler.ExtractLanguages(xml);
if (langList != null && langList.Count == 1)
{
return langList[0];
}
return null;
}
/// <summary>
/// Download the user favorite list
/// </summary>
/// <param name="_userId">Id of user (register at http://thetvdb.com to get a user id)</param>
/// <returns>Favorite list for specified user</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbUserNotFoundException">The user doesn't exist</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
public List<int> DownloadUserFavoriteList(String _userId)
{
return DownloadUserFavoriteList(_userId, Util.UserFavouriteAction.none, 0);
}
/// <summary>
/// Download the user favorite list
/// </summary>
/// <param name="_userId">Id of user</param>
/// <param name="_type">Type of action</param>
/// <param name="_seriesId">id of series</param>
/// <returns>List of user favorites</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbUserNotFoundException">The user doesn't exist</exception>
/// <exception cref="TvdbNotAvailableException">The tvdb database is unavailable</exception>
internal List<int> DownloadUserFavoriteList(String _userId, Util.UserFavouriteAction _type, int _seriesId)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateUserFavouriteLink(_userId, _type, _seriesId);
xml = m_webClient.DownloadString(link);
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbUserNotFoundException("Couldn't connect to Thetvdb.com to retrieve favorite list for user " + _userId +
", are you sure this is the correct user id?");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve favorite list for user " + _userId +
", check your internet connection and the status of http://thetvdb.com");
}
}
List<int> favList = m_xmlHandler.ExtractSeriesFavorites(xml);
return favList;
}
/// <summary>
/// Download an Update
/// </summary>
/// <param name="_updateSeries">updated series to return</param>
/// <param name="_updateEpisodes">updated episodes to return</param>
/// <param name="_updateBanners">updated banners to return</param>
/// <param name="_interval">interval to download (0=day, 1=week, 2=month)</param>
/// <param name="_zipped">use zip</param>
/// <returns>Time of the update</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public DateTime DownloadUpdate(out List<TvdbSeries> _updateSeries, out List<TvdbEpisode> _updateEpisodes,
out List<TvdbBanner> _updateBanners, int _interval,
bool _zipped)
{
return DownloadUpdate(out _updateSeries, out _updateEpisodes, out _updateBanners, (Interval)_interval, _zipped);
}
/// <summary>
/// Download an Update
/// </summary>
/// <param name="_updateSeries">updated series to return</param>
/// <param name="_updateEpisodes">updated episodes to return</param>
/// <param name="_updateBanners">updated banners to return</param>
/// <param name="_interval">interval to download</param>
/// <param name="_zipped">use zip</param>
/// <returns>Time of the update</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public DateTime DownloadUpdate(out List<TvdbSeries> _updateSeries, out List<TvdbEpisode> _updateEpisodes,
out List<TvdbBanner> _updateBanners, Interval _interval, bool _zipped)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateUpdateLink(m_apiKey, _interval, _zipped);
if (_zipped)
{
byte[] data = m_webClient.DownloadData(link);
ZipInputStream zip = new ZipInputStream(new MemoryStream(data));
zip.GetNextEntry();
byte[] buffer = new byte[zip.Length];
int count = zip.Read(buffer, 0, (int)zip.Length);
xml = Encoding.UTF8.GetString(buffer);
}
else
{
xml = m_webClient.DownloadString(link);
}
_updateEpisodes = m_xmlHandler.ExtractEpisodeUpdates(xml);
_updateSeries = m_xmlHandler.ExtractSeriesUpdates(xml);
_updateBanners = m_xmlHandler.ExtractBannerUpdates(xml);
return m_xmlHandler.ExtractUpdateTime(xml);
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (ZipException ex)
{
Log.Error("Error unzipping the xml file " + link, ex);
throw new TvdbInvalidXmlException("Error unzipping the xml file " + link);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve updates for " + _interval +
", you may use an invalid api key");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve updates for " + _interval +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Download list available languages.
/// </summary>
/// <returns>A list of TvdbLanguage objects</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbContentNotFoundException">The episode/series/banner couldn't be located on the tvdb server.</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public List<TvdbLanguage> DownloadLanguages()
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateLanguageLink(m_apiKey);
xml = m_webClient.DownloadString(link);
return m_xmlHandler.ExtractLanguages(xml);
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve the list of available languages" +
", you may use an invalid api key");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve the list of available languages" +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Download search results for a series search in the default language (english)
/// </summary>
/// <param name="_name">name of the series</param>
/// <returns>List of possible matches for the search</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public List<TvdbSearchResult> DownloadSearchResults(String _name)
{
return DownloadSearchResults(_name, TvdbLanguage.DefaultLanguage);
}
/// <summary>
/// Download search results for a series search
/// </summary>
/// <param name="_name">name of the series</param>
/// <param name="_language">language of the search</param>
/// <returns>List of possible matches for the search</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbInvalidApiKeyException">The stored api key is invalid</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public List<TvdbSearchResult> DownloadSearchResults(String _name, TvdbLanguage _language)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateSearchLink(_name, _language);
xml = m_webClient.DownloadString(link);
return m_xmlHandler.ExtractSeriesSearchResults(xml);
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbInvalidApiKeyException("Couldn't connect to Thetvdb.com to retrieve search results for " + _name +
", you may use an invalid api key");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve search results for " + _name +
", check your internet connection and the status of http://thetvdb.com");
}
}
}
/// <summary>
/// Make the request for rating a series
/// </summary>
/// <param name="_userId">The id of the user</param>
/// <param name="_seriesId">The id of the series</param>
/// <param name="_rating">The rating for this series</param>
/// <returns>A double value with the current rating for this series</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>
/// <para>Feel free to post a detailed description of this issue on http://code.google.com/p/tvdblib
/// or http://forums.thetvdb.com/</para></exception>
/// <exception cref="TvdbUserNotFoundException">The user doesn't exist</exception>
/// <exception cref="TvdbNotAvailableException">Exception is thrown when thetvdb isn't available.</exception>
public double RateSeries(String _userId, int _seriesId, int _rating)
{
String xml = "";
String link = "";
try
{
link = TvdbLinkCreator.CreateUserSeriesRating(_userId, _seriesId, _rating);
xml = m_webClient.DownloadString(link);
}
catch (XmlException ex)
{
Log.Error("Error parsing the xml file " + link + "\n\n" + xml, ex);
throw new TvdbInvalidXmlException("Error parsing the xml file " + link + "\n\n" + xml);
}
catch (WebException ex)
{
Log.Warn("Request not successfull", ex);
if (ex.Message.Equals("The remote server returned an error: (404) Not Found."))
{
throw new TvdbUserNotFoundException("Couldn't connect to Thetvdb.com to rate series " + _seriesId +
", you may use an invalid user id.");
}
else
{
throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to rate series " + _seriesId +
", check your internet connection and the status of http://thetvdb.com");
}
}
return m_xmlHandler.ExtractRating(xml);
}
/// <summary>
/// Make the request for rating an episode
/// </summary>
/// <param name="_userId">The id of the user</param>
/// <param name="_episodeId">The id of the episode</param>
/// <param name="_rating">The rating for this series</param>
/// <returns>A double value with the current rating for this series</returns>
/// <exception cref="TvdbInvalidXmlException"><para>Exception is thrown when there was an error parsing the xml files. </para>