private async Task ImportSeriesDetailsFromTvDb() { // get and set token await _tvDbApi.RefreshJwtToken(); for (var index = 0; index < _importSeriesIds.Count; index++) { var seriesId = _importSeriesIds.ElementAt(index); var tvDbId = await _tvSeriesContext .Series .Where(x => x.MovieDbId == seriesId) .Select(x => x.TvDbId) .FirstOrDefaultAsync(); SeriesJson seriesJson = null; if (tvDbId != null) { seriesJson = await _tvDbApi.GetSeries(tvDbId.Value); } if (seriesJson != null) { var seriesFromImport = _tvDbDomainConverter.ConvertToSeriesRuntimeAndBroadcast(seriesJson); var seriesFromDb = await _tvSeriesContext .Series .Where(x => x.MovieDbId == seriesId) .FirstOrDefaultAsync(); await _movieDbImportServiceDbHelper.UpdateSeriesRuntimeAndBroadcast(seriesFromDb, seriesFromImport); } else { _logger.LogInformation("Series with TvDbId [{0}] not found", tvDbId); } } }
private async Task ImportSeries() { var refreshRetries = 0; // get and set token await _tvDbApi.RefreshJwtToken(); for (var index = 0; index < _importSeriesIds.Count - 1; index++) { var seriesId = _importSeriesIds.ElementAt(index); var seriesJson = await _tvDbApi.GetSeries(seriesId); if (seriesJson != null) { var series = await _tvDbDomainConverter.ConvertToSeries(seriesJson); // often series does not contain name, skip such record // TODO move to validator if (string.IsNullOrEmpty(series.Name)) { _logger.LogInformation("Skip Series with id [{0}] - empty name", seriesId); continue; } // save to DB; var seriesFromDb = await _tvSeriesContext .Series .Where(x => x.TvDbId == seriesId) .FirstOrDefaultAsync(); // TODO think about default value series.IsImportEnabled = true; // series already in db if (seriesFromDb != null) { // TODO move mapper? MapSeriesFromImportToSeriesFromDb(ref seriesFromDb, series); seriesFromDb.LastChangedAt = DateTime.UtcNow; // TODO system user id //seriesFromDb.LastChangedBy = 1; _tvSeriesContext.Series.Update(seriesFromDb); _logger.LogInformation("Updated series with id [{0}]", seriesId); } else { series.CreatedAt = DateTime.UtcNow; //series.CreatedBy = 1; await _tvSeriesContext.Series.AddAsync(series); _logger.LogInformation("Added series with id [{0}]", seriesId); } await _tvSeriesContext.SaveChangesAsync(); } if (seriesJson != null) { continue; } if (!_tvDbApi.IsTokenFresh()) { if (refreshRetries == _maxRefreshRetries) { _logger.LogError("Maximum number of refresh retries [{0}] was reached. Last processed series with id [{1}]. Worker will be shut down", _maxRefreshRetries, seriesId); return; } await _tvDbApi.RefreshJwtToken(); _logger.LogWarning("Unauthorized request. Token was refreshed. Retrying to get series with id [{0}]", seriesId); index--; refreshRetries++; continue; } _logger.LogInformation("Could not found error-free series with id [{0}]", seriesId); } }