Пример #1
0
        public static async Task<Movie> GetData(ObjectId id = new ObjectId(), string name="Captain Phillips")
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://www.omdbapi.com");
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                var searchName = HttpUtility.UrlEncode(name);
                HttpResponseMessage response = await client.GetAsync(string.Format("?t={0}&r=json", searchName));

                if (response.IsSuccessStatusCode)
                {
                    string responseBody = await response.Content.ReadAsStringAsync();
                    dynamic result = new ExpandoObject();
                    result.Actors = "";
                    result.Director= "";
                    result.Genre= "";
                    result.imdbRating= "";
                    result.Language= "";
                    result.Metascore= "";
                    result.Released= "";
                    result.Runtime = "";
                    result.Poster = "";
                    result.Plot = "";
                    result = Json.Decode(responseBody);
                    
                    Movie movie = new Movie()
                    {
                        ImdbRating = result.imdbRating,
                        MetascoreRating = result.Metascore
                    };

                    /*if (result.Poster != "")
                    {
                        using (WebClient imgClient = new WebClient())
                        {
                            try
                            {
                                movie.Poster = imgClient.DownloadData(result.Poster);
                            }
                            catch (Exception e)
                            {
                                movie.Poster = null;
                            }
                            
                        }
                    }*/

                    return movie;
                }
                else
                {
                    return null;
                }
            }
        }
Пример #2
0
 public async Task<ObjectId?> InsertNewMovie(Movie movie)
 {
     await Movies.InsertOneAsync(movie);
     return movie.ID;
 }