Skip to content

Commit

Permalink
Change when thumbnail sync is done, also sync other details if alread…
Browse files Browse the repository at this point in the history
…y pulling metadata
  • Loading branch information
Foxocube committed Jan 10, 2025
1 parent 4b719a8 commit 0e2d5c7
Showing 1 changed file with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using System.Xml;
using Google.Apis.YouTube.v3;
using MassTransit;
Expand All @@ -10,7 +11,8 @@ namespace MediaFeeder.Providers.Youtube;
public class YoutubeActualVideoSynchroniseConsumer(
ILogger<YoutubeActualVideoSynchroniseConsumer> logger,
IDbContextFactory<MediaFeederDataContext> contextFactory,
YouTubeService youTubeService
YouTubeService youTubeService,
Utils utils
) : IConsumer<YoutubeActualVideoSynchroniseContract>
{
public async Task Consume(ConsumeContext<YoutubeActualVideoSynchroniseContract> context)
Expand Down Expand Up @@ -62,7 +64,7 @@ public async Task Consume(ConsumeContext<YoutubeActualVideoSynchroniseContract>
}
}

if (video.Duration == 0)
if (video.Duration == 0 || string.IsNullOrWhiteSpace(video.Thumb))
{
var videoRequest = youTubeService.Videos.List("id,statistics,contentDetails");
videoRequest.Id = video.VideoId;
Expand All @@ -79,19 +81,24 @@ public async Task Consume(ConsumeContext<YoutubeActualVideoSynchroniseContract>
}

if (videoStats?.Snippet != null)
{
video.Description = videoStats.Snippet.Description;
video.Name = videoStats.Snippet.Title;
}

if (videoStats?.Snippet?.Thumbnails != null)
video.Thumb = await utils.LoadResourceThumbnail(video.VideoId, "video", videoStats.Snippet.Thumbnails, logger, context.CancellationToken);

logger.LogInformation("Got state for video from YT: {}", JsonSerializer.Serialize(videoStats));
}

await db.SaveChangesAsync(context.CancellationToken);
}
}

public class YoutubeVideoSynchroniseConsumer(
ILogger<YoutubeVideoSynchroniseConsumer> logger,
IDbContextFactory<MediaFeederDataContext> contextFactory,
IBus bus,
Utils utils,
YouTubeService youTubeService
IBus bus
) : IConsumer<YoutubeVideoSynchroniseContract>
{
public async Task Consume(ConsumeContext<YoutubeVideoSynchroniseContract> context)
Expand All @@ -102,17 +109,6 @@ public async Task Consume(ConsumeContext<YoutubeVideoSynchroniseContract> contex
if (video.DownloadedPath != null || video.Duration == 0 || string.IsNullOrWhiteSpace(video.Thumb))
await bus.Publish(new YoutubeActualVideoSynchroniseContract(video.Id), context.CancellationToken);

if (video.Thumb == null)
{
var videoRequest = youTubeService.Videos.List("snippet");
videoRequest.Id = video.VideoId;
var videoResponse = await videoRequest.ExecuteAsync(context.CancellationToken);
var videoStats = videoResponse.Items.SingleOrDefault();

if (videoStats != null)
video.Thumb = await utils.LoadResourceThumbnail(video.VideoId, "video", videoStats.Snippet.Thumbnails, logger, context.CancellationToken);
}

await db.SaveChangesAsync(context.CancellationToken);
}
}

0 comments on commit 0e2d5c7

Please sign in to comment.