/// <summary> /// For a given series and language, download the zip file containing all episode information /// to the specified file /// </summary> /// <param name="series"></param> /// <param name="language"></param> /// <param name="cacheFile">The file to use to cache the episode information</param> /// <returns></returns> private void GetEpisodeBundle(TVDBSeriesInfo series, string language, string cacheFile) { // Construct the url path string url = string.Format("http://thetvdb.com/api/{0}/series/{1}/all/{2}.zip", this.APIKey, series.SeriesId, language ); // Attempt to download the file: try { HttpClient webClient = new HttpClient(); File.WriteAllBytes(cacheFile, webClient.GetByteArrayAsync(url).Result); } catch (Exception ex) { logger.Error(ex, "There was a problem downloading the list of episodes"); } }
/// <summary> /// For a given series and language, download the zip file containing all episode information /// to the specified file /// </summary> /// <param name="series"></param> /// <param name="language"></param> /// <param name="cacheFile">The file to use to cache the episode information</param> /// <returns></returns> private void GetEpisodeBundle(TVDBSeriesInfo series, string language, string cacheFile) { // Construct the url path string url = string.Format("http://thetvdb.com/api/{0}/series/{1}/all/{2}.zip", this.APIKey, series.SeriesId, language ); // Attempt to download the file: try { WebClient webClient = new WebClient(); webClient.Encoding = Encoding.UTF8; webClient.DownloadFile(url, cacheFile); } catch (Exception ex) { logger.ErrorException("There was a problem downloading the list of episodes", ex); } }
/// <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); }