/// <summary> /// Scrapes the cast members for a particular show. /// </summary> /// <param name="showid">The showid.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns> /// A list of cast members. /// </returns> public async Task <List <CastMemberDto> > ScrapeCastMembers(int showid, CancellationToken cancellationToken = default) { var(status, json) = await this.apiRepository.RequestJsonForTvMaze( new Uri($"/shows/{showid}/cast", UriKind.Relative), cancellationToken) .ConfigureAwait(false); if (status != HttpStatusCode.OK) { this.logger.LogWarning("Scraping cast for #{showid} returned status {status}.", showid, status); return(null); } var result = new List <CastMemberDto>(); // read json var roles = JArray.Parse(json); foreach (dynamic role in roles) { var person = role.person; var member = new CastMemberDto { Id = person.id, Name = person.name, Birthdate = person.birthday, }; result.Add(member); } this.logger.LogInformation("Found {resultCount} cast members for show #{showid}.", result.Count, showid); return(result); }
/// <summary> /// Scrapes the single show by its identifier. /// </summary> /// <param name="showId">The show's identifier.</param> /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>A Task with Show and status code.</returns> public async Task <ScrapeResult> ScrapeSingleShowById(int showId, CancellationToken cancellationToken = default) { var sw = Stopwatch.StartNew(); var(status, json) = await this.apiRepository.RequestJsonForTvMaze( new Uri($"/shows/{showId}?embed=cast", UriKind.Relative), cancellationToken) .ConfigureAwait(false); sw.Stop(); this.logger.LogInformation("Getting info about show {ShowId} took {Elapsed} msec and returned status {HttpStatus}.", showId, sw.ElapsedMilliseconds, status); if (status == Constants.ServerTooBusy) { // too much, so back off this.logger.LogDebug("Server too busy to scrape #{ShowId}, backing off.", showId); return(new ScrapeResult { HttpStatus = status }); } if (status == HttpStatusCode.OK) { dynamic jshow = JObject.Parse(json); var show = new ShowDto { Id = jshow.id, Name = jshow.name, ImdbId = jshow.externals?.imdb, }; var embedded = jshow._embedded; if (embedded is null) { this.logger.LogError("Server didn't return the requested embedded data for show {ShowId}.", showId); } else { var jcast = embedded.cast; if (jcast is null) { this.logger.LogError("Server didn't return the requested cast in the embedded data for show {ShowId}.", showId); } else { foreach (var container in jcast) { var person = container.person; var member = new CastMemberDto { Id = person.id, Name = person.name, Birthdate = person.birthday, }; show.CastMembers.Add(member); } } } return(new ScrapeResult { Show = show, HttpStatus = status }); } return(new ScrapeResult { HttpStatus = status }); }