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

[TwitterExperimental] Add token parameter, UA, referrer #110

Merged
merged 4 commits into from
Aug 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TwitterMetadataTest
[InlineData(
463440424141459456L,
"US Department of the Interior",
"Sunsets don't get much better than this one over @GrandTetonNPS. #nature #sunset pic.twitter.com/YuKy2rcjyU"
"Sunsets don't get much better than this one over @GrandTetonNPS. #nature #sunset http://t.co/YuKy2rcjyU"
)]
public void FetchAsyncTest(long tweetId, string expectedDisplayName, string expectedDescription)
{
Expand Down
8 changes: 4 additions & 4 deletions MoEmbed.Core.Tests/Models/Metadata/TwitterMetadataTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public void FetchAsyncTest(long tweetId, string expectedDisplayName, string expe

[Theory]
[InlineData(
463440424141459456L,
"https://twitter.com/Interior/status/463440424141459456/photo/1",
"https://pbs.twimg.com/media/Bm54nBCCYAACwBi.jpg"
1608046425149177856L,
"https://twitter.com/supermomonga/status/1608046425149177856/photo/1",
"https://pbs.twimg.com/media/FlDtq9fakAIHeKj.jpg"
)]
public void SingleImageTest(long tweetId, string expectedLocation, string expectedRawUrl)
{
var uri = $"https://twitter.com/Interior/status/{tweetId}";
var uri = $"https://twitter.com/supermomonga/status/{tweetId}";
var target = new TwitterExperimentalMetadataProvider().GetMetadata(
new ConsumerRequest(new Uri(uri)));

Expand Down
48 changes: 30 additions & 18 deletions MoEmbed.Core/Models/Metadata/TwitterExperimentalMetadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,11 @@ public override async Task<EmbedData> FetchAsync(RequestContext context)
{
if (string.IsNullOrEmpty(StatusId)) return null;

var req = new HttpRequestMessage(HttpMethod.Get, $"https://cdn.syndication.twimg.com/tweet-result?id={StatusId}");
var req = new HttpRequestMessage(HttpMethod.Get, $"https://cdn.syndication.twimg.com/tweet-result?id={StatusId}&lang=ja&token=elonmusk");
req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
req.Headers.UserAgent.Clear();
req.Headers.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36");
req.Headers.Referrer = new("https://platform.twitter.com/");
var res = await context.Service.HttpClient.SendAsync(req).ConfigureAwait(false);

if (res.StatusCode == System.Net.HttpStatusCode.NotFound)
Expand All @@ -60,28 +63,37 @@ public override async Task<EmbedData> FetchAsync(RequestContext context)

var tweet = JsonSerializer.Deserialize<Tweet>(res.Content.ReadAsStream(), new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping });

return new EmbedData()
return tweet.Typename switch
{
Url = Url,
MetadataImage = new Media
"TweetTombstone" => new EmbedData()
{
Thumbnail = new ImageInfo
Url = Url,
Title = "Twitter",
Description = tweet.Tombstone.Text.TextText,
},
_ => new EmbedData()
{
Url = Url,
MetadataImage = new Media
{
Url = tweet.User.ProfileImageUrlHttps,
Height = 48,
Width = 48,
Thumbnail = new ImageInfo
{
Url = tweet.User.ProfileImageUrlHttps,
Height = 48,
Width = 48,
},
RawUrl = tweet.User.ProfileImageUrlHttps,
Location = Url,
RestrictionPolicy = RestrictionPolicies.Safe
},
RawUrl = tweet.User.ProfileImageUrlHttps,
Location = Url,
RestrictionPolicy = RestrictionPolicies.Safe
},
Title = $"{tweet.User.Name} (@{tweet.User.ScreenName})",
Description = tweet.Text,
Medias = tweet.MediaDetails?.Select(ToMedia).ToList() ?? new(),
RestrictionPolicy = tweet.PossiblySensitive ? RestrictionPolicies.Restricted : RestrictionPolicies.Safe,
Title = $"{tweet.User.Name} (@{tweet.User.ScreenName})",
Description = tweet.Text,
Medias = tweet.MediaDetails?.Select(ToMedia).ToList() ?? new(),
RestrictionPolicy = tweet.PossiblySensitive ? RestrictionPolicies.Restricted : RestrictionPolicies.Safe,

ProviderName = "Twitter",
ProviderUrl = "https://twitter.com/",
ProviderName = "Twitter",
ProviderUrl = "https://twitter.com/",
},
};
}

Expand Down
16 changes: 16 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Entity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace MoEmbed.Models.TwitterExperimental
{
public class Entity
{
[JsonPropertyName("from_index")]
public long FromIndex { get; set; }

[JsonPropertyName("to_index")]
public long ToIndex { get; set; }

[JsonPropertyName("ref")]
public Ref Ref { get; set; }
}
}
17 changes: 17 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Ref.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Text.Json.Serialization;

namespace MoEmbed.Models.TwitterExperimental
{
public class Ref
{
[JsonPropertyName("__typename")]
public string Typename { get; set; }

[JsonPropertyName("url")]
public Uri Url { get; set; }

[JsonPropertyName("url_type")]
public string UrlType { get; set; }
}
}
16 changes: 16 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Text.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Text.Json.Serialization;

namespace MoEmbed.Models.TwitterExperimental
{
public class Text
{
[JsonPropertyName("text")]
public string TextText { get; set; }

[JsonPropertyName("entities")]
public Entity[] Entities { get; set; }

[JsonPropertyName("rtl")]
public bool Rtl { get; set; }
}
}
10 changes: 10 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Tombstone.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Text.Json.Serialization;

namespace MoEmbed.Models.TwitterExperimental
{
public class Tombstone
{
[JsonPropertyName("text")]
public Text Text { get; set; }
}
}
3 changes: 3 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Tweet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,8 @@ public class Tweet

[JsonPropertyName("isStaleEdit")]
public bool IsStaleEdit { get; set; }

[JsonPropertyName("tombstone")]
public Tombstone Tombstone { get; set; }
}
}
Loading