twitch clips support added
This commit is contained in:
@@ -8,7 +8,9 @@ using VDownload.Models;
|
||||
using VDownload.Services.Data.Configuration;
|
||||
using VDownload.Sources.Common;
|
||||
using VDownload.Sources.Twitch.Api;
|
||||
using VDownload.Sources.Twitch.Api.GQL.GetClipToken.Response;
|
||||
using VDownload.Sources.Twitch.Api.GQL.GetVideoToken.Response;
|
||||
using VDownload.Sources.Twitch.Api.Helix.GetClips.Response;
|
||||
using VDownload.Sources.Twitch.Api.Helix.GetUsers.Response;
|
||||
using VDownload.Sources.Twitch.Api.Helix.GetVideos.Response;
|
||||
using VDownload.Sources.Twitch.Authentication;
|
||||
@@ -34,6 +36,8 @@ namespace VDownload.Sources.Twitch
|
||||
protected readonly ITwitchAuthenticationService _twitchAuthenticationService;
|
||||
protected readonly ITwitchVideoStreamFactoryService _videoStreamFactoryService;
|
||||
|
||||
protected readonly Configuration.Models.Search _searchConfiguration;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -46,6 +50,8 @@ namespace VDownload.Sources.Twitch
|
||||
_apiService = apiService;
|
||||
_twitchAuthenticationService = authenticationService;
|
||||
_videoStreamFactoryService = videoStreamFactoryService;
|
||||
|
||||
_searchConfiguration = _configurationService.Twitch.Search;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -57,13 +63,18 @@ namespace VDownload.Sources.Twitch
|
||||
async Task<Video> ISourceSearchService.SearchVideo(string url) => await SearchVideo(url);
|
||||
public async Task<TwitchVideo> SearchVideo(string url)
|
||||
{
|
||||
foreach (Regex regex in _configurationService.Twitch.Search.Vod.Regexes.Select(x => new Regex(x)))
|
||||
List<SearchRegex> regexes =
|
||||
[
|
||||
.. _searchConfiguration.Vod.Regexes.Select(x => new SearchRegex { Regex = new Regex(x), SearchFunction = async (id) => await GetVod(id) }),
|
||||
.. _searchConfiguration.Clip.Regexes.Select(x => new SearchRegex { Regex = new Regex(x), SearchFunction = async (id) => await GetClip(id) }),
|
||||
];
|
||||
foreach (SearchRegex regex in regexes)
|
||||
{
|
||||
Match match = regex.Match(url);
|
||||
Match match = regex.Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
string id = match.Groups[1].Value;
|
||||
return await GetVod(id);
|
||||
return (TwitchVideo)await regex.SearchFunction.Invoke(id);
|
||||
}
|
||||
}
|
||||
throw new MediaSearchException("Invalid url"); // TODO : Change to string resource
|
||||
@@ -72,16 +83,20 @@ namespace VDownload.Sources.Twitch
|
||||
async Task<Playlist> ISourceSearchService.SearchPlaylist(string url, int maxVideoCount) => await SearchPlaylist(url, maxVideoCount);
|
||||
public async Task<TwitchPlaylist> SearchPlaylist(string url, int maxVideoCount)
|
||||
{
|
||||
foreach (Regex regex in _configurationService.Twitch.Search.Channel.Regexes.Select(x => new Regex(x)))
|
||||
List<SearchRegex> regexes =
|
||||
[
|
||||
.. _searchConfiguration.Channel.Regexes.Select(x => new SearchRegex { Regex = new Regex(x), SearchFunction = async (id) => await GetChannel(id, maxVideoCount) }),
|
||||
];
|
||||
foreach (SearchRegex regex in regexes)
|
||||
{
|
||||
Match match = regex.Match(url);
|
||||
Match match = regex.Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
string id = match.Groups[1].Value;
|
||||
return await GetChannel(id, maxVideoCount);
|
||||
return (TwitchPlaylist)await regex.SearchFunction.Invoke(id);
|
||||
}
|
||||
}
|
||||
throw new MediaSearchException("Invalid url");
|
||||
throw new MediaSearchException("Invalid url"); // TODO : Change to string resource
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -103,6 +118,19 @@ namespace VDownload.Sources.Twitch
|
||||
return vod;
|
||||
}
|
||||
|
||||
protected async Task<TwitchClip> GetClip(string id)
|
||||
{
|
||||
byte[] token = await GetToken();
|
||||
|
||||
GetClipsResponse info = await _apiService.HelixGetClip(id, token);
|
||||
|
||||
Api.Helix.GetClips.Response.Data clipResponse = info.Data[0];
|
||||
|
||||
TwitchClip clip = await ParseClip(clipResponse);
|
||||
|
||||
return clip;
|
||||
}
|
||||
|
||||
protected async Task<TwitchChannel> GetChannel(string id, int count)
|
||||
{
|
||||
byte[] token = await GetToken();
|
||||
@@ -171,6 +199,31 @@ namespace VDownload.Sources.Twitch
|
||||
return vod;
|
||||
}
|
||||
|
||||
protected async Task<TwitchClip> ParseClip(Api.Helix.GetClips.Response.Data data)
|
||||
{
|
||||
Task<IEnumerable<TwitchClipStream>> streamsTask = GetClipStreams(data.Id);
|
||||
|
||||
TwitchClip clip = new TwitchClip
|
||||
{
|
||||
Title = data.Title,
|
||||
Author = data.BroadcasterName,
|
||||
Creator = data.CreatorName,
|
||||
PublishDate = data.CreatedAt,
|
||||
Duration = TimeSpan.FromSeconds(Math.Round(data.Duration)),
|
||||
Views = data.ViewCount,
|
||||
ThumbnailUrl = new Uri(data.ThumbnailUrl),
|
||||
Url = new Uri(data.Url),
|
||||
};
|
||||
|
||||
await streamsTask;
|
||||
foreach (TwitchClipStream stream in streamsTask.Result)
|
||||
{
|
||||
clip.Streams.Add(stream);
|
||||
}
|
||||
|
||||
return clip;
|
||||
}
|
||||
|
||||
protected async Task<IEnumerable<TwitchVodStream>> GetVodStreams(string id)
|
||||
{
|
||||
GetVideoTokenResponse videoToken = await _apiService.GQLGetVideoToken(id);
|
||||
@@ -195,6 +248,25 @@ namespace VDownload.Sources.Twitch
|
||||
return streams;
|
||||
}
|
||||
|
||||
protected async Task<IEnumerable<TwitchClipStream>> GetClipStreams(string id)
|
||||
{
|
||||
GetClipTokenResponse clipToken = await _apiService.GQLGetClipToken(id);
|
||||
|
||||
List<TwitchClipStream> streams = new List<TwitchClipStream>();
|
||||
foreach (GetClipTokenVideoQuality streamData in clipToken.Data.Clip.VideoQualities)
|
||||
{
|
||||
TwitchClipStream stream = _videoStreamFactoryService.CreateClipStream();
|
||||
stream.Name = $"{streamData.Quality}p{Math.Round(streamData.FrameRate)}";
|
||||
stream.Height = int.Parse(streamData.Quality);
|
||||
stream.FrameRate = streamData.FrameRate;
|
||||
stream.Url = new Uri(streamData.SourceURL);
|
||||
stream.Signature = clipToken.Data.Clip.PlaybackAccessToken.Signature;
|
||||
stream.Token = clipToken.Data.Clip.PlaybackAccessToken.Value;
|
||||
streams.Add(stream);
|
||||
}
|
||||
return streams;
|
||||
}
|
||||
|
||||
protected TimeSpan ParseVodDuration(string duration)
|
||||
{
|
||||
IEnumerable<string> parts = duration.Split(['h', 'm', 's'])[..^1].Reverse();
|
||||
|
||||
@@ -6,7 +6,8 @@ namespace VDownload.Sources.Twitch
|
||||
{
|
||||
public interface ITwitchVideoStreamFactoryService
|
||||
{
|
||||
TwitchVodStream CreateVodStream();
|
||||
TwitchVodStream CreateVodStream();
|
||||
TwitchClipStream CreateClipStream();
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +42,8 @@ namespace VDownload.Sources.Twitch
|
||||
|
||||
public TwitchVodStream CreateVodStream() => new TwitchVodStream(_httpClient, _configurationService, _settingsService);
|
||||
|
||||
public TwitchClipStream CreateClipStream() => new TwitchClipStream(_httpClient, _configurationService, _settingsService);
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user