Exemplo n.º 1
0
        private static async Task AddMovieCollection(int collectionId)
        {
            if (await _moviecollectionRepository.GetBySourceAndMovieCollectionId
                    (Category.SOURCE_TMDB, collectionId) == null)
            {
                try
                {
                    var myUrl = "https://api.themoviedb.org/3/collection/" + collectionId +
                                "?api_key=661b76973b90b91e0df214904015fd4d";
                    var client = new HttpClient();
                    var data   = await client.GetStringAsync(myUrl);

                    var o = JObject.Parse(data);

                    var movieCollection = MovieCollection.InstanciateTmdbCollection(
                        (int)o["id"], o["name"] + "", o["overview"] + "",
                        o["poster_path"] + "", o["backdrop_path"] + "");

                    await _moviecollectionRepository.Save(movieCollection);

                    var parts = (JArray)o["parts"];

                    foreach (var token in parts)
                    {
                        var movie = await _movieRepository.GetByTmdbId((int)token["id"]);

                        if (movie == null)
                        {
                            await AddMovieToDatabase((int)token["id"]);

                            await Task.Delay(200);

                            movie = await _movieRepository.GetByTmdbId((int)token["id"]);
                        }

                        await _relationRepository.CreateMovieCollectionToMovieRelationship(movieCollection, movie);
                    }

                    Console.WriteLine(movieCollection.Name + " Collection added with " + parts.Count + " movies added");
                }
                catch (Exception e)
                {
                    Console.WriteLine("My error is " + e);
                }
            }
        }