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

                var o = JObject.Parse(data);
                var episodeRunTimeArray = (JArray)o["episode_run_time"];

                var episodeRunTime = episodeRunTimeArray[0] + "";

                var tvShow = TVShow.Instanciate(o["original_name"] + "",
                                                o["overview"] + "",
                                                string.IsNullOrEmpty(o["first_air_date"] + "") ? (DateTime?)null : DateTime.Parse(o["first_air_date"] + ""),
                                                string.IsNullOrEmpty(episodeRunTime) ? 0 : Int32.Parse(episodeRunTime),
                                                string.IsNullOrEmpty(o["id"] + "") ? 0 : Int32.Parse(o["id"] + ""),
                                                o["imdb_id"] + "",
                                                o["poster_path"] + "",
                                                o["backdrop_path"] + "");

                await _tvShowRepository.Save(tvShow);

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

                foreach (var item in genreArray)
                {
                    //Checked to ensure is TMDB when list is loaded from memory
                    var genre = _tmdbTVShowGenres.FirstOrDefault(x => x.SourceId == (int)item["id"]);
                    if (genre != null)
                    {
                        await _relationRepository.CreateTVShowToTVShowGenreRelationship(tvShow, genre);
                    }
                }

                int year;

                year = tvShow.FirstAirDate?.Year ?? 1900;

                Console.WriteLine(tvShow.GetId().ToString("0000") + " : " + tvShow.Name + " released in " +
                                  year + " with IMDB id " + o["imdb_id"] + " " + year);

                await AddExternalIds(tvShow);

                await _tvShowRepository.Save(tvShow);

                Console.WriteLine("Added external ids for " + tvShow.Name + " imdb " + tvShow.ImdbId);
            }
            catch (Exception e)

            {
                Console.WriteLine("Adding TV Show to DB: My error is " + e);
            }
        }