Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scopegg module clip link fix #10

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions den0bot/Modules/ModScopeGG.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// den0bot (c) StanR 2024 - MIT License

using System;
using System.Net.Http;
using System.Text.RegularExpressions;
Expand All @@ -21,15 +22,12 @@ public ModScopeGG(ILogger<IModule> logger) : base(logger)
[GeneratedRegex(@"(https:\/\/app\.scope\.gg([\w/]*[\w/])?)")]
private static partial Regex ScopeLinkRegex();

[GeneratedRegex(@"(https:\/\/mediacdn\.allstar\.gg([a-zA-Z0-9/]*[a-zA-Z0-9/])?)")]
[GeneratedRegex(@"(https:\/\/hl\.xplay\.cloud\/video([a-zA-Z0-9/]*[a-zA-Z0-9/])?.mp4)")]
private static partial Regex ClipLinkRegex();

[GeneratedRegex(@"(?>thumbs|og)")]
private static partial Regex ClipLinkReplaceRegex();
[GeneratedRegex(@"""clipTitle"":\s+?""(.+?)"",")]
private static partial Regex ClipTitleCodeRegex();

[GeneratedRegex(@"""clipTitle"":""(.+?)"",")]
private static partial Regex ClipTitleRegex();

public async Task ReceiveMessage(Message message)
{
if (string.IsNullOrEmpty(message.Text))
Expand All @@ -40,14 +38,24 @@ public async Task ReceiveMessage(Message message)
{
using var client = new HttpClient();
var page = await client.GetStringAsync(regexMatch.Value);

var match = ClipLinkRegex().Match(page);
var videoLink = string.Concat(ClipLinkReplaceRegex().Replace(match.Value, "clips"), ".mp4");
var videoLink = match.Value;

if (Uri.TryCreate(videoLink, UriKind.Absolute, out var videoUri))
{
var clipMatch = ClipTitleRegex().Match(page);
var caption = clipMatch.Success ? clipMatch.Groups[1].Value : "VAC";
var clipTitleCodeMatch = ClipTitleCodeRegex().Match(page);
string caption;
if (clipTitleCodeMatch.Success)
{
Regex clipTitleRegex = new Regex(ClipTitlePattern(clipTitleCodeMatch.Groups[1].Value));
var clipTitleMatch = clipTitleRegex.Match(page);
caption = clipTitleMatch.Success ? clipTitleMatch.Groups[1].Value : "VAC";
}
else
{
caption = "VAC";
}

if (!await API.SendVideo(videoLink, message.Chat.Id, caption, message.MessageId))
{
Expand All @@ -64,5 +72,9 @@ public async Task ReceiveMessage(Message message)
}
}

private static string ClipTitlePattern(string clipTitleCode)
{
return @"""" + Regex.Escape(clipTitleCode) + @""":\s+?""(.+?)"",";
}
}
}
Loading