/// <summary> Try several approaches to find the show. </summary> protected static TVMaze_Show GetShowName2(string fileShowName) { // after each failure, lop off the end of the name and try again string slowlyReducingFileName = fileShowName.Trim(); while (slowlyReducingFileName.Length > 0) { CacheItem singlesearchCache = StrafeForm.Cache.Get("http://api.tvmaze.com/singlesearch/shows?q=" + slowlyReducingFileName); JSONResponse singlesearch = singlesearchCache.JSONResponse; if (singlesearch.HTTPStatus == HttpStatusCode.OK) { return(new TVMaze_Show(singlesearch.JSON)); } if (singlesearch.HTTPStatus != HttpStatusCode.NotFound) { StrafeForm.Log("Unknown TVMaze http status: " + singlesearch.HTTPStatus); throw new TVMazeException("TVMaze: unknown error"); } int lastSpace = slowlyReducingFileName.LastIndexOf(' '); slowlyReducingFileName = lastSpace >= 0 ? slowlyReducingFileName.Substring(0, lastSpace).Trim() : ""; } StrafeForm.Log("Couldn't find show on TVMaze: http://api.tvmaze.com/singlesearch/shows?q=" + fileShowName); throw new TVMazeException("TVMaze: couldn't find show"); }
public CacheItem Get(string url) { // if we don't have it - go get it if (!CacheItems.Any(o => o.Url == url)) { StrafeForm.Log("Adding new cache item: " + url); CacheItems.Add(new CacheItem(url)); } CacheItem hit = CacheItems.Single(o => o.Url == url); hit.LastUsed = DateTime.Now; return(hit); }
public Episode(string filename) { // we don't need the full path if (filename.Contains("\\")) { filename = filename.Split('\\').ToList().Last(); } StrafeForm.Log("Processing [" + filename + "]"); RawShowName = System.IO.Path.GetFileNameWithoutExtension(filename).ToLower(); string seSubstring = ExtractSE(filename); // eg, S01E01 if (seSubstring == "") { StrafeForm.Log("Can't determine season/episode number"); Error = "Can't determine season/episode number."; } else { StrafeForm.Log("Found SE string: \"" + seSubstring + "\""); // assume everything before the SE string is the show name RawShowName = filename.Substring(0, filename.IndexOf(seSubstring)); } RawShowName = Regex.Replace(RawShowName, @"'", ""); // remove some punctuation RawShowName = Regex.Replace(RawShowName, @"\W", " "); // convert others to spaces RawShowName = Regex.Replace(RawShowName, @"\s+", " "); // consolidate white space RawShowName = RawShowName.Trim(); StrafeForm.Log("Raw show name: \"" + RawShowName + "\""); /* TODO: whenever TheTVDB starts working and I can log in, provide a switch in Config as to which service to get show info from // https://api.thetvdb.com/swagger * for now, we just use TVMaze (which seems to work really well) */ try { TVMaze_Show tvmazeResult = TVMaze.GetShowName(RawShowName); TVMazeId = tvmazeResult.TVMazeId; ShowName = tvmazeResult.ShowName; Year = tvmazeResult.Year; if (Season > 0 || EpisodeNumber > 0) { EpisodeName = TVMaze.GetEpisodeName(TVMazeId, Season, EpisodeNumber); } } catch (TVMazeException tvExc) { Error = tvExc.Message; } }
public void Save() { if (!StrafeForm.Config.CacheEnabled) { CacheFile.Delete(); return; } StrafeForm.Log("Saving cache file"); if (CacheItems.Count == 0) { CacheFile.Delete(); return; } File.WriteAllText(CacheFile.FullName, JsonConvert.SerializeObject(CacheItems)); }
public Cache() { CacheItems = new List <CacheItem>(); StrafeForm.Log("Cache system " + (StrafeForm.Config.CacheEnabled ? "enabled" : "disabled")); if (!StrafeForm.Config.CacheEnabled) { return; } StrafeForm.Log("Loading cache file"); if (!CacheFile.Exists) { return; } CacheItems = (List <CacheItem>)JsonConvert.DeserializeObject <List <CacheItem> >(File.ReadAllText(CacheFile.FullName)); CacheItems.RemoveAll(o => DateTime.Now.Subtract(o.LastUsed).TotalDays > StrafeForm.Config.CacheExpiration); // remove everything that's expired StrafeForm.Log("Cache loaded, " + CacheItems.Count + " items"); }
public static JSONResponse Get(string url, int attempt = 0) { HttpWebRequest webRequest = (HttpWebRequest)HttpWebRequest.Create(url); webRequest.UserAgent = Properties.Settings.Default.useragent.Replace("{version}", System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString()); try { HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); return(new JSONResponse() { JSON = JsonConvert.DeserializeObject(new System.IO.StreamReader(webResponse.GetResponseStream()).ReadToEnd()), HTTPStatus = webResponse.StatusCode }); } catch (WebException webexc) { // timeout error; slow down and retry if (((HttpWebResponse)webexc.Response).StatusCode == (HttpStatusCode)429) { StrafeForm.Log("TVMaze responded with 429 - sleeping 2000 ms"); System.Threading.Thread.Sleep(2000); if (attempt > 5) { return new JSONResponse() { JSON = null, HTTPStatus = (HttpStatusCode)429 } } ; return(JSONResponse.Get(url, attempt + 1)); } return(new JSONResponse() { JSON = null, HTTPStatus = ((HttpWebResponse)webexc.Response).StatusCode }); } catch (Exception exc) { StrafeForm.Log("Unknown exception in JSONResponse: " + exc.Message); return(new JSONResponse() { JSON = null, HTTPStatus = HttpStatusCode.Ambiguous }); } }
public static List <string> GetEpisodeList(int tvMazeId) { CacheItem episodesCache = StrafeForm.Cache.Get("http://api.tvmaze.com/shows/" + tvMazeId + "/episodes?specials=1"); JSONResponse episodes = episodesCache.JSONResponse; if (episodes.HTTPStatus != HttpStatusCode.OK) { StrafeForm.Log("Couldn't find episode on TVMaze: http://api.tvmaze.com/shows/" + tvMazeId + "/episodes?specials=1"); throw new TVMazeException("TVMaze: couldn't get episode list"); } List <string> episodeNames = new List <string>(); foreach (var item in episodes.JSON) { int season = (int?)item.season ?? 0; int number = (int?)item.number ?? 0; string name = item.name; episodeNames.Add(season + "," + number + "," + name); } return(episodeNames); }
public static string GetEpisodeName(int tvMazeId, int season, int episode) { CacheItem episodesCache = StrafeForm.Cache.Get("http://api.tvmaze.com/shows/" + tvMazeId + "/episodes?specials=1"); JSONResponse episodes = episodesCache.JSONResponse; if (episodes.HTTPStatus != HttpStatusCode.OK) { StrafeForm.Log("Couldn't get episode list on TVMaze: http://api.tvmaze.com/shows/" + tvMazeId + "/episodes?specials=1"); throw new TVMazeException("TVMaze: couldn't get episode list"); } foreach (var item in episodes.JSON) { if (((int?)item.season ?? 0) == season && ((int?)item.number ?? 0) == episode) { return((string)item.name); } } StrafeForm.Log("Couldn't find episode on TVMaze: http://api.tvmaze.com/shows/" + tvMazeId + "/episodes?specials=1"); throw new TVMazeException("TVMaze: couldn't find episode in list"); }