Example #1
0
        /// <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");
            }
              }
        }
Example #2
0
        /// <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");
            }
              }
        }