Skip to content

Commit

Permalink
Merge pull request #78 from bgwastu/main
Browse files Browse the repository at this point in the history
feat: better social link embed (vxtwitter, ddinstagram)
  • Loading branch information
ronnygunawan authored Dec 4, 2023
2 parents a0c045e + c2c66a9 commit 6cede0c
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 58 deletions.
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

0 comments on commit 6cede0c

Please sign in to comment.