Exemplo n.º 1
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">The Xml of the downloaded series is invalid</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");
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load the give series from cache
        /// </summary>
        /// <param name="_seriesId">Id of the series to load</param>
        /// <returns>Series that has been loaded or null if series doesn't exist</returns>
        public TvdbSeries LoadSeriesFromCache(int _seriesId)
        {
            String seriesRoot = m_rootFolder + Path.DirectorySeparatorChar + _seriesId;

            if (!Directory.Exists(seriesRoot))
            {
                return(null);
            }
            TvdbSeries series = new TvdbSeries();

            #region load series in all available languages
            String[] seriesLanguages = Directory.GetFiles(seriesRoot, "*.xml");
            foreach (String l in seriesLanguages)
            {
                if (!l.EndsWith("actors.xml") && !l.EndsWith("banners.xml"))
                {
                    String content = File.ReadAllText(l);
                    List <TvdbSeriesFields> seriesList = m_xmlReader.ExtractSeriesFields(content);
                    if (seriesList != null && seriesList.Count == 1)
                    {
                        TvdbSeriesFields s = seriesList[0];
                        //Load episodes
                        if (l.EndsWith("full.xml"))
                        {
                            List <TvdbEpisode> epList = m_xmlReader.ExtractEpisodes(content);
                            s.EpisodesLoaded = true;
                            s.Episodes.Clear();
                            s.Episodes.AddRange(epList);
                        }
                        series.AddLanguage(s);
                    }
                }
            }



            if (series.SeriesTranslations.Count > 0)
            {//change language of the series to the default language
                series.SetLanguage(series.SeriesTranslations.Keys.First());
            }
            else
            {//no series info could be loaded
                return(null);
            }

            if (!series.BannerPath.Equals(""))
            {
                series.Banners.Add(new TvdbSeriesBanner(series.Id, series.BannerPath, series.Language, TvdbSeriesBanner.Type.graphical));
            }

            if (!series.PosterPath.Equals(""))
            {
                series.Banners.Add(new TvdbPosterBanner(series.Id, series.PosterPath, series.Language));
            }

            if (!series.FanartPath.Equals(""))
            {
                series.Banners.Add(new TvdbFanartBanner(series.Id, series.FanartPath, series.Language));
            }

            Regex rex = new Regex("S(\\d+)E(\\d+)");
            if (Directory.Exists(seriesRoot + Path.DirectorySeparatorChar + "EpisodeImages"))
            {
                String[] episodeFiles = Directory.GetFiles(seriesRoot + Path.DirectorySeparatorChar + "EpisodeImages", "ep_*.jpg");
                foreach (String epImageFile in episodeFiles)
                {
                    try
                    {
                        Match match   = rex.Match(epImageFile);
                        int   season  = Int32.Parse(match.Groups[1].Value);
                        int   episode = Int32.Parse(match.Groups[2].Value);
                        foreach (TvdbEpisode e in series.Episodes)
                        {
                            if (e.SeasonNumber == season && e.EpisodeNumber == episode)
                            {
                                if (epImageFile.Contains("thumb"))
                                {
                                    e.Banner.LoadThumb(Image.FromFile(epImageFile));
                                }
                                else
                                {
                                    e.Banner.LoadBanner(Image.FromFile(epImageFile));
                                }
                                break;
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Log.Warn("Couldn't load episode image file " + epImageFile);
                    }
                }
            }

            #endregion

            #region Banner loading
            String bannerFile = seriesRoot + Path.DirectorySeparatorChar + "banners.xml";
            //load cached banners
            if (File.Exists(bannerFile))
            {//banners have been already loaded
                List <TvdbBanner> bannerList = m_xmlReader.ExtractBanners(File.ReadAllText(bannerFile));

                String[] banners = Directory.GetFiles(seriesRoot, "banner*.jpg");
                foreach (String b in banners)
                {
                    try
                    {
                        int bannerId = Int32.Parse(b.Remove(b.IndexOf(".")).Remove(0, b.LastIndexOf("_") + 1));
                        foreach (TvdbBanner banner in bannerList)
                        {
                            if (banner.Id == bannerId)
                            {
                                if (b.Contains("thumb") && banner.GetType().BaseType == typeof(TvdbBannerWithThumb))
                                {
                                    ((TvdbBannerWithThumb)banner).LoadThumb(Image.FromFile(b));
                                }
                                else if (b.Contains("vignette") && banner.GetType() == typeof(TvdbFanartBanner))
                                {
                                    ((TvdbFanartBanner)banner).LoadVignette(Image.FromFile(b));
                                }
                                else
                                {
                                    banner.LoadBanner(Image.FromFile(b));
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Log.Warn("Couldn't load image file " + b);
                    }
                }
                series.Banners = bannerList;
            }
            #endregion

            #region actor loading
            //load actor info
            String actorFile = seriesRoot + Path.DirectorySeparatorChar + "actors.xml";
            if (File.Exists(actorFile))
            {
                List <TvdbActor> actorList = m_xmlReader.ExtractActors(File.ReadAllText(actorFile));

                String[] banners = Directory.GetFiles(seriesRoot, "actor_*.jpg");
                foreach (String b in banners)
                {
                    try
                    {
                        int actorId = Int32.Parse(b.Remove(b.IndexOf(".")).Remove(0, b.LastIndexOf("_") + 1));
                        foreach (TvdbActor actor in actorList)
                        {
                            if (actor.Id == actorId)
                            {
                                actor.ActorImage.LoadBanner(Image.FromFile(b));
                            }
                        }
                    }
                    catch (Exception)
                    {
                        Log.Warn("Couldn't load image file " + b);
                    }
                }
                series.TvdbActors = actorList;
            }
            #endregion

            return(series);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Load the give series from cache
        /// </summary>
        /// <param name="_seriesId"></param>
        /// <returns></returns>
        public TvdbSeries LoadSeriesFromCache(int _seriesId)
        {
            String seriesRoot = m_rootFolder + "\\" + _seriesId;
            String xmlFile    = seriesRoot + "\\all.xml";

            if (!File.Exists(xmlFile))
            {
                return(null);
            }
            String            content    = File.ReadAllText(xmlFile);
            List <TvdbSeries> seriesList = m_xmlReader.ExtractSeries(content);

            if (seriesList != null && seriesList.Count == 1)
            {
                TvdbSeries         series = seriesList[0];
                List <TvdbEpisode> epList = m_xmlReader.ExtractEpisodes(content);
                series.Episodes = epList;
                String bannerFile = seriesRoot + "\\banners.xml";
                String actorFile  = seriesRoot + "\\actors.xml";
                Regex  rex        = new Regex("S(\\d+)E(\\d+)");


                if (Directory.Exists(seriesRoot + "\\EpisodeImages"))
                {
                    String[] episodeFiles = Directory.GetFiles(seriesRoot + "\\EpisodeImages", "*.jpg");
                    foreach (String epImageFile in episodeFiles)
                    {
                        try
                        {
                            Match match   = rex.Match(epImageFile);
                            int   season  = Int32.Parse(match.Groups[1].Value);
                            int   episode = Int32.Parse(match.Groups[2].Value);
                            foreach (TvdbEpisode e in series.Episodes)
                            {
                                if (e.SeasonNumber == season && e.EpisodeNumber == episode)
                                {
                                    e.Banner.LoadBanner(Image.FromFile(epImageFile));
                                    break;
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Warn("Couldn't load episode image file " + epImageFile);
                        }
                    }
                }
                //load cached banners
                if (File.Exists(bannerFile))
                {//banners have been already loaded
                    List <TvdbBanner> bannerList = m_xmlReader.ExtractBanners(File.ReadAllText(bannerFile));

                    String[] banners = Directory.GetFiles(seriesRoot, "banner*.jpg");
                    foreach (String b in banners)
                    {
                        try
                        {
                            int bannerId = Int32.Parse(b.Remove(b.IndexOf(".")).Remove(0, b.LastIndexOf("_") + 1));
                            foreach (TvdbBanner banner in bannerList)
                            {
                                if (banner.Id == bannerId)
                                {
                                    if (b.Contains("thumb") && banner.GetType() == typeof(TvdbFanartBanner))
                                    {
                                        ((TvdbFanartBanner)banner).LoadThumb(Image.FromFile(b));
                                    }
                                    else if (b.Contains("vignette") && banner.GetType() == typeof(TvdbFanartBanner))
                                    {
                                        ((TvdbFanartBanner)banner).LoadVignette(Image.FromFile(b));
                                    }
                                    else
                                    {
                                        banner.LoadBanner(Image.FromFile(b));
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                            Log.Warn("Couldn't load image file " + b);
                        }
                    }
                    series.Banners = bannerList;
                }

                //load actor info
                if (File.Exists(actorFile))
                {
                    List <TvdbActor> actorList = m_xmlReader.ExtractActors(File.ReadAllText(actorFile));

                    String[] banners = Directory.GetFiles(seriesRoot, "actor_*.jpg");
                    foreach (String b in banners)
                    {
                        try
                        {
                            int actorId = Int32.Parse(b.Remove(b.IndexOf(".")).Remove(0, b.LastIndexOf("_") + 1));
                            foreach (TvdbActor actor in actorList)
                            {
                                if (actor.Id == actorId)
                                {
                                    actor.ActorImage.LoadBanner(Image.FromFile(b));
                                }
                            }
                        }
                        catch (Exception)
                        {
                            Log.Warn("Couldn't load image file " + b);
                        }
                    }
                    series.TvdbActors = actorList;
                }

                return(series);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Download the list of actors
        /// </summary>
        /// <param name="_seriesId"></param>
        /// <returns></returns>
        internal List <TvdbActor> DownloadActors(int _seriesId)
        {
            String xml = m_webClient.DownloadString(TvdbLinks.CreateActorLink(_seriesId, m_apiKey));

            return(m_xmlHandler.ExtractActors(xml));
        }
Exemplo n.º 5
0
        internal TvdbSeries DownloadSeriesZipped(int _seriesId, TvdbLanguage _language)
        {
            //download the xml data from this request
            byte[] data;
            try
            {
                data = m_webClient.DownloadData(TvdbLinks.CreateSeriesLinkZipped(m_apiKey, _seriesId, _language));
            }
            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 +
                                                         ", it seems like you have an invalid api key (" + m_apiKey + ")");
                }
                else
                {
                    throw new TvdbNotAvailableException("Couldn't connect to Thetvdb.com to retrieve " + _seriesId +
                                                        ", check your internet connection and the status of http://thetvdb.com");
                }
            }

            ZipInputStream zip = new ZipInputStream(new MemoryStream(data));

            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];
                series.EpisodesLoaded = true;
                series.Episodes       = new List <TvdbEpisode>();
                //add episode info to series
                List <TvdbEpisode> epList = m_xmlHandler.ExtractEpisodes(seriesString);
                foreach (TvdbEpisode e in epList)
                {
                    Util.AddEpisodeToSeries(e, series);
                }

                //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);
            }
        }