Skip to content

Commit

Permalink
Merge pull request #111 from 0xBADDCAFE/main
Browse files Browse the repository at this point in the history
Support x.com with TwitterExperimentalMetadataProvider
  • Loading branch information
supermomonga authored Apr 4, 2024
2 parents 56cec01 + 68a6a10 commit f2a4013
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
23 changes: 23 additions & 0 deletions MoEmbed.Core.Tests/Models/Metadata/TwitterMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,28 @@ public void SingleImageTest(long tweetId, string expectedLocation, string expect
Assert.Equal(expectedLocation, data.Medias[0].Location);
Assert.Equal(expectedRawUrl, data.Medias[0].RawUrl);
}
[Theory]
[InlineData(
463440424141459456L,
"US Department of the Interior (@Interior)",
"https://pbs.twimg.com/profile_images/432081479/DOI_LOGO_normal.jpg",
"Sunsets don't get much better than this one over @GrandTetonNPS. #nature #sunset http://t.co/YuKy2rcjyU"
)]
public void XComFetchAsyncTest(long tweetId, string expectedDisplayName, string expectedProfileImageUrl, string expectedDescription)
{
var uri = $"https://x.com/Interior/status/{tweetId}";
var target = new TwitterExperimentalMetadataProvider().GetMetadata(
new ConsumerRequest(new Uri(uri)));

var data = target.FetchAsync(
new RequestContext(
new MetadataService(),
new ConsumerRequest(new Uri(uri))))
.GetAwaiter().GetResult();

Assert.Equal(expectedDisplayName, data.Title);
Assert.Equal(expectedProfileImageUrl, data.MetadataImage.Thumbnail.Url);
Assert.Equal(expectedDescription, data.Description);
}
}
}
10 changes: 8 additions & 2 deletions MoEmbed.Core/MetadataServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Configuration;
using System.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

using MoEmbed.Providers;
Expand All @@ -11,6 +12,11 @@ namespace MoEmbed
/// </summary>
public static class MetadataServiceCollectionExtensions
{
static System.Type[] IgnoredMetadataProvierTypes = {
typeof(TwitterMetadataProvider),
typeof(XMetadataProvider),
};

/// <summary>
/// Adds <see cref="ServiceDescriptor" /> s of the <see cref="IMetadataProvider" /> in the
/// <see cref="N:MoEmbed.Providers" /> namespace to the specified <see
Expand All @@ -34,7 +40,7 @@ public static void AddMetadataProviders(this IServiceCollection services)

foreach (var t in OEmbedProxyMetadataProvider.CreateKnownHandlerTypes())
{
if (t == typeof(TwitterMetadataProvider)) continue;
if (IgnoredMetadataProvierTypes.Contains(t)) continue;
services.Add(new ServiceDescriptor(typeof(IMetadataProvider), t, ServiceLifetime.Singleton));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public sealed class TwitterExperimentalMetadataProvider : IMetadataProvider
{
private const string GROUP_STATUS_ID = "statusId";
// private static readonly Regex uriPattern = new(@$"^(https://twitter\.com/|https://twitter\.com/.*/status/(?<{GROUP_STATUS_ID}>\d+)|https://.*\.twitter\.com/.*/status/(?<{GROUP_STATUS_ID}>\d+))");
private static readonly Regex uriPattern = new(@$"^(https://twitter\.com/.*/status/(?<{GROUP_STATUS_ID}>\d+)|https://.*\.twitter\.com/.*/status/(?<{GROUP_STATUS_ID}>\d+))");
private static readonly Regex uriPattern = new(@$"^(https://(?:twitter|x)\.com/.*/status/(?<{GROUP_STATUS_ID}>\d+)|https://.*\.(?:twitter|x)\.com/.*/status/(?<{GROUP_STATUS_ID}>\d+))");

bool IMetadataProvider.SupportsAnyHost
=> false;
Expand Down Expand Up @@ -43,6 +43,7 @@ public Metadata GetMetadata(ConsumerRequest request)
public IEnumerable<string> GetSupportedHostNames()
{
yield return "twitter.com";
yield return "x.com";
}
}
}

0 comments on commit f2a4013

Please sign in to comment.