public void InitCache() { using (IDbConnection sqlconn = getConnection()) { var col = new System.Collections.Concurrent.ConcurrentDictionary <int, TVShowInfo>(); using (IDbCommand cmd = sqlconn.CreateCommand()) { cmd.CommandText = "SELECT * FROM TVShow;"; using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var tvshow = new TVShowInfo(); tvshow.ID = rdr.GetInt32(0); tvshow.Name = rdr.GetString(1); tvshow.LastUpdated = rdr.GetInt64(2); tvshow.IMDBID = rdr.GetString(3); tvshow.TVEpisodes = new List <TVEpisode>(); col.TryAdd(tvshow.ID, tvshow); } } } using (IDbCommand cmd = sqlconn.CreateCommand()) { cmd.CommandText = "SELECT * FROM TVSHowEntry;"; using (var rdr = cmd.ExecuteReader()) { while (rdr.Read()) { var tvshow = new TVEpisode(); var id = rdr.GetInt32(0); tvshow.Season = rdr.GetInt32(1); tvshow.Episode = rdr.GetInt32(2); tvshow.EpisodeId = rdr.GetInt32(3); tvshow.AbsoluteNumber = rdr.GetInt32(4); tvshow.Title = rdr.GetString(5); tvshow.FirstAired = DateTime.Parse(rdr.GetString(6)); var tv = col[id]; tv.TVEpisodes.Add(tvshow); TheTVDB.cacheshow.AddOrUpdate(tv.ID, tv, (key, oldvalue) => (oldvalue.LastUpdated > tv.LastUpdated ? oldvalue : tv)); } } } } }
public static TVShowInfo GetTVShowDetails(int showid, bool noncache = false) { TVShowInfo entry; if (!cacheshow.TryGetValue(showid, out entry) || noncache) { var url = String.Format("http://thetvdb.com/api/{1}/series/{0}/all/en.zip", showid, tvdbkey); byte[] xmlData; using (var wc = new System.Net.WebClient()) { xmlData = wc.DownloadData(url); } var xmlStream = new System.IO.MemoryStream(xmlData); ZipArchive archive = new ZipArchive(xmlStream, ZipArchiveMode.Read); foreach (var a in archive.Entries) { if (a.Name == "en.xml") { var memoryStream = new System.IO.MemoryStream(); var x = a.Open(); x.CopyTo(memoryStream); var t = memoryStream.ToArray(); string xmlStr = System.Text.Encoding.UTF8.GetString(t); var xmlDoc = new System.Xml.XmlDocument(); xmlDoc.LoadXml(xmlStr); entry = new TVShowInfo(); entry.ID = showid; entry.Name = xmlDoc.SelectSingleNode("//SeriesName").InnerText; entry.LastUpdated = System.Int64.Parse(xmlDoc.SelectSingleNode(".//lastupdated").InnerText); entry.IMDBID = xmlDoc.SelectSingleNode("//IMDB_ID").InnerText; entry.TVEpisodes = new List <TVEpisode>(); var airtime = xmlDoc.SelectSingleNode("//Airs_Time").InnerText; var episodes = xmlDoc.SelectNodes("//Episode"); foreach (XmlNode ep in episodes) { var seasoninfo = new TVEpisode(); seasoninfo.FirstAired = new System.DateTime(); var epnum = ep.SelectSingleNode(".//EpisodeNumber").InnerText; var seasonnum = ep.SelectSingleNode(".//SeasonNumber").InnerText; var title = ep.SelectSingleNode(".//EpisodeName").InnerText; var firstaired = ep.SelectSingleNode(".//FirstAired").InnerText; var absnumber = ep.SelectSingleNode(".//absolute_number").InnerText; DateTime faired; if (!String.IsNullOrEmpty(firstaired)) { if (DateTime.TryParse(firstaired + " " + airtime, out faired)) { seasoninfo.FirstAired = faired; } } seasoninfo.Episode = (int)Math.Ceiling(System.Double.Parse(epnum, new System.Globalization.CultureInfo("en-US"))); seasoninfo.Season = (int)Math.Ceiling(System.Double.Parse(seasonnum, new System.Globalization.CultureInfo("en-US"))); seasoninfo.Title = title; if (!string.IsNullOrEmpty(absnumber)) { seasoninfo.AbsoluteNumber = (System.Int32.Parse(absnumber, new System.Globalization.CultureInfo("en-US"))); } seasoninfo.EpisodeId = (System.Int32.Parse(ep.SelectSingleNode(".//EpisodeNumber").InnerText, new System.Globalization.CultureInfo("en-US"))); entry.TVEpisodes.Add(seasoninfo); } cacheshow.AddOrUpdate(showid, entry, (key, oldvalue) => (oldvalue.LastUpdated > entry.LastUpdated ? oldvalue : entry)); } } } return(entry); }
public static TVShowInfo GetTVShowDetails(int showid, bool noncache = false) { if (cacheshow.TryGetValue(showid, out var entry) && !noncache) { return(entry); } if (string.IsNullOrWhiteSpace(tvdbkey)) { return(null); } var url = string.Format("http://thetvdb.com/api/{1}/series/{0}/all/en.zip", showid, tvdbkey); byte[] xmlData; using (var wc = new WebClient()) xmlData = wc.DownloadData(url); var xmlStream = new MemoryStream(xmlData); var archive = new ZipArchive(xmlStream, ZipArchiveMode.Read); foreach (var zipEntry in archive.Entries) { if (zipEntry.Name == "en.xml") { var xmlDoc = new XmlDocument(); using (var memoryStream = new MemoryStream()) { var zipStream = zipEntry.Open(); zipStream.CopyTo(memoryStream); var textBytes = memoryStream.ToArray(); var xmlStr = Encoding.UTF8.GetString(textBytes); xmlDoc.LoadXml(xmlStr); } entry = new TVShowInfo { ID = showid, Name = xmlDoc.SelectSingleNode("//SeriesName").InnerText, LastUpdated = long.Parse(xmlDoc.SelectSingleNode(".//lastupdated").InnerText), IMDBID = xmlDoc.SelectSingleNode("//IMDB_ID").InnerText, TVEpisodes = new List <TVEpisode>() }; var airtime = xmlDoc.SelectSingleNode("//Airs_Time").InnerText; var episodes = xmlDoc.SelectNodes("//Episode"); foreach (XmlNode ep in episodes) { var seasoninfo = new TVEpisode(); seasoninfo.FirstAired = new DateTime(); var epnum = ep.SelectSingleNode(".//EpisodeNumber").InnerText; var seasonnum = ep.SelectSingleNode(".//SeasonNumber").InnerText; var title = ep.SelectSingleNode(".//EpisodeName").InnerText; var firstaired = ep.SelectSingleNode(".//FirstAired").InnerText; var absnumber = ep.SelectSingleNode(".//absolute_number").InnerText; if (!string.IsNullOrEmpty(firstaired)) { if (DateTime.TryParse(firstaired + " " + airtime, out var faired)) { seasoninfo.FirstAired = faired; } } seasoninfo.Episode = (int)Math.Ceiling(double.Parse(epnum, new CultureInfo("en-US"))); seasoninfo.Season = (int)Math.Ceiling(double.Parse(seasonnum, new CultureInfo("en-US"))); seasoninfo.Title = title; if (!string.IsNullOrEmpty(absnumber)) { seasoninfo.AbsoluteNumber = int.Parse(absnumber, new CultureInfo("en-US")); } seasoninfo.EpisodeId = int.Parse(ep.SelectSingleNode(".//EpisodeNumber").InnerText, new CultureInfo("en-US")); entry.TVEpisodes.Add(seasoninfo); } cacheshow.AddOrUpdate(showid, entry, (key, oldvalue) => oldvalue.LastUpdated > entry.LastUpdated ? oldvalue : entry); } } return(entry); }