Files
VDownload/VDownload.Core/Services/Source.cs

55 lines
1.8 KiB
C#
Raw Normal View History

2022-03-02 22:13:28 +01:00
using System.Text.RegularExpressions;
2022-02-26 14:32:34 +01:00
using VDownload.Core.Enums;
namespace VDownload.Core.Services
{
public class Source
{
#region CONSTANTS
2022-03-02 22:13:28 +01:00
// VIDEO SOURCES REGULAR EXPRESSIONS
2022-02-26 14:32:34 +01:00
private static readonly (Regex Regex, VideoSource Type)[] VideoSources = new (Regex Regex, VideoSource Type)[]
{
(new Regex(@"^https://www.twitch.tv/videos/(?<id>\d+)"), VideoSource.TwitchVod),
(new Regex(@"^https://www.twitch.tv/\S+/clip/(?<id>[^?]+)"), VideoSource.TwitchClip),
(new Regex(@"^https://clips.twitch.tv/(?<id>[^?]+)"), VideoSource.TwitchClip),
};
2022-03-02 22:13:28 +01:00
// PLAYLIST SOURCES REGULAR EXPRESSIONS
2022-02-26 14:32:34 +01:00
private static readonly (Regex Regex, PlaylistSource Type)[] PlaylistSources = new (Regex Regex, PlaylistSource Type)[]
{
(new Regex(@"^https://www.twitch.tv/(?<id>[^?/]+)"), PlaylistSource.TwitchChannel),
2022-02-26 14:32:34 +01:00
};
#endregion
#region METHODS
2022-03-02 22:13:28 +01:00
// GET VIDEO SOURCE
2022-02-26 14:32:34 +01:00
public static (VideoSource Type, string ID) GetVideoSource(string url)
{
foreach ((Regex Regex, VideoSource Type) Source in VideoSources)
{
Match sourceMatch = Source.Regex.Match(url);
if (sourceMatch.Success) return (Source.Type, sourceMatch.Groups["id"].Value);
}
return (VideoSource.Null, null);
}
2022-03-02 22:13:28 +01:00
// GET PLAYLIST SOURCE
2022-02-26 14:32:34 +01:00
public static (PlaylistSource Type, string ID) GetPlaylistSource(string url)
{
foreach ((Regex Regex, PlaylistSource Type) Source in PlaylistSources)
{
Match sourceMatch = Source.Regex.Match(url);
if (sourceMatch.Success) return (Source.Type, sourceMatch.Groups["id"].Value);
}
return (PlaylistSource.Null, null);
}
#endregion
}
}