more consolidation
This commit is contained in:
@@ -37,111 +37,20 @@ public class ContentRefreshService : IContentRefreshService
|
||||
/// <returns>List of URNs for new content.</returns>
|
||||
public async Task<List<string>> RefreshLatestContentAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var urns = new List<string>();
|
||||
|
||||
try
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null || !config.EnableLatestContent)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null || !config.EnableLatestContent)
|
||||
{
|
||||
_logger.LogDebug("Latest content refresh is disabled");
|
||||
return urns;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Refreshing latest content for business unit: {BusinessUnit}", config.BusinessUnit);
|
||||
|
||||
using var apiClient = _apiClientFactory.CreateClient();
|
||||
var businessUnit = config.BusinessUnit.ToString().ToLowerInvariant();
|
||||
|
||||
// Get all shows from Play v3 API
|
||||
var shows = await apiClient.GetAllShowsAsync(businessUnit, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (shows == null || shows.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No shows found for business unit: {BusinessUnit}", config.BusinessUnit);
|
||||
return urns;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Found {Count} shows, fetching latest episodes from each", shows.Count);
|
||||
|
||||
// Get latest episodes from each show (limit to 20 shows to avoid overwhelming)
|
||||
var showsToFetch = shows.Where(s => s.NumberOfEpisodes > 0)
|
||||
.OrderByDescending(s => s.NumberOfEpisodes)
|
||||
.Take(20)
|
||||
.ToList();
|
||||
|
||||
foreach (var show in showsToFetch)
|
||||
{
|
||||
if (show.Id == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var videos = await apiClient.GetVideosForShowAsync(businessUnit, show.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (videos != null && videos.Count > 0)
|
||||
{
|
||||
_logger.LogDebug("Show {Show} ({ShowId}): Found {Count} videos", show.Title, show.Id, videos.Count);
|
||||
|
||||
// Filter to videos that are actually published (validFrom in the past)
|
||||
var now = DateTime.UtcNow;
|
||||
var publishedVideos = videos.Where(v =>
|
||||
v.ValidFrom == null || v.ValidFrom.Value.ToUniversalTime() <= now).ToList();
|
||||
|
||||
_logger.LogDebug("Show {Show}: {PublishedCount} published out of {TotalCount} videos", show.Title, publishedVideos.Count, videos.Count);
|
||||
|
||||
if (publishedVideos.Count > 0)
|
||||
{
|
||||
// Take only the most recent published video from each show
|
||||
var latestVideo = publishedVideos.OrderByDescending(v => v.Date).FirstOrDefault();
|
||||
if (latestVideo?.Urn != null)
|
||||
{
|
||||
urns.Add(latestVideo.Urn);
|
||||
_logger.LogInformation(
|
||||
"Added latest video from show {Show}: {Title} (URN: {Urn}, Date: {Date}, ValidFrom: {ValidFrom}, ValidTo: {ValidTo})",
|
||||
show.Title,
|
||||
latestVideo.Title,
|
||||
latestVideo.Urn,
|
||||
latestVideo.Date,
|
||||
latestVideo.ValidFrom,
|
||||
latestVideo.ValidTo);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Show {Show}: Latest video has null URN", show.Title);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Show {Show} has no published videos yet", show.Title);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Show {Show} ({ShowId}): No videos returned from API", show.Title, show.Id);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error fetching videos for show {ShowId}", show.Id);
|
||||
}
|
||||
|
||||
// Respect cancellation
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Refreshed {Count} latest content items from {ShowCount} shows", urns.Count, showsToFetch.Count);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error refreshing latest content");
|
||||
_logger.LogDebug("Latest content refresh is disabled");
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
return urns;
|
||||
return await FetchVideosFromShowsAsync(
|
||||
config.BusinessUnit.ToString().ToLowerInvariant(),
|
||||
minEpisodeCount: 0,
|
||||
maxShows: 20,
|
||||
videosPerShow: 1,
|
||||
contentType: "latest",
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -151,43 +60,62 @@ public class ContentRefreshService : IContentRefreshService
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>List of URNs for trending content.</returns>
|
||||
public async Task<List<string>> RefreshTrendingContentAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null || !config.EnableTrendingContent)
|
||||
{
|
||||
_logger.LogDebug("Trending content refresh is disabled");
|
||||
return new List<string>();
|
||||
}
|
||||
|
||||
return await FetchVideosFromShowsAsync(
|
||||
config.BusinessUnit.ToString().ToLowerInvariant(),
|
||||
minEpisodeCount: 10,
|
||||
maxShows: 15,
|
||||
videosPerShow: 2,
|
||||
contentType: "trending",
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fetches videos from shows based on filter criteria.
|
||||
/// </summary>
|
||||
private async Task<List<string>> FetchVideosFromShowsAsync(
|
||||
string businessUnit,
|
||||
int minEpisodeCount,
|
||||
int maxShows,
|
||||
int videosPerShow,
|
||||
string contentType,
|
||||
CancellationToken cancellationToken)
|
||||
{
|
||||
var urns = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
var config = Plugin.Instance?.Configuration;
|
||||
if (config == null || !config.EnableTrendingContent)
|
||||
{
|
||||
_logger.LogDebug("Trending content refresh is disabled");
|
||||
return urns;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Refreshing trending content for business unit: {BusinessUnit}", config.BusinessUnit);
|
||||
_logger.LogInformation("Refreshing {ContentType} content for business unit: {BusinessUnit}", contentType, businessUnit);
|
||||
|
||||
using var apiClient = _apiClientFactory.CreateClient();
|
||||
var businessUnit = config.BusinessUnit.ToString().ToLowerInvariant();
|
||||
|
||||
// Get all shows from Play v3 API
|
||||
var shows = await apiClient.GetAllShowsAsync(businessUnit, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
if (shows == null || shows.Count == 0)
|
||||
{
|
||||
_logger.LogWarning("No shows found for business unit: {BusinessUnit}", config.BusinessUnit);
|
||||
_logger.LogWarning("No shows found for business unit: {BusinessUnit}", businessUnit);
|
||||
return urns;
|
||||
}
|
||||
|
||||
_logger.LogInformation("Found {Count} shows, fetching popular content", shows.Count);
|
||||
_logger.LogInformation("Found {Count} shows, fetching {ContentType} content", shows.Count, contentType);
|
||||
|
||||
// Get videos from popular shows (those with many episodes)
|
||||
var popularShows = shows.Where(s => s.NumberOfEpisodes > 10)
|
||||
var filteredShows = shows
|
||||
.Where(s => s.NumberOfEpisodes > minEpisodeCount)
|
||||
.OrderByDescending(s => s.NumberOfEpisodes)
|
||||
.Take(15)
|
||||
.Take(maxShows)
|
||||
.ToList();
|
||||
|
||||
foreach (var show in popularShows)
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
foreach (var show in filteredShows)
|
||||
{
|
||||
if (show.Id == null)
|
||||
if (show.Id == null || cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -195,64 +123,46 @@ public class ContentRefreshService : IContentRefreshService
|
||||
try
|
||||
{
|
||||
var videos = await apiClient.GetVideosForShowAsync(businessUnit, show.Id, cancellationToken).ConfigureAwait(false);
|
||||
if (videos != null && videos.Count > 0)
|
||||
if (videos == null || videos.Count == 0)
|
||||
{
|
||||
_logger.LogDebug("Show {Show} ({ShowId}): Found {Count} videos for trending", show.Title, show.Id, videos.Count);
|
||||
|
||||
// Filter to videos that are actually published (validFrom in the past)
|
||||
var now = DateTime.UtcNow;
|
||||
var publishedVideos = videos.Where(v =>
|
||||
v.ValidFrom == null || v.ValidFrom.Value.ToUniversalTime() <= now).ToList();
|
||||
|
||||
_logger.LogDebug("Show {Show}: {PublishedCount} published out of {TotalCount} videos for trending", show.Title, publishedVideos.Count, videos.Count);
|
||||
|
||||
if (publishedVideos.Count > 0)
|
||||
{
|
||||
// Take 2 recent published videos from each popular show
|
||||
var recentVideos = publishedVideos.OrderByDescending(v => v.Date).Take(2);
|
||||
foreach (var video in recentVideos)
|
||||
{
|
||||
if (video.Urn != null)
|
||||
{
|
||||
urns.Add(video.Urn);
|
||||
_logger.LogInformation(
|
||||
"Added trending video from show {Show}: {Title} (URN: {Urn}, Date: {Date}, ValidFrom: {ValidFrom}, ValidTo: {ValidTo})",
|
||||
show.Title,
|
||||
video.Title,
|
||||
video.Urn,
|
||||
video.Date,
|
||||
video.ValidFrom,
|
||||
video.ValidTo);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogWarning("Show {Show}: Trending video has null URN - {Title}", show.Title, video.Title);
|
||||
}
|
||||
}
|
||||
}
|
||||
_logger.LogDebug("Show {Show} ({ShowId}): No videos returned from API", show.Title, show.Id);
|
||||
continue;
|
||||
}
|
||||
else
|
||||
|
||||
_logger.LogDebug("Show {Show} ({ShowId}): Found {Count} videos", show.Title, show.Id, videos.Count);
|
||||
|
||||
// Filter to videos that are actually published (validFrom in the past)
|
||||
var publishedVideos = videos
|
||||
.Where(v => v.ValidFrom == null || v.ValidFrom.Value.ToUniversalTime() <= now)
|
||||
.OrderByDescending(v => v.Date)
|
||||
.Take(videosPerShow)
|
||||
.ToList();
|
||||
|
||||
foreach (var video in publishedVideos)
|
||||
{
|
||||
_logger.LogDebug("Show {Show} ({ShowId}): No videos returned from API for trending", show.Title, show.Id);
|
||||
if (video.Urn != null)
|
||||
{
|
||||
urns.Add(video.Urn);
|
||||
_logger.LogDebug(
|
||||
"Added {ContentType} video from show {Show}: {Title} (URN: {Urn})",
|
||||
contentType,
|
||||
show.Title,
|
||||
video.Title,
|
||||
video.Urn);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Error fetching videos for show {ShowId}", show.Id);
|
||||
}
|
||||
|
||||
// Respect cancellation
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_logger.LogInformation("Refreshed {Count} trending content items from {ShowCount} shows", urns.Count, popularShows.Count);
|
||||
_logger.LogInformation("Refreshed {Count} {ContentType} content items from {ShowCount} shows", urns.Count, contentType, filteredShows.Count);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error refreshing trending content");
|
||||
_logger.LogError(ex, "Error refreshing {ContentType} content", contentType);
|
||||
}
|
||||
|
||||
return urns;
|
||||
|
||||
Reference in New Issue
Block a user