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

feat: better social link embed (vxtwitter, ddinstagram) #78

Merged
merged 4 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
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
39 changes: 39 additions & 0 deletions BotNet.Services/SocialLink/SocialLinkEmbedFixer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace BotNet.Services.SocialLink {
public partial class SocialLinkEmbedFixer {
public static Uri Fix(Uri link) {
string url = link.ToString();
string newUrl = link.Host switch {
"twitter.com" => url.Replace("//twitter.com/", "//vxtwitter.com/"),
"www.twitter.com" => url.Replace("//www.twitter.com/", "//www.vxtwitter.com/"),
"x.com" => url.Replace("//x.com/", "//vxtwitter.com/"),
"www.x.com" => url.Replace("//www.x.com/", "//www.vxtwitter.com/"),
"instagram.com" => url.Replace("//instagram.com/", "//ddinstagram.com/"),
"www.instagram.com" => url.Replace("//www.instagram.com/", "//www.ddinstagram.com/"),
_ => url
};
return new Uri(newUrl);
}

public static IEnumerable<Uri> GetPossibleUrls(string message) {
MatchCollection twitterMatches = TwitterRegex().Matches(message);
foreach (Match match in twitterMatches) {
yield return new Uri(match.Value);
}

MatchCollection instagramMatches = InstagramRegex().Matches(message);
foreach (Match match in instagramMatches) {
yield return new Uri(match.Value);
}
}

[GeneratedRegex(@"(?:https?:\/\/)?(?:www.)?(?:twitter\.com|x\.com)\/(?:[a-zA-Z0-9_]+\/status\/)?[0-9]+")]
private static partial Regex TwitterRegex();

[GeneratedRegex(@"(?:https?:\/\/)?(?:www.)?instagram.com\/?([a-zA-Z0-9\._\-]+)?\/([p]+)?([reel]+)?([tv]+)?([stories]+)?\/([a-zA-Z0-9\-_\.]+)\/?([0-9]+)?")]
private static partial Regex InstagramRegex();
}
}
51 changes: 51 additions & 0 deletions BotNet.Tests/Services/SocialLink/SocialLinkEmbedFixerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BotNet.Services.SocialLink;
using FluentAssertions;
using Xunit;

namespace BotNet.Tests.Services.SocialLink {
public class SocialLinkEmbedFixerTests {
[Theory]
[InlineData("https://www.instagram.com/reel/C0XXKVnpRUI/", "https://www.ddinstagram.com/reel/C0XXKVnpRUI/")]
[InlineData("https://instagram.com/reel/C0XXKVnpRUI/", "https://ddinstagram.com/reel/C0XXKVnpRUI/")]
[InlineData("https://twitter.com/ShowwcaseHQ/status/1556259601829576707?t=S6GuFx37mAXOLI2wdusfXg&s=19",
"https://vxtwitter.com/ShowwcaseHQ/status/1556259601829576707?t=S6GuFx37mAXOLI2wdusfXg&s=19")]
[InlineData("https://www.twitter.com/ShowwcaseHQ/status/1556259601829576707?t=S6GuFx37mAXOLI2wdusfXg&s=19",
"https://www.vxtwitter.com/ShowwcaseHQ/status/1556259601829576707?t=S6GuFx37mAXOLI2wdusfXg&s=19")]
public void CanReplaceSocialLink(string url, string? replacedUrl) {
Uri resUrl = SocialLinkEmbedFixer.Fix(new Uri(url));
resUrl.OriginalString.Should().Be(replacedUrl);
}

[Theory]
[InlineData("LUCU ga si? https://www.instagram.com/reel/C0XXKVnpRUI/",
new[] { "https://www.instagram.com/reel/C0XXKVnpRUI/" })]
[InlineData("LUCU ga si? https://instagram.com/reel/C0XXKVnpRUI/",
new[] { "https://instagram.com/reel/C0XXKVnpRUI/" })]
[InlineData(
"WKWKWK alisnya Kevin https://twitter.com/ShowwcaseHQ/status/1556259601829576707?t=S6GuFx37mAXOLI2wdusfXg&s=19 😂",
new[] { "https://twitter.com/ShowwcaseHQ/status/1556259601829576707" })]
[InlineData(
"WKWKWK alisnya Kevin https://x.com/ShowwcaseHQ/status/1556259601829576707?t=S6GuFx37mAXOLI2wdusfXg&s=19 😂",
new[] { "https://x.com/ShowwcaseHQ/status/1556259601829576707" })]
[InlineData("https://www.x.com/ShowwcaseHQ/status/1556259601829576707",
new[] { "https://www.x.com/ShowwcaseHQ/status/1556259601829576707" })]
[InlineData("https://www.twitter.com/ShowwcaseHQ/status/1556259601829576707",
new[] { "https://www.twitter.com/ShowwcaseHQ/status/1556259601829576707" })]
[InlineData(
"lo udah liat ini belum? https://www.twitter.com/ShowwcaseHQ/status/1556259601829576707. Ohh iya, ini juga lucu: https://instagram.com/reel/C0XXKVnpRUI",
new[] { "https://www.twitter.com/ShowwcaseHQ/status/1556259601829576707", "https://instagram.com/reel/C0XXKVnpRUI" })]
[InlineData("Iyakah?", new string[] { })]
public void CanDetectSocialLink(string message, IEnumerable<string>? urls) {
List<string>? resUrls =
SocialLinkEmbedFixer.GetPossibleUrls(message)?.Select(u => u.OriginalString).ToList();
if (urls == null) {
resUrls.Should().BeNull();
} else {
resUrls.Should().BeEquivalentTo(urls);
}
}
}
}
Loading