public async Task ParseNewMovies() { var movieProvider = DependencyResolver.Resolve <IMovieProvider>(provider.Name); int startPageNumber = Math.Max((byte)1, provider.LastPageProcessed); logger.Debug(string.Format("Start the parsing process from the page {0}", startPageNumber)); var pageSet = movieProvider.EnumerateFromPage(startPageNumber); foreach (var moviesPageTask in pageSet.Take(2)) { var page = await moviesPageTask; var newMovies = (from movieSummary in page where !context.Movies.Select(li => li.Title).Contains(movieSummary.Title) select movieSummary).ToList(); logger.Debug(string.Format("Found {0} movies and {1} are new", page.Count, newMovies.Count)); foreach (var movie in newMovies) { var builder = new MovieBuilder(null, movie); builder.OnMovieLoaded += Builder_OnMovieLoaded; await movieProvider.AddToBuilder(builder, movie); } if (++provider.LastPageProcessed >= pageSet.TotalPages) { provider.LastPageProcessed = 0; // All pages processed } context.SubmitChanges(); if (provider.LastPageProcessed == 0 && newMovies.Count < page.Count) { break; } } }
async Task <Movie> GetMovieInfo(int id) { using (var context = new MoviesContext()) { var dlo = new DataLoadOptions(); dlo.LoadWith <Database.Movies>(li => li.MovieStreamSet); dlo.LoadWith <Database.MovieStreamSet>(li => li.StreamCookie); context.LoadOptions = dlo; var movie = context.Movies.Single(li => li.Id == id); var movieStreams = new List <Contracts.DTA.MovieStreamSet>(); var firstMovieStreamSet = movie.MovieStreamSet.FirstOrDefault(); if (firstMovieStreamSet == null) { throw new InvalidOperationException(string.Format("Movie {0} does not have a stream attached", movie.Id)); } if (firstMovieStreamSet.StreamCookie.Any(li => li.ExpirationDate < DateTime.UtcNow)) { await IssueStreamParse(firstMovieStreamSet); context.SubmitChanges(); } for (var i = 1; i < movie.MovieStreamSet.Count; i++) { var streamSet = movie.MovieStreamSet[i]; if (streamSet.StreamCookie.Any(li => li.ExpirationDate >= DateTime.UtcNow)) { // add a record just with an id and null streams movieStreams.Add(new Contracts.DTA.MovieStreamSet { Id = streamSet.Id }); continue; } movieStreams.Add(streamSet.ToDTA()); } return(movie.ToDTA()); } }