-
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.
Merge pull request #106 from 0xBADDCAFE/main
- Loading branch information
Showing
20 changed files
with
527 additions
and
0 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
96 changes: 96 additions & 0 deletions
96
MoEmbed.Core/Models/Metadata/TwitterExperimentalMetadata.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,96 @@ | ||
using System; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using MoEmbed.Models.TweetExperimental; | ||
using Portable.Xaml.Markup; | ||
|
||
namespace MoEmbed.Models.Metadata | ||
{ | ||
/// <summary> | ||
/// Represents the <see cref="Metadata" /> for the URL of the Twitter. | ||
/// </summary> | ||
[Serializable] | ||
[ContentProperty(nameof(Data))] | ||
public class TwitterExperimentalMetadata : Metadata | ||
{ | ||
/// <summary> | ||
/// Gets or sets the resolved data. | ||
/// </summary> | ||
[DefaultValue(null)] | ||
public EmbedData Data { get; set; } | ||
|
||
/// <summary> | ||
/// Tweet url | ||
/// </summary> | ||
[DefaultValue(null)] | ||
public string Url { get; set; } | ||
|
||
/// <summary> | ||
/// Twitter status id | ||
/// </summary> | ||
[DefaultValue(null)] | ||
public string StatusId { get; set; } | ||
|
||
/// <inheritdoc/> | ||
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}"); | ||
req.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); | ||
var res = await context.Service.HttpClient.SendAsync(req).ConfigureAwait(false); | ||
|
||
res.EnsureSuccessStatusCode(); | ||
|
||
var tweet = JsonSerializer.Deserialize<Tweet>(res.Content.ReadAsStream(), new JsonSerializerOptions { Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping }); | ||
|
||
return new EmbedData() | ||
{ | ||
Url = Url, | ||
MetadataImage = new Media | ||
{ | ||
Thumbnail = new ImageInfo | ||
{ | ||
Url = tweet.User.ProfileImageUrlHttps, | ||
Height = 48, | ||
Width = 48, | ||
}, | ||
RawUrl = tweet.User.ProfileImageUrlHttps, | ||
Location = Url, | ||
RestrictionPolicy = RestrictionPolicies.Safe | ||
}, | ||
Title = tweet.User.Name, | ||
Description = tweet.Text, | ||
Medias = tweet.MediaDetails.Select(ToMedia).ToList(), | ||
RestrictionPolicy = tweet.PossiblySensitive ? RestrictionPolicies.Restricted : RestrictionPolicies.Safe, | ||
|
||
ProviderName = "Twitter", | ||
ProviderUrl = "https://twitter.com/", | ||
}; | ||
} | ||
|
||
private static Media ToMedia(MediaDetail m) => new() | ||
{ | ||
Type = MediaTypeFromString(m.Type), | ||
Thumbnail = new() | ||
{ | ||
Url = $"{m.MediaUrlHttps}:thumb", | ||
Width = 150, | ||
Height = 150, | ||
}, | ||
RawUrl = m.MediaUrlHttps, | ||
Location = m.ExpandedUrl, | ||
}; | ||
|
||
private static MediaTypes MediaTypeFromString(string s) => s switch | ||
{ | ||
"photo" => MediaTypes.Image, | ||
"video" or "animated_gif" => MediaTypes.Video, | ||
_ => throw new NotImplementedException() | ||
}; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
MoEmbed.Core/Models/TwitterExperimental/BackgroundColor.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,16 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class BackgroundColor | ||
{ | ||
[JsonPropertyName("red")] | ||
public int Red { get; set; } | ||
|
||
[JsonPropertyName("green")] | ||
public int Green { get; set; } | ||
|
||
[JsonPropertyName("blue")] | ||
public int Blue { get; set; } | ||
} | ||
} |
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,20 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class EditControl | ||
{ | ||
[JsonPropertyName("edit_tweet_ids")] | ||
public List<string> EditTweetIds { get; set; } | ||
|
||
[JsonPropertyName("editable_until_msecs")] | ||
public string EditableUntilMsecs { get; set; } | ||
|
||
[JsonPropertyName("is_edit_eligible")] | ||
public bool IsEditEligible { get; set; } | ||
|
||
[JsonPropertyName("edits_remaining")] | ||
public string EditsRemaining { get; set; } | ||
} | ||
} |
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,23 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Entities | ||
{ | ||
[JsonPropertyName("hashtags")] | ||
public List<Hashtag> Hashtags { get; set; } | ||
|
||
[JsonPropertyName("urls")] | ||
public List<object> Urls { get; set; } | ||
|
||
[JsonPropertyName("user_mentions")] | ||
public List<UserMention> UserMentions { get; set; } | ||
|
||
[JsonPropertyName("symbols")] | ||
public List<object> Symbols { get; set; } | ||
|
||
[JsonPropertyName("media")] | ||
public List<Media> Media { get; set; } | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
MoEmbed.Core/Models/TwitterExperimental/ExtMediaAvailability.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,10 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class ExtMediaAvailability | ||
{ | ||
[JsonPropertyName("status")] | ||
public string Status { get; set; } | ||
} | ||
} |
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,14 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Hashtag | ||
{ | ||
[JsonPropertyName("indices")] | ||
public List<int> Indices { get; set; } | ||
|
||
[JsonPropertyName("text")] | ||
public string Text { get; set; } | ||
} | ||
} |
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,16 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Large | ||
{ | ||
[JsonPropertyName("h")] | ||
public int H { get; set; } | ||
|
||
[JsonPropertyName("resize")] | ||
public string Resize { get; set; } | ||
|
||
[JsonPropertyName("w")] | ||
public int W { get; set; } | ||
} | ||
} |
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,21 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
// Tweet myDeserializedClass = JsonConvert.DeserializeObject<Tweet>(myJsonResponse); | ||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Media | ||
{ | ||
[JsonPropertyName("display_url")] | ||
public string DisplayUrl { get; set; } | ||
|
||
[JsonPropertyName("expanded_url")] | ||
public string ExpandedUrl { get; set; } | ||
|
||
[JsonPropertyName("indices")] | ||
public List<int> Indices { get; set; } | ||
|
||
[JsonPropertyName("url")] | ||
public string Url { get; set; } | ||
} | ||
} |
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,35 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class MediaDetail | ||
{ | ||
[JsonPropertyName("display_url")] | ||
public string DisplayUrl { get; set; } | ||
|
||
[JsonPropertyName("expanded_url")] | ||
public string ExpandedUrl { get; set; } | ||
|
||
[JsonPropertyName("ext_media_availability")] | ||
public ExtMediaAvailability ExtMediaAvailability { get; set; } | ||
|
||
[JsonPropertyName("indices")] | ||
public List<int> Indices { get; set; } | ||
|
||
[JsonPropertyName("media_url_https")] | ||
public string MediaUrlHttps { get; set; } | ||
|
||
[JsonPropertyName("original_info")] | ||
public OriginalInfo OriginalInfo { get; set; } | ||
|
||
[JsonPropertyName("sizes")] | ||
public Sizes Sizes { get; set; } | ||
|
||
[JsonPropertyName("type")] | ||
public string Type { get; set; } | ||
|
||
[JsonPropertyName("url")] | ||
public string Url { get; set; } | ||
} | ||
} |
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,17 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
// Tweet myDeserializedClass = JsonConvert.DeserializeObject<Tweet>(myJsonResponse); | ||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Medium | ||
{ | ||
[JsonPropertyName("h")] | ||
public int H { get; set; } | ||
|
||
[JsonPropertyName("resize")] | ||
public string Resize { get; set; } | ||
|
||
[JsonPropertyName("w")] | ||
public int W { get; set; } | ||
} | ||
} |
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,14 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
// Tweet myDeserializedClass = JsonConvert.DeserializeObject<Tweet>(myJsonResponse); | ||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class OriginalInfo | ||
{ | ||
[JsonPropertyName("height")] | ||
public int Height { get; set; } | ||
|
||
[JsonPropertyName("width")] | ||
public int Width { get; set; } | ||
} | ||
} |
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,27 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json.Serialization; | ||
|
||
// Tweet myDeserializedClass = JsonConvert.DeserializeObject<Tweet>(myJsonResponse); | ||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Photo | ||
{ | ||
[JsonPropertyName("backgroundColor")] | ||
public BackgroundColor BackgroundColor { get; set; } | ||
|
||
[JsonPropertyName("cropCandidates")] | ||
public List<object> CropCandidates { get; set; } | ||
|
||
[JsonPropertyName("expandedUrl")] | ||
public string ExpandedUrl { get; set; } | ||
|
||
[JsonPropertyName("url")] | ||
public string Url { get; set; } | ||
|
||
[JsonPropertyName("width")] | ||
public int Width { get; set; } | ||
|
||
[JsonPropertyName("height")] | ||
public int Height { get; set; } | ||
} | ||
} |
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,20 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
// Tweet myDeserializedClass = JsonConvert.DeserializeObject<Tweet>(myJsonResponse); | ||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Sizes | ||
{ | ||
[JsonPropertyName("large")] | ||
public Large Large { get; set; } | ||
|
||
[JsonPropertyName("medium")] | ||
public Media Medium { get; set; } | ||
|
||
[JsonPropertyName("small")] | ||
public Small Small { get; set; } | ||
|
||
[JsonPropertyName("thumb")] | ||
public Thumb Thumb { get; set; } | ||
} | ||
} |
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,17 @@ | ||
using System.Text.Json.Serialization; | ||
|
||
// Tweet myDeserializedClass = JsonConvert.DeserializeObject<Tweet>(myJsonResponse); | ||
namespace MoEmbed.Models.TweetExperimental | ||
{ | ||
public class Small | ||
{ | ||
[JsonPropertyName("h")] | ||
public int H { get; set; } | ||
|
||
[JsonPropertyName("resize")] | ||
public string Resize { get; set; } | ||
|
||
[JsonPropertyName("w")] | ||
public int W { get; set; } | ||
} | ||
} |
Oops, something went wrong.