Example #1
0
        private static async Task AddMovieToDatabase(int movieId)
        {
            try
            {
                var myUrl = "https://api.themoviedb.org/3/movie/" + movieId +
                            "?api_key=661b76973b90b91e0df214904015fd4d";
                var client = new HttpClient();
                var data   = await client.GetStringAsync(myUrl);

                var o = JObject.Parse(data);

                var movie = Movie.Instanciate(o["title"] + "",
                                              o["overview"] + "",
                                              string.IsNullOrEmpty(o["release_date"] + "") ? (DateTime?)null : DateTime.Parse(o["release_date"] + ""),
                                              string.IsNullOrEmpty(o["runtime"] + "") ? 0 : Int32.Parse(o["runtime"] + ""),
                                              string.IsNullOrEmpty(o["id"] + "") ? 0 : Int32.Parse(o["id"] + ""),
                                              o["imdb_id"] + "",
                                              o["poster_path"] + "",
                                              o["backdrop_path"] + "",
                                              DateTime.UtcNow);

                await _movieRepository.Save(movie);

                var genreArray = (JArray)o["genres"];

                foreach (var item in genreArray)
                {
                    //Checked to ensure is TMDB when list is loaded from memory
                    var genre = _tmdbMovieGenres.First(x => x.SourceId == (int)item["id"]);
                    await _relationRepository.CreateMovieToMovieGenreRelationship(movie, genre);
                }

                var collectiontoken = o["belongs_to_collection"];

                var isPartOfCollection = "";

                if (collectiontoken.HasValues)
                {
                    isPartOfCollection = " Is part of collection " + (int)collectiontoken["id"];
                    await AddMovieCollection((int)collectiontoken["id"]);

                    await Task.Delay(200);
                }

                int year;
                if (movie.ReleaseDate.HasValue)
                {
                    year = movie.ReleaseDate.Value.Year;
                }
                else
                {
                    year = 1900;
                }

                Console.WriteLine(movie.GetId().ToString("0000") + " : " + movie.Name + " released in " +
                                  year + " with IMDB id " + o["imdb_id"] + " " + isPartOfCollection);
            }
            catch (Exception e)

            {
                Console.WriteLine("My error is " + e);
            }
        }