private async Task <List <SearchViewModel> > SearchMovies(string query, MyList myList) { var response = await webRequest.Get(showService.ConvertToMovieSearch(query)); if (!response.IsSuccessStatusCode) { throw new ArgumentException("Query request failed"); } var content = await response.Content.ReadAsStringAsync(); var json = JObject.Parse(content); IList <JToken> results = json["results"].Children().ToList(); List <SearchViewModel> movieList = new List <SearchViewModel>(); foreach (JToken r in results) { Movie movieResult = r.ToObject <Movie>(); try { movieResult.Aired = DateTime.ParseExact(r["release_date"].ToString(), "yyyy-M-d", null).ToString("y"); } catch (FormatException) { movieResult.Aired = "Unknown Start Date"; } movieResult.Artwork = r["poster_240x342"].ToString(); bool inList = false; if (myList != null) { var listCheck = myList.MyListShows?.FirstOrDefault(x => x.SafeCompareId(movieResult.ID)); inList = listCheck == null ? false : true; } movieList.Add(new SearchViewModel() { IsInList = inList, Movie = movieResult }); } return(movieList); }
public async Task <Show> GetShowDetails(int?id, PostgresDataContext _context, IWebRequestHandler webRequest) { if (id == null) { return((Show)NotFound()); } var show = await _context.Shows .Include(m => m.ShowService) .Include(m => m.ShowGenre) .SingleOrDefaultAsync(m => m.ID == id); var loadGenres = _context.Genres .Include(m => m.ShowGenre) .ToDictionary(x => x.ID, x => x); var loadServices = _context.Services .Include(m => m.ShowService) .ToDictionary(x => x.Source, x => x); //switch cast to string separated by ; if (show == null) { //get show details var response = await webRequest.Get(this.ConvertToShowDetail(id.Value)); if (!response.IsSuccessStatusCode) { return((Show)NotFound()); } var content = await response.Content.ReadAsStringAsync(); var json = JObject.Parse(content); // may want to come up with a new way to get these values out. // instead of pulling values out manually List <Synonym> synonyms = json["alternate_titles"] .Children() .Select(x => new Synonym() { ShowID = id.Value, Title = x.ToString() }) .ToList(); List <Genre> genres = json["genres"] .Children() .Select(x => new Genre() { ID = (int)JObject.Parse(x.ToString())["id"], Title = (string)JObject.Parse(x.ToString())["title"] }) .ToList(); List <string> cast = json["cast"] .Children() .Select(x => (string)JObject.Parse(x.ToString())["name"]) .ToList(); string castString = "No cast available."; if (cast.Count > 0) { castString = cast[0]; cast.Remove(castString); foreach (var str in cast) { castString = castString + ";" + str; } } //get service details var serviceResponse = await webRequest.Get(this.ConvertToServices(id.Value)); if (!serviceResponse.IsSuccessStatusCode) { return((Show)NotFound()); } var serviceContent = await serviceResponse.Content.ReadAsStringAsync(); var serviceJson = JObject.Parse(serviceContent); // TODO: support more than just web links (such as ios + android) List <Service> services = serviceJson["results"]["web"]["episodes"]["all_sources"] .Children() .Select(x => new Service() { ID = (int)JObject.Parse(x.ToString())["id"], Name = (string)JObject.Parse(x.ToString())["display_name"], Source = (string)JObject.Parse(x.ToString())["source"] }) .ToList(); //get episodes var episodeResponse = await webRequest.Get(this.GetEpisodes(id.Value, 1, 0)); if (!episodeResponse.IsSuccessStatusCode) { return((Show)NotFound()); } var episodeContent = await episodeResponse.Content.ReadAsStringAsync(); JObject episodeJson = JObject.Parse(episodeContent); Dictionary <string, string> freeWeb = null; Dictionary <string, string> tvEverywhereWeb = null; Dictionary <string, string> subscriptionWeb = null; Dictionary <string, string> purchaseWeb = null; JArray results = (JArray)episodeJson["results"]; if (results.Count > 0) { freeWeb = getDictionary(episodeJson, "free_web_sources"); tvEverywhereWeb = getDictionary(episodeJson, "tv_everywhere_web_sources"); subscriptionWeb = getDictionary(episodeJson, "subscription_web_sources"); purchaseWeb = getDictionary(episodeJson, "purchase_web_sources"); } //TODO services displayed multiple times List <ShowService> showServices = new List <ShowService>(); HashSet <string> check = new HashSet <string>(); foreach (var service in services) { loadServices.TryGetValue(service.Source, out Service getService); if (getService == null) { getService = service; } string link = ""; if (freeWeb.ContainsKey(getService.Name)) { link = freeWeb[getService.Name]; } else if (tvEverywhereWeb.ContainsKey(getService.Name)) { link = tvEverywhereWeb[getService.Name]; } else if (subscriptionWeb.ContainsKey(getService.Name)) { link = subscriptionWeb[getService.Name]; } else if (purchaseWeb.ContainsKey(getService.Name)) { link = purchaseWeb[getService.Name]; } if (!check.Contains(getService.Name)) { check.Add(getService.Name); showServices.Add(new ShowService(id.Value, getService.ID, null, getService, link)); } } List <ShowGenre> showGenres = new List <ShowGenre>(); foreach (var genre in genres) { loadGenres.TryGetValue(genre.ID, out Genre getGenre); if (getGenre == null) { getGenre = genre; } showGenres.Add(new ShowGenre(id.Value, getGenre.ID, null, getGenre)); } string poster = (string)json["poster"]; poster.Replace("http://", "https://"); string artwork = (string)json["artwork_304x171"]; artwork.Replace("http://", "https://"); show = new Show() { ID = (int)json["id"], Title = (string)json["title"], Poster = poster, Artwork = artwork, Desc = (string)json["overview"], Started = json["first_aired"].ToString().Substring(0, 4), Rating = (string)json["rating"], Cast = castString, Synonyms = synonyms, ShowGenre = showGenres, ShowService = showServices }; _context.Add(show); _context.SaveChanges(); } return(show); }
public async Task <Movie> GetMovieDetails(int?id, PostgresDataContext _context, IWebRequestHandler webRequest) { if (id == null) { return((Movie)NotFound()); } var movie = await _context.Movies .Include(m => m.MovieService) .Include(m => m.MovieGenre) .SingleOrDefaultAsync(m => m.ID == id); var loadGenres = _context.Genres .Include(m => m.MovieGenre) .ToDictionary(x => x.ID, x => x); var loadServices = _context.Services .Include(m => m.MovieService) .ToDictionary(x => x.Source, x => x); //switch cast to string separated by ; if (movie == null) { //get show details var response = await webRequest.Get(this.ConvertToMovieDetail(id.Value)); if (!response.IsSuccessStatusCode) { return((Movie)NotFound()); } var content = await response.Content.ReadAsStringAsync(); var json = JObject.Parse(content); // may want to come up with a new way to get these values out. // instead of pulling values out manually List <Synonym> synonyms = json["alternate_titles"] .Children() .Select(x => new Synonym() { ShowID = id.Value, Title = x.ToString() }) .ToList(); List <Genre> genres = json["genres"] .Children() .Select(x => new Genre() { ID = (int)JObject.Parse(x.ToString())["id"], Title = (string)JObject.Parse(x.ToString())["title"] }) .ToList(); List <MovieGenre> movieGenres = new List <MovieGenre>(); foreach (var genre in genres) { loadGenres.TryGetValue(genre.ID, out Genre getGenre); if (getGenre == null) { getGenre = genre; } movieGenres.Add(new MovieGenre(id.Value, getGenre.ID, null, getGenre)); } List <string> cast = json["cast"] .Children() .Select(x => (string)JObject.Parse(x.ToString())["name"]) .ToList(); string castString = "No cast available."; if (cast.Count > 0) { castString = cast[0]; cast.Remove(castString); foreach (var str in cast) { castString = castString + ";" + str; } } //get service details List <Service> services = new List <Service>(); Dictionary <string, string> links = new Dictionary <string, string>(); foreach (var x in json["free_web_sources"].Children()) { var source = (string)JObject.Parse(x.ToString())["source"]; services.Add(new Service() { Name = (string)JObject.Parse(x.ToString())["display_name"], Source = source }); links.Add(source, (string)JObject.Parse(x.ToString())["link"]); } foreach (var x in json["tv_everywhere_web_sources"].Children()) { var source = (string)JObject.Parse(x.ToString())["source"]; services.Add(new Service() { Name = (string)JObject.Parse(x.ToString())["display_name"], Source = source }); links.Add(source, (string)JObject.Parse(x.ToString())["link"]); } foreach (var x in json["subscription_web_sources"].Children()) { var source = (string)JObject.Parse(x.ToString())["source"]; services.Add(new Service() { Name = (string)JObject.Parse(x.ToString())["display_name"], Source = source }); links.Add(source, (string)JObject.Parse(x.ToString())["link"]); } foreach (var x in json["purchase_web_sources"].Children()) { var source = (string)JObject.Parse(x.ToString())["source"]; services.Add(new Service() { Name = (string)JObject.Parse(x.ToString())["display_name"], Source = source }); links.Add(source, (string)JObject.Parse(x.ToString())["link"]); } List <MovieService> movieServices = new List <MovieService>(); HashSet <string> check = new HashSet <string>(); foreach (var ser in services) { loadServices.TryGetValue(ser.Source, out Service getService); if (getService != null && !check.Contains(ser.Name)) { check.Add(ser.Name); movieServices.Add(new MovieService(id.Value, getService.ID, null, getService, links[getService.Source])); } } string poster = (string)json["poster_400x570"]; poster.Replace("http://", "https://"); string artwork = poster; var movieResponse = await webRequest.Get(this.ConvertToMovieImage(id.Value)); if (!movieResponse.IsSuccessStatusCode) { return((Movie)NotFound()); } var movieContent = await movieResponse.Content.ReadAsStringAsync(); var movieJson = JObject.Parse(movieContent); JToken results = movieJson["results"]; if (results.Count() > 0) { JToken thumbnails = results["thumbnails"]; if (thumbnails != null) { var artworkToken = thumbnails.First; artwork = artworkToken["medium"]["url"].ToString(); artwork.Replace("http://", "https://"); } } movie = new Movie() { ID = (int)json["id"], Title = (string)json["title"], Poster = poster, Artwork = artwork, Desc = (string)json["overview"], Aired = json["release_date"].ToString().Substring(0, 4), Rating = (string)json["rating"], Cast = castString, MovieGenre = movieGenres, MovieService = movieServices }; _context.Add(movie); _context.SaveChanges(); } return(movie); }
public async Task <IActionResult> Services() { var getShowService = await _context.ShowServices .Include(s => s.Service) .Include(s => s.Show) .ToListAsync(); var getMovieService = await _context.MovieService .Include(m => m.Service) .Include(m => m.Movie) .ToListAsync(); var getServices = await _context.Services .ToListAsync(); var getGenres = await _context.Genres .ToListAsync(); if (getServices.Count == 0) { //get all sources var sources = await webRequest.Get(showService.GetSources()); if (!sources.IsSuccessStatusCode) { return(NotFound()); } var sourceContent = await sources.Content.ReadAsStringAsync(); var sourceJson = JObject.Parse(sourceContent); List <Service> serviceList = sourceJson["results"] .Children() .Select(x => new Service() { ID = (int)JObject.Parse(x.ToString())["id"], Name = (string)JObject.Parse(x.ToString())["display_name"], Source = (string)JObject.Parse(x.ToString())["source"], Link = (string)JObject.Parse(x.ToString())["info"] }) .ToList(); foreach (var ser in serviceList) { Service getService = _context.Services.SingleOrDefault(s => s.ID == ser.ID); if (getService == null) { _context.Add(ser); } } _context.SaveChanges(); } if (getGenres.Count == 0) { var genres = await webRequest.Get(showService.GetGenres()); if (!genres.IsSuccessStatusCode) { return(NotFound()); } var genreContent = await genres.Content.ReadAsStringAsync(); var genreJson = JObject.Parse(genreContent); List <Genre> genreList = genreJson["results"] .Children() .Select(x => new Genre() { ID = (int)JObject.Parse(x.ToString())["id"], Title = (string)JObject.Parse(x.ToString())["genre"] }) .ToList(); foreach (var gen in genreList) { Genre getGenre = _context.Genres.SingleOrDefault(s => s.ID == gen.ID); if (getGenre == null) { _context.Add(gen); } } _context.SaveChanges(); } SortedDictionary <String, HashSet <Object> > serviceDictionary = new SortedDictionary <String, HashSet <Object> >(); foreach (var ser in getShowService) { string key = ser.Service.Name; if (!serviceDictionary.ContainsKey(key)) { serviceDictionary.Add(key, new HashSet <Object>() { ser.Show }); } else { HashSet <Object> getList = serviceDictionary[key]; if (!getList.Contains(ser.Show)) { getList.Add(ser.Show); serviceDictionary.Remove(key); serviceDictionary.Add(key, getList); } } } foreach (var mov in getMovieService) { string key = mov.Service.Name; if (!serviceDictionary.ContainsKey(key)) { serviceDictionary.Add(key, new HashSet <Object>() { mov.Movie }); } else { HashSet <Object> getList = serviceDictionary[key]; if (!getList.Contains(mov.Movie)) { getList.Add(mov.Movie); serviceDictionary.Remove(key); serviceDictionary.Add(key, getList); } } } return(View(serviceDictionary)); }