/// <summary>
        /// Gets the first listed TVDB seriesId for a given show name
        /// </summary>
        /// <param name="showName"></param>
        /// <returns></returns>
        private TVDBSeriesResult GetSeriesForShow(string showName)
        {
            TVDBSeriesResult retval = null;

            //  The formatted API url to call
            string apiUrl = string.Format("http://thetvdb.com/api/GetSeries.php?seriesname={0}&apikey={1}", showName, this.APIKey);

            //  Call the API
            retval = GetAPIResponse <TVDBSeriesResult>(apiUrl);

            return(retval);
        }
        public TVEpisodeInfo GetShowInfo(string showName, int year, int month, int day)
        {
            TVEpisodeInfo retval = null;

            logger.Debug("Getting TVDB show information for: {0} date: {1}-{2}-{3}", showName, year, month, day);

            //  If we can't find it, throw an exception
            if (string.IsNullOrEmpty(this.APIKey))
            {
                throw new ApplicationException("Missing TheTVDB API key.  Please include the TheTVDB_APIKey in the application config");
                return(retval);
            }

            //  Get the seriesId for the show
            TVDBSeriesResult series = GetSeriesForShow(showName);

            //  If we were able to get the seriesId...
            if (series != null && series.SeriesInfo != null && series.SeriesInfo.SeriesId != null)
            {
                //  Get the episode information using the seriesId, airdate
                string apiUrl = string.Format("http://thetvdb.com/api/GetEpisodeByAirDate.php?apikey={0}&seriesid={1}&airdate={2}-{3}-{4}",
                                              this.APIKey,
                                              series.SeriesInfo.SeriesId,
                                              year,
                                              month,
                                              day);

                TVDBEpisodeResult response = GetAPIResponse <TVDBEpisodeResult>(apiUrl);

                if (response != null)
                {
                    retval = new TVEpisodeInfo()
                    {
                        EpisodeNumber   = response.EpisodeInfo.EpisodeNumber,
                        EpisodeSummary  = response.EpisodeInfo.EpisodeSummary,
                        EpisodeTitle    = response.EpisodeInfo.EpisodeName,
                        OriginalAirDate = response.EpisodeInfo.OriginalAirDate,
                        SeasonNumber    = response.EpisodeInfo.SeasonNumber,
                        ShowName        = series.SeriesInfo.SeriesName
                    };
                }
            }

            return(retval);
        }
        public TVEpisodeInfo GetShowInfo(string showName, int season, int episode)
        {
            TVEpisodeInfo retval = null;

            logger.Debug("Getting TVDB show information for: {0} season {1}, episode {2}", showName, season, episode);

            //  If we can't find it, throw an exception
            if (string.IsNullOrEmpty(this.APIKey))
            {
                throw new Exception("Missing TheTVDB API key.  Please include the TheTVDB_APIKey in the application config");
                return(retval);
            }

            //  Get the seriesId for the show
            TVDBSeriesResult series = GetSeriesForShow(showName);

            //  If we were able to get the seriesId...
            if (series != null && series.SeriesInfo != null && series.SeriesInfo.SeriesId != null)
            {
                //  Get the episode information using the API key, seriesId, season, episode
                string apiUrl = string.Format("http://thetvdb.com/api/{0}/series/{1}/default/{2}/{3}",
                                              this.APIKey,
                                              series.SeriesInfo.SeriesId,
                                              season,
                                              episode);

                TVDBEpisodeResult response = GetAPIResponse <TVDBEpisodeResult>(apiUrl);

                if (response != null)
                {
                    retval = new TVEpisodeInfo()
                    {
                        EpisodeNumber   = response.EpisodeInfo.EpisodeNumber,
                        EpisodeSummary  = response.EpisodeInfo.EpisodeSummary,
                        EpisodeTitle    = response.EpisodeInfo.EpisodeName,
                        OriginalAirDate = response.EpisodeInfo.OriginalAirDate,
                        SeasonNumber    = response.EpisodeInfo.SeasonNumber,
                        ShowName        = series.SeriesInfo.SeriesName
                    };
                }
            }

            return(retval);
        }
        /// <summary>
        /// Gets all show information, including all episodes for a show
        /// </summary>
        /// <param name="showname"></param>
        /// <returns></returns>
        public TVSeriesInfo GetSeriesInfo(string showname)
        {
            TVSeriesInfo retval = new TVSeriesInfo();

            //  Get the show information
            TVDBSeriesResult seriesResult = GetSeriesForShow(showname);
            TVDBSeriesInfo   series       = null;

            if (seriesResult != null)
            {
                series = seriesResult.SeriesInfo;
            }

            //  Get all episodes for the show:
            if (series != null)
            {
                //  Set basic show information:
                retval.Name = series.SeriesName;

                //  Get the language from the configuration file
                language = ConfigurationManager.AppSettings["TheTVDB_Language"] ?? "en";

                //  Construct the cache path
                string currentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
                cacheDirectory = ConfigurationManager.AppSettings["TheTVDB_CacheDir"] ?? "cache";
                cacheDirectory = Path.Combine(currentPath, cacheDirectory, series.SeriesId);

                //  Construct the cache filename
                string cacheFile = cacheDirectory + Path.DirectorySeparatorChar + "episodes.zip";

                //  Make sure the cache file exists:
                if (!Directory.Exists(cacheDirectory))
                {
                    //  If the directory doesn't exist, create it:
                    if (!Directory.Exists(cacheDirectory))
                    {
                        Directory.CreateDirectory(cacheDirectory);
                    }
                }

                //  Check to see if we have cached results for the series
                if (!File.Exists(cacheFile))
                {
                    //  Get the latest episode zip and save it to the cache path
                    GetEpisodeBundle(series, language, cacheFile);
                }

                //  If the cache file exists...
                if (File.Exists(cacheFile))
                {
                    XDocument doc = new XDocument();

                    //  Open it as a zipfile:
                    using (ZipArchive zip = ZipFile.Open(cacheFile, ZipArchiveMode.Read))
                    {
                        foreach (ZipArchiveEntry entry in zip.Entries)
                        {
                            if (entry.Name == language + ".xml")
                            {
                                //  Attempt to create an XDocument from it
                                doc = XDocument.Load(entry.Open());

                                break;
                            }
                        }
                    }

                    //  Using the XDocument return a list of TVEpisodeInfo's
                    var episodes = from item in doc.Descendants("Episode")
                                   select new TVEpisodeInfo()
                    {
                        EpisodeTitle    = item.Element("EpisodeName").Value,
                        EpisodeSummary  = item.Element("Overview").Value,
                        EpisodeNumber   = Convert.ToInt32(item.Element("EpisodeNumber").Value),
                        OriginalAirDate = DateTime.Parse(item.Element("FirstAired").Value),
                        SeasonNumber    = Convert.ToInt32(item.Element("SeasonNumber").Value),
                        ShowName        = series.SeriesName
                    };

                    retval.Seasons = from episode in episodes
                                     group episode by episode.SeasonNumber into seasonGroup
                                     orderby seasonGroup.Key
                                     select seasonGroup;
                }
            }

            return(retval);
        }
        /// <summary>
        /// Gets all show information, including all episodes for a show
        /// </summary>
        /// <param name="showname"></param>
        /// <returns></returns>
        public TVSeriesInfo GetSeriesInfo(string showname)
        {
            TVSeriesInfo retval = new TVSeriesInfo();

            //  Get the show information
            TVDBSeriesResult seriesResult = GetSeriesForShow(showname);
            TVDBSeriesInfo   series       = null;

            if (seriesResult != null)
            {
                series = seriesResult.SeriesInfo;
            }

            //  Get all episodes for the show:
            if (series != null)
            {
                //  Set basic show information:
                retval.Name = series.SeriesName;

                //  Get the language from the configuration file
                language = "en";

                //  Construct the cache path
                string currentPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
                cacheDirectory = "cache";
                cacheDirectory = Path.Combine(currentPath, cacheDirectory, series.SeriesId);

                //  Construct the cache filename
                string cacheFile = cacheDirectory + Path.DirectorySeparatorChar + "episodes.zip";

                //  Make sure the cache file exists:
                if (!Directory.Exists(cacheDirectory))
                {
                    //  If the directory doesn't exist, create it:
                    if (!Directory.Exists(cacheDirectory))
                    {
                        Directory.CreateDirectory(cacheDirectory);
                    }
                }

                //  Check to see if we have cached results for the series
                if (!File.Exists(cacheFile))
                {
                    //  Get the latest episode zip and save it to the cache path
                    GetEpisodeBundle(series, language, cacheFile);
                }

                //  If the cache file exists...
                if (File.Exists(cacheFile))
                {
                    XDocument doc = new XDocument();

                    //  Open it as a zipfile:
                    using (ZipArchive zip = ZipFile.Open(cacheFile, ZipArchiveMode.Read))
                    {
                        foreach (ZipArchiveEntry entry in zip.Entries)
                        {
                            if (entry.Name == language + ".xml")
                            {
                                //  Attempt to create an XDocument from it
                                doc = XDocument.Load(entry.Open());

                                break;
                            }
                        }
                    }

                    //  Using the XDocument return a list of TVEpisodeInfo's
                    var episodes = (from item in doc.Descendants("Episode")
                                    select GetEpisode(item, series.SeriesName)).Where(e => e != null);

                    retval.Seasons = from episode in episodes
                                     group episode by episode.SeasonNumber into seasonGroup
                                     orderby seasonGroup.Key
                                     select seasonGroup;
                }
            }

            return(retval);
        }