Ejemplo n.º 1
0
        public async Task CreateImdbMovieList(string listId, string listTitle)
        {
            var movieListParser =
                new MediaListParser(listId, listTitle);
            await movieListParser.GetListOfMedia();

            _mediaList = await _mediaListRepository.GetBySourceAndCollectionId(Category.SOURCE_IMDB, listId, true);

            if (_mediaList != null)
            {
                //TODO If list exists, want size to compare with size of IMDB list
                //and then update if necessary

                Console.WriteLine("List with id {0} and title {1} already exists", listId, listTitle);
                return;
            }
            _mediaList = MediaList.InstanciateImdbMovieList(movieListParser.ListId, movieListParser.ListName
                                                            , movieListParser.ListDescription, null, null);

            await _mediaListRepository.Save(_mediaList);

            var moviePopulating = new MoviePopulating(_configuration, _cache);

            foreach (var movieImdbId in movieListParser.MediaList)
            {
                try
                {
                    await Task.Delay(200);

                    var movie = await _movieRepository.GetByImdbId(movieImdbId);

                    if (movie == null)
                    {
                        var myUrl = "https://api.themoviedb.org/3/find/" + movieImdbId +
                                    "?api_key=661b76973b90b91e0df214904015fd4d&external_source=imdb_id";
                        var client = new HttpClient();
                        var data   = await client.GetStringAsync(myUrl);

                        var o = JObject.Parse(data);
                        var a = (JArray)o["movie_results"];

                        var tmdbId = (int)a[0]["id"];

                        await moviePopulating.AddMovie(tmdbId);

                        await Task.Delay(200);

                        movie = await _movieRepository.GetByTmdbId(tmdbId);
                    }
                    await _relationRepository.CreateMediaListToMovieRelationship(_mediaList, movie);
                }
                catch (Exception e)

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