-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from bgwastu/main
feat: better social link embed (vxtwitter, ddinstagram)
- Loading branch information
Showing
3 changed files
with
213 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
BotNet.Tests/Services/SocialLink/SocialLinkEmbedFixerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.