|
1 | | -using FitSyncHub.Youtube.Options; |
2 | | -using Google.Apis.YouTube.v3; |
3 | | -using Microsoft.Extensions.Options; |
| 1 | +using Google.Apis.YouTube.v3; |
| 2 | +using Google.Apis.YouTube.v3.Data; |
4 | 3 |
|
5 | 4 | namespace FitSyncHub.Youtube.Services; |
6 | 5 |
|
7 | 6 | public class YouTubeLiveService |
8 | 7 | { |
9 | 8 | private readonly YouTubeService _youtubeService; |
10 | | - private readonly string _channelId; |
11 | 9 |
|
12 | | - public YouTubeLiveService(YouTubeService youTubeService, IOptions<YoutubeOptions> options) |
| 10 | + public YouTubeLiveService(YouTubeService youTubeService) |
13 | 11 | { |
14 | 12 | _youtubeService = youTubeService; |
15 | | - _channelId = options.Value.ChannelId; |
16 | 13 | } |
17 | 14 |
|
18 | 15 | public async Task<string?> GetNextUpcomingVideoId(CancellationToken cancellationToken) |
19 | 16 | { |
20 | | - var searchListRequest = _youtubeService.Search.List("snippet"); |
21 | | - searchListRequest.ChannelId = _channelId; |
22 | | - searchListRequest.EventType = SearchResource.ListRequest.EventTypeEnum.Upcoming; |
23 | | - searchListRequest.Type = "video"; |
| 17 | + var liveBroadcastsRequest = _youtubeService.LiveBroadcasts.List("snippet"); |
| 18 | + liveBroadcastsRequest.BroadcastStatus = LiveBroadcastsResource.ListRequest.BroadcastStatusEnum.Upcoming; |
24 | 19 |
|
25 | | - var searchResponse = await searchListRequest.ExecuteAsync(cancellationToken); |
| 20 | + var response = await GetAllPagesAsync(liveBroadcastsRequest, cancellationToken); |
26 | 21 |
|
27 | | - var upcomingVideoIds = searchResponse.Items.Select(item => item.Id.VideoId).ToHashSet(); |
28 | | - var videosListRequest = _youtubeService.Videos.List("liveStreamingDetails"); |
29 | | - videosListRequest.Id = new Google.Apis.Util.Repeatable<string>(upcomingVideoIds); |
30 | | - |
31 | | - var videosListResponse = await videosListRequest.ExecuteAsync(cancellationToken); |
32 | | - |
33 | | - return videosListResponse.Items |
34 | | - .OrderBy(x => x.LiveStreamingDetails.ScheduledStartTimeDateTimeOffset) |
| 22 | + return response |
| 23 | + .OrderBy(x => x.Snippet.ScheduledStartTimeDateTimeOffset) |
35 | 24 | .Select(x => x.Id) |
36 | 25 | .FirstOrDefault(); |
37 | 26 | } |
38 | 27 |
|
39 | 28 | public async Task<string?> GetLiveVideoId(CancellationToken cancellationToken) |
40 | 29 | { |
41 | | - var searchListRequest = _youtubeService.Search.List("snippet"); |
42 | | - searchListRequest.ChannelId = _channelId; |
43 | | - searchListRequest.EventType = SearchResource.ListRequest.EventTypeEnum.Live; |
44 | | - searchListRequest.Type = "video"; |
| 30 | + var liveBroadcastsRequest = _youtubeService.LiveBroadcasts.List("snippet"); |
| 31 | + liveBroadcastsRequest.BroadcastStatus = LiveBroadcastsResource.ListRequest.BroadcastStatusEnum.Active; |
| 32 | + |
| 33 | + var response = await GetAllPagesAsync(liveBroadcastsRequest, cancellationToken); |
| 34 | + |
| 35 | + return response.Select(x => x.Id).SingleOrDefault(); |
| 36 | + } |
| 37 | + |
| 38 | + private static async Task<List<LiveBroadcast>> GetAllPagesAsync( |
| 39 | + LiveBroadcastsResource.ListRequest liveBroadcastsRequest, CancellationToken cancellationToken) |
| 40 | + { |
| 41 | + string? nextPageToken = null; |
| 42 | + List<LiveBroadcast> result = []; |
| 43 | + |
| 44 | + do |
| 45 | + { |
| 46 | + liveBroadcastsRequest.PageToken = nextPageToken; |
| 47 | + |
| 48 | + var response = await liveBroadcastsRequest.ExecuteAsync(cancellationToken); |
| 49 | + result.AddRange(response.Items); |
45 | 50 |
|
46 | | - var searchResponse = await searchListRequest.ExecuteAsync(cancellationToken); |
| 51 | + nextPageToken = response.NextPageToken; |
| 52 | + } while (!string.IsNullOrEmpty(nextPageToken)); |
47 | 53 |
|
48 | | - return searchResponse.Items.FirstOrDefault()?.Id.VideoId; |
| 54 | + return result; |
49 | 55 | } |
50 | 56 | } |
0 commit comments