//join get movie roots into the same method
        //return a task of type results
        private static async Task <MovieRoot> GetMovieRoot(string movieTitle, string year)
        {
            string url;

            if (year == "0")
            {
                url = String.Format("https://api.themoviedb.org/3/search/movie?api_key=11111111111111111111&language=en-US&query={0}&page=1&include_adult=false", movieTitle);
            }
            else
            {
                url = String.Format("https://api.themoviedb.org/3/search/movie?api_key=11111111111111111111&language=en-US&query={0}&page=1&include_adult=false&year={1}", movieTitle, year);
            }

            /*call out to moviedb
            *  //limit api requests to 40 calls per 10 seconds
            *  //250ms per request*/

            await Task.Delay(250);

            /*delay 250ms per request so program does not exceed the limit(this is a temporary solution,
             * I will use a Nuget Package to throttle later) also there is the issue of multiple users
             * making requests which means they will have to be cached on a server somewhere.*/

            HttpClient          http     = new HttpClient();
            HttpResponseMessage response = null;

            try
            {
                //gets a response of type http response msg
                response = await http.GetAsync(url);

                response.EnsureSuccessStatusCode();
            }
            catch (HttpRequestException)
            {
                internetFail = true;
                MovieRoot temp = new MovieRoot();
                temp.total_results = 0;
                return(temp);
            }

            Debug.WriteLine("code" + (int)response.StatusCode);

            //takes the http response and converts it to string
            var json = await response.Content.ReadAsStringAsync();

            //response -> string/ json -> deserilaize
            var serializer = new DataContractJsonSerializer(typeof(MovieRoot));

            /*buffer to hold the api info as it comes in and then hand it to serializer
             * which gives the info as an object graph beginning at the root with results*/
            var ms = new MemoryStream(Encoding.UTF8.GetBytes(json));

            //serializer reads the memory stream object and is cast to a results object
            var result = (MovieRoot)serializer.ReadObject(ms);

            return(result);
        }
        public void OnGet()
        {
            var client  = new RestClient("http://localhost:15931/movie/nowplaying");
            var request = new RestRequest(Method.GET);

            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("Connection", "keep-alive");
            request.AddHeader("Accept-Encoding", "gzip, deflate");
            request.AddHeader("Host", "localhost:15931");
            request.AddHeader("Postman-Token", "4976c241-d542-4531-8129-b801379b87e3,78587da2-59c1-441f-95c5-e759a3d04864");
            request.AddHeader("Cache-Control", "no-cache");
            request.AddHeader("Accept", "*/*");
            request.AddHeader("User-Agent", "PostmanRuntime/7.17.1");
            IRestResponse response = client.Execute(request);

            MoviesNowPlaying = JsonConvert.DeserializeObject <MovieRoot>(response.Content);
        }
Esempio n. 3
0
        public static async Task SearchMoviesAsync(string apiKey, string lang, string query)
        {
            var client = new HttpClient();

            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            var resultMovieAsync = await client.GetStringAsync($"https://api.themoviedb.org/3/search/movie?api_key={apiKey}&language={lang}&query={query}")
                                   .ConfigureAwait(false);

            MovieRoot movieRoot = JsonConvert.DeserializeObject <MovieRoot>(resultMovieAsync);

            if (movieRoot.TotalResults == 0)
            {
                Console.WriteLine("По вашему запросу ничего не найдено");
            }
            else
            {
                foreach (var movie in movieRoot.MovieResults)
                {
                    Console.WriteLine($"ID: {movie.Id}\nTitle: {movie.Title}\nRelease date: {movie.ReleaseDate}\nOverview: {movie.Overview}\n");
                }
            }
        }
        /* Acquire the poster images from MovieDB
         */
        public static async Task Populate(ObservableCollection <Movie> movies)
        {
            MovieRoot gmr = new MovieRoot();

            //iterate through titleYear List and make calls to the api
            try
            {
                //---------------------------------------------------------------------------------------------------------
                //regex movies with year
                foreach (KeyValuePair <string, string> kvp in titleYear)
                {
                    if (kvp.Key != "")
                    {
                        gmr = await GetMovieRoot(kvp.Key, kvp.Value);

                        if (gmr.total_results == 0)
                        {
                            notFound.Add(kvp.Key);
                            Debug.WriteLine("not found: " + kvp.Key);
                        }
                    }

                    //this is getting the json out of the movie object (a list of type movie that holds bools strings etc)
                    List <Movie> regexMoviesList = gmr.results;

                    AddToMoviesList(regexMoviesList, movies);
                }
            }

            catch (Exception)
            {
                internetFail = true;
                return;
            }



            try
            {
                //---------------------------------------------------------------------------------------------------------
                //titles that didnt match the regex (noMatch)
                foreach (string title in noMatchList)
                {
                    if (title != "")
                    {
                        gmr = await GetMovieRoot(title, "0");

                        if (gmr.total_results == 0)
                        {
                            notFound.Add(title);
                            Debug.WriteLine("not found: " + title);
                        }
                    }
                    //this is getting the json out of the movie object (a list of type movie that holds bools strings etc)
                    List <Movie> noMatchMoviesList = gmr.results;
                    AddToMoviesList(noMatchMoviesList, movies);
                }
            }
            catch (Exception)
            {
                internetFail = true;
                return;
            }

            try
            {
                //---------------------------------------------------------------------------------------------------------
                //notFound - titles that got no repsonse from the api
                CleanNotFoundList();

                foreach (string nf in notFound)
                {
                    if (nf != "")
                    {
                        gmr = await GetMovieRoot(nf, "0");
                    }
                    //this is getting the json out of the movie object (a list of type movie that holds bools strings etc)
                    List <Movie> notFoundMoviesList = gmr.results;
                    AddToMoviesList(notFoundMoviesList, movies);
                }
            }
            catch (Exception)
            {
                internetFail = true;
                return;
            }
        }