1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
YouTubeScraper/YouTube.API/Resources/CaptionsResource.cs
T
2020-05-10 15:11:42 +03:00

57 lines
1.5 KiB
C#

using Google.Apis.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using YouTube.Models;
using YoutubeExplode;
namespace YouTube.Resources
{
public class CaptionsResource : Google.Apis.YouTube.v3.CaptionsResource
{
IClientService Service { get; }
public CaptionsResource(IClientService service) : base(service) =>
Service = service;
public LoadRequest Load(ClosedCaptionInfo captionInfo) =>
new LoadRequest(Service, captionInfo);
public class LoadRequest
{
public ClosedCaptionInfo CaptionInfo { get; set; }
IClientService Service { get; set; }
public LoadRequest(IClientService service, ClosedCaptionInfo captionInfo)
{
CaptionInfo = captionInfo;
Service = service;
}
public async Task<ClosedCaptionTrack> ExecuteAsync()
{
YoutubeClient client = new YoutubeClient(Service.HttpClient);
var response = await client.Videos.ClosedCaptions.GetAsync(CaptionInfo.TrackInfo);
List<ClosedCaptionTrack.ClosedCaption> captions = new List<ClosedCaptionTrack.ClosedCaption>();
foreach (var i in response.Captions)
captions.Add(new ClosedCaptionTrack.ClosedCaption
{
Offset = i.Offset,
Duration = i.Duration,
Content = i.Text
});
return new ClosedCaptionTrack
{
Info = CaptionInfo,
Captions = captions.AsReadOnly()
};
}
public ClosedCaptionTrack Execute()
{
Task<ClosedCaptionTrack> task = ExecuteAsync();
task.Wait();
return task.Result;
}
}
}
}