1
0

Implemented CC retrieval

This commit is contained in:
Michael Gordeev
2019-11-30 23:15:53 +03:00
parent f55af5c276
commit ceab79255f
6 changed files with 92 additions and 3 deletions
@@ -0,0 +1,19 @@
using NUnit.Framework;
using System.Linq;
using YouTube.Models;
namespace YouTube.API.Test
{
public class ClosedCaptionsTest
{
[Test]
public void ValidCaptionsTest()
{
YouTubeService service = new YouTubeService();
ClosedCaptionInfo info = service.VideoPlayback.List("VC5-YkjMHuw").Execute().ClosedCaptions.FirstOrDefault();
ClosedCaptionTrack track = service.Captions.Load(info).Execute();
Assert.IsNotNull(track);
Assert.IsNotEmpty(track.Captions);
}
}
}
@@ -1,4 +1,5 @@
using System.Globalization; using System.Globalization;
using YoutubeExplode.Models.ClosedCaptions;
namespace YouTube.Models namespace YouTube.Models
{ {
@@ -7,5 +8,7 @@ namespace YouTube.Models
public CultureInfo Language { get; set; } public CultureInfo Language { get; set; }
public string Url { get; set; } public string Url { get; set; }
public bool AutoGenerated { get; set; } public bool AutoGenerated { get; set; }
internal ClosedCaptionTrackInfo TrackInfo { get; set; }
} }
} }
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace YouTube.Models
{
public class ClosedCaptionTrack
{
public ClosedCaptionInfo Info { get; set; }
public IReadOnlyList<ClosedCaption> Captions { get; set; }
public class ClosedCaption
{
public TimeSpan Offset { get; set; }
public TimeSpan Duration { get; set; }
public string Content { get; set; }
}
}
}
@@ -1,11 +1,57 @@
using Google.Apis.Services; using Google.Apis.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using YouTube.Models;
using YoutubeExplode;
namespace YouTube.Resources namespace YouTube.Resources
{ {
public class CaptionsResource public class CaptionsResource : Google.Apis.YouTube.v3.CaptionsResource
{ {
IClientService Service { get; } IClientService Service { get; }
public CaptionsResource(IClientService service) => public CaptionsResource(IClientService service) : base(service) =>
Service = 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.GetClosedCaptionTrackAsync(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;
}
}
} }
} }
@@ -87,7 +87,8 @@ namespace YouTube.Resources
{ {
AutoGenerated = i.IsAutoGenerated, AutoGenerated = i.IsAutoGenerated,
Url = i.Url, Url = i.Url,
Language = new CultureInfo(i.Language.Code) Language = new CultureInfo(i.Language.Code),
TrackInfo = i
}); });
item.ClosedCaptions = captions.AsReadOnly(); item.ClosedCaptions = captions.AsReadOnly();
@@ -6,6 +6,7 @@ namespace YouTube
{ {
public DashManifestsResource DashManifests => new DashManifestsResource(this); public DashManifestsResource DashManifests => new DashManifestsResource(this);
public VideoPlaybackResource VideoPlayback => new VideoPlaybackResource(this); public VideoPlaybackResource VideoPlayback => new VideoPlaybackResource(this);
public new CaptionsResource Captions => new CaptionsResource(this);
public HistoryResource History { get; } public HistoryResource History { get; }
public WatchLaterResource WatchLater { get; } public WatchLaterResource WatchLater { get; }
// TODO: Add Activities override for recomendations and subscriptions and implementation of cc retrieval // TODO: Add Activities override for recomendations and subscriptions and implementation of cc retrieval