コード例 #1
0
        /// <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");
        }
コード例 #2
0
        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
                });
            }
        }
コード例 #3
0
        public static List <string> GetShowList(string search)
        {
            CacheItem    episodesCache = StrafeForm.Cache.Get("http://api.tvmaze.com/search/shows?q=" + search);
            JSONResponse episodes      = episodesCache.JSONResponse;

            if (episodes.HTTPStatus != HttpStatusCode.OK)
            {
                throw new TVMazeException("TVMaze: no matches");
            }

            List <string> showNames = new List <string>();

            foreach (var item in episodes.JSON)
            {
                string name      = item.show.name;
                int    id        = item.show.id;
                string premiered = (string)item.show.premiered;
                showNames.Add(id + "," + (string.IsNullOrWhiteSpace(premiered) ? "" : premiered.Substring(0, 4)) + "," + name);
            }
            return(showNames);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        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");
        }
コード例 #6
0
 public CacheItem(string url)
 {
     Url          = url;
     LastUsed     = DateTime.Now;
     JSONResponse = JSONResponse.Get(url);
 }