-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add oEmbed based Twitter metadata and provider
- Loading branch information
1 parent
da94374
commit d5d5507
Showing
12 changed files
with
204 additions
and
13 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
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
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,33 @@ | ||
using MoEmbed.Providers; | ||
|
||
using System; | ||
|
||
using Xunit; | ||
|
||
namespace MoEmbed.Models.Metadata | ||
{ | ||
public class TwitterMetadataTest | ||
{ | ||
[Theory] | ||
[InlineData( | ||
463440424141459456L, | ||
"US Department of the Interior", | ||
"Sunsets don't get much better than this one over @GrandTetonNPS. #nature #sunset pic.twitter.com/YuKy2rcjyU— US Department of the Interior (@Interior) May 5, 2014" | ||
)] | ||
public void FetchAsyncTest(long tweetId, string expectedDisplayName, string expectedDescription) | ||
{ | ||
var uri = $"https://twitter.com/Interior/status/{tweetId}"; | ||
var target = new TwitterMetadataProvider().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(expectedDescription, data.Description); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
MoEmbed.Core.Tests/Providers/TwitterMetadataProviderTest.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,45 @@ | ||
using MoEmbed.Models; | ||
using MoEmbed.Models.Metadata; | ||
|
||
using System; | ||
|
||
using Xunit; | ||
|
||
namespace MoEmbed.Providers | ||
{ | ||
public sealed class TwitterMetadataProviderTest | ||
{ | ||
#region CanHandle | ||
|
||
[Theory] | ||
[InlineData("https://twitter.com/Interior/status/463440424141459456")] | ||
public void CanHandleTest_True(string uri) | ||
=> Assert.True(new TwitterMetadataProvider().CanHandle(new ConsumerRequest(new Uri(uri)))); | ||
|
||
[Theory] | ||
[InlineData("https://rms.sexy/")] | ||
public void CanHandleTest_False(string uri) | ||
=> Assert.False(new TwitterMetadataProvider().CanHandle(new ConsumerRequest(new Uri(uri)))); | ||
|
||
#endregion CanHandle | ||
|
||
#region GetMetadata | ||
|
||
[Theory] | ||
[InlineData("https://twitter.com/Interior/status/463440424141459456")] | ||
public void GetMetadataTest_Success(string uri) | ||
{ | ||
var m = Assert.IsType<TwitterMetadata>( | ||
new TwitterMetadataProvider().GetMetadata(new ConsumerRequest(new Uri(uri)))); | ||
Assert.Equal(uri, m.Url); | ||
Assert.Null(m.Data); | ||
} | ||
|
||
[Theory] | ||
[InlineData("https://rms.sexy/")] | ||
public void GetMetadataTest_Null(string uri) | ||
=> Assert.Null(new TwitterMetadataProvider().GetMetadata(new ConsumerRequest(new Uri(uri)))); | ||
|
||
#endregion GetMetadata | ||
} | ||
} |
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
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
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,29 @@ | ||
using Portable.Xaml.Markup; | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace MoEmbed.Models.Metadata | ||
{ | ||
/// <summary> | ||
/// Represents the <see cref="Metadata"/> for the URL of the nicotweet.jp. | ||
/// </summary> | ||
[Serializable] | ||
[ContentProperty(nameof(Data))] | ||
public class TwitterMetadata : OEmbedProxyMetadata | ||
{ | ||
/// <inheritdoc/> | ||
protected internal override EmbedData CreateEmbedData(Dictionary<string, object> values) | ||
{ | ||
var data = base.CreateEmbedData(values); | ||
data.Title = data.AuthorName ?? "Twitter"; | ||
|
||
|
||
var parser = new AngleSharp.Html.Parser.HtmlParser(); | ||
using var doc = parser.ParseDocument(data.Html); | ||
data.Description = doc.QuerySelector("blockquote").TextContent; | ||
|
||
return data; | ||
} | ||
} | ||
} |
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
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
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,34 @@ | ||
using MoEmbed.Models; | ||
using MoEmbed.Models.Metadata; | ||
|
||
namespace MoEmbed.Providers | ||
{ | ||
/// <summary> | ||
/// Provides a twitter.com specifiec metadata for the specified consumer request. | ||
/// </summary> | ||
public sealed partial class TwitterMetadataProvider : IMetadataProvider | ||
{ | ||
|
||
bool IMetadataProvider.SupportsAnyHost | ||
=> false; | ||
|
||
bool IMetadataProvider.IsEnabled | ||
=> true; | ||
|
||
/// <inheritdoc/> | ||
public new Metadata GetMetadata(ConsumerRequest request) | ||
{ | ||
if (!CanHandle(request)) | ||
{ | ||
return null; | ||
} | ||
var m = new TwitterMetadata | ||
{ | ||
Url = request.Url.ToString(), | ||
OEmbedUrl = GetProviderUriFor(request).ToString() | ||
}; | ||
|
||
return m; | ||
} | ||
} | ||
} |
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
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