source search refactoring
This commit is contained in:
@@ -1,10 +0,0 @@
|
||||
using VDownload.Models;
|
||||
|
||||
namespace VDownload.Sources.Common
|
||||
{
|
||||
public interface ISourceSearchService
|
||||
{
|
||||
Task<Video> SearchVideo(string url);
|
||||
Task<Playlist> SearchPlaylist(string url, int maxVideoCount);
|
||||
}
|
||||
}
|
||||
@@ -4,13 +4,17 @@ using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using VDownload.Models;
|
||||
|
||||
namespace VDownload.Sources.Common
|
||||
{
|
||||
public struct SearchRegex
|
||||
public class SearchRegex<TFunc> where TFunc : Delegate
|
||||
{
|
||||
public Regex Regex { get; set; }
|
||||
public Func<string, Task<object>> SearchFunction { get; set; }
|
||||
#region PROPERTIES
|
||||
|
||||
public required Regex Regex { get; init; }
|
||||
|
||||
public TFunc SearchFunction { get; set; }
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using VDownload.Models;
|
||||
|
||||
namespace VDownload.Sources.Common
|
||||
{
|
||||
public class SearchRegexPlaylist : SearchRegex<Func<string, int, Task<Playlist>>>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using VDownload.Models;
|
||||
|
||||
namespace VDownload.Sources.Common
|
||||
{
|
||||
public class SearchRegexVideo : SearchRegex<Func<string, Task<Video>>>
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using VDownload.Models;
|
||||
|
||||
namespace VDownload.Sources.Common
|
||||
{
|
||||
public interface ISourceSearchService
|
||||
{
|
||||
Task<Video> SearchVideo(string url);
|
||||
Task<Playlist> SearchPlaylist(string url, int maxVideoCount);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public abstract class SourceSearchService : ISourceSearchService
|
||||
{
|
||||
#region PUBLIC METHODS
|
||||
|
||||
public async Task<Video> SearchVideo(string url)
|
||||
{
|
||||
foreach (SearchRegexVideo regex in GetVideoRegexes())
|
||||
{
|
||||
Match match = regex.Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
string id = match.Groups[1].Value;
|
||||
Video video = await regex.SearchFunction.Invoke(id);
|
||||
return video;
|
||||
}
|
||||
}
|
||||
throw new MediaSearchException("Invalid url"); // TODO : Change to string resource
|
||||
}
|
||||
|
||||
public async Task<Playlist> SearchPlaylist(string url, int maxVideoCount)
|
||||
{
|
||||
foreach (SearchRegexPlaylist regex in GetPlaylistRegexes())
|
||||
{
|
||||
Match match = regex.Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
string id = match.Groups[1].Value;
|
||||
Playlist video = await regex.SearchFunction.Invoke(id, maxVideoCount);
|
||||
return video;
|
||||
}
|
||||
}
|
||||
throw new MediaSearchException("Invalid url"); // TODO : Change to string resource
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
protected abstract IEnumerable<SearchRegexVideo> GetVideoRegexes();
|
||||
|
||||
protected abstract IEnumerable<SearchRegexPlaylist> GetPlaylistRegexes();
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -21,13 +21,11 @@ namespace VDownload.Sources.Twitch
|
||||
{
|
||||
public interface ITwitchSearchService : ISourceSearchService
|
||||
{
|
||||
new Task<TwitchPlaylist> SearchPlaylist(string url, int maxVideoCount);
|
||||
new Task<TwitchVideo> SearchVideo(string url);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public class TwitchSearchService : ITwitchSearchService
|
||||
public class TwitchSearchService : SourceSearchService, ITwitchSearchService
|
||||
{
|
||||
#region SERVICES
|
||||
|
||||
@@ -36,8 +34,6 @@ namespace VDownload.Sources.Twitch
|
||||
protected readonly ITwitchAuthenticationService _twitchAuthenticationService;
|
||||
protected readonly ITwitchVideoStreamFactoryService _videoStreamFactoryService;
|
||||
|
||||
protected readonly Configuration.Models.Search _searchConfiguration;
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
@@ -50,53 +46,6 @@ namespace VDownload.Sources.Twitch
|
||||
_apiService = apiService;
|
||||
_twitchAuthenticationService = authenticationService;
|
||||
_videoStreamFactoryService = videoStreamFactoryService;
|
||||
|
||||
_searchConfiguration = _configurationService.Twitch.Search;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
|
||||
#region PUBLIC METHODS
|
||||
|
||||
async Task<Video> ISourceSearchService.SearchVideo(string url) => await SearchVideo(url);
|
||||
public async Task<TwitchVideo> SearchVideo(string url)
|
||||
{
|
||||
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.Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
string id = match.Groups[1].Value;
|
||||
return (TwitchVideo)await regex.SearchFunction.Invoke(id);
|
||||
}
|
||||
}
|
||||
throw new MediaSearchException("Invalid url"); // TODO : Change to string resource
|
||||
}
|
||||
|
||||
async Task<Playlist> ISourceSearchService.SearchPlaylist(string url, int maxVideoCount) => await SearchPlaylist(url, maxVideoCount);
|
||||
public async Task<TwitchPlaylist> SearchPlaylist(string url, int maxVideoCount)
|
||||
{
|
||||
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.Regex.Match(url);
|
||||
if (match.Success)
|
||||
{
|
||||
string id = match.Groups[1].Value;
|
||||
return (TwitchPlaylist)await regex.SearchFunction.Invoke(id);
|
||||
}
|
||||
}
|
||||
throw new MediaSearchException("Invalid url"); // TODO : Change to string resource
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -105,6 +54,33 @@ namespace VDownload.Sources.Twitch
|
||||
|
||||
#region PRIVATE METHODS
|
||||
|
||||
protected override IEnumerable<SearchRegexVideo> GetVideoRegexes()
|
||||
{
|
||||
return [
|
||||
.._configurationService.Twitch.Search.Vod.Regexes.Select(x => new SearchRegexVideo
|
||||
{
|
||||
Regex = new Regex(x),
|
||||
SearchFunction = async (id) => await GetVod(id)
|
||||
}),
|
||||
.._configurationService.Twitch.Search.Clip.Regexes.Select(x => new SearchRegexVideo
|
||||
{
|
||||
Regex = new Regex(x),
|
||||
SearchFunction = async (id) => await GetClip(id)
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
protected override IEnumerable<SearchRegexPlaylist> GetPlaylistRegexes()
|
||||
{
|
||||
return [
|
||||
.. _configurationService.Twitch.Search.Channel.Regexes.Select(x => new SearchRegexPlaylist
|
||||
{
|
||||
Regex = new Regex(x),
|
||||
SearchFunction = async (id, maxVideoCount) => await GetChannel(id, maxVideoCount)
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
protected async Task<TwitchVod> GetVod(string id)
|
||||
{
|
||||
byte[] token = await GetToken();
|
||||
@@ -167,7 +143,7 @@ namespace VDownload.Sources.Twitch
|
||||
return channel;
|
||||
}
|
||||
|
||||
public async Task<TwitchVod> ParseVod(Api.Helix.GetVideos.Response.Data data)
|
||||
protected async Task<TwitchVod> ParseVod(Api.Helix.GetVideos.Response.Data data)
|
||||
{
|
||||
Task<IEnumerable<TwitchVodStream>> streamsTask = GetVodStreams(data.Id);
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace VDownload.Sources
|
||||
{
|
||||
_urlMappings =
|
||||
[
|
||||
.. configurationService.Twitch.Search.GeneralRegexes.Select(x => (new Regex(x), (ISourceSearchService)twitchSearchService)),
|
||||
.. configurationService.Twitch.Search.GeneralRegexes.Select(x => (new Regex(x), twitchSearchService)),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user