Skip to content

Commit

Permalink
Merge pull request #106 from 0xBADDCAFE/main
Browse files Browse the repository at this point in the history
  • Loading branch information
supermomonga committed Jul 1, 2023
2 parents 64f76af + ded50cc commit 10414c4
Show file tree
Hide file tree
Showing 20 changed files with 527 additions and 0 deletions.
2 changes: 2 additions & 0 deletions MoEmbed.Core/MetadataServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public static void AddMetadataProviders(this IServiceCollection services)
services.Add(new ServiceDescriptor(typeof(IMetadataProvider), typeof(DroplrMetadataProvider), ServiceLifetime.Singleton));
services.Add(new ServiceDescriptor(typeof(IMetadataProvider), typeof(GyazoMetadataProvider), ServiceLifetime.Singleton));
services.Add(new ServiceDescriptor(typeof(IMetadataProvider), typeof(MastodonMetadataProvider), ServiceLifetime.Singleton));
services.Add(new ServiceDescriptor(typeof(IMetadataProvider), typeof(TwitterExperimentalMetadataProvider), ServiceLifetime.Singleton));

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

Expand Down
96 changes: 96 additions & 0 deletions MoEmbed.Core/Models/Metadata/TwitterExperimentalMetadata.cs
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 MoEmbed.Core/Models/TwitterExperimental/BackgroundColor.cs
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

Check warning on line 5 in MoEmbed.Core/Models/TwitterExperimental/BackgroundColor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BackgroundColor'
{
[JsonPropertyName("red")]
public int Red { get; set; }

Check warning on line 8 in MoEmbed.Core/Models/TwitterExperimental/BackgroundColor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BackgroundColor.Red'

[JsonPropertyName("green")]
public int Green { get; set; }

Check warning on line 11 in MoEmbed.Core/Models/TwitterExperimental/BackgroundColor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BackgroundColor.Green'

[JsonPropertyName("blue")]
public int Blue { get; set; }

Check warning on line 14 in MoEmbed.Core/Models/TwitterExperimental/BackgroundColor.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'BackgroundColor.Blue'
}
}
20 changes: 20 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/EditControl.cs
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

Check warning on line 6 in MoEmbed.Core/Models/TwitterExperimental/EditControl.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'EditControl'
{
[JsonPropertyName("edit_tweet_ids")]
public List<string> EditTweetIds { get; set; }

Check warning on line 9 in MoEmbed.Core/Models/TwitterExperimental/EditControl.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'EditControl.EditTweetIds'

[JsonPropertyName("editable_until_msecs")]
public string EditableUntilMsecs { get; set; }

Check warning on line 12 in MoEmbed.Core/Models/TwitterExperimental/EditControl.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'EditControl.EditableUntilMsecs'

[JsonPropertyName("is_edit_eligible")]
public bool IsEditEligible { get; set; }

Check warning on line 15 in MoEmbed.Core/Models/TwitterExperimental/EditControl.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'EditControl.IsEditEligible'

[JsonPropertyName("edits_remaining")]
public string EditsRemaining { get; set; }

Check warning on line 18 in MoEmbed.Core/Models/TwitterExperimental/EditControl.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'EditControl.EditsRemaining'
}
}
23 changes: 23 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Entities.cs
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

Check warning on line 6 in MoEmbed.Core/Models/TwitterExperimental/Entities.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member '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 MoEmbed.Core/Models/TwitterExperimental/ExtMediaAvailability.cs
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; }
}
}
14 changes: 14 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Hashtag.cs
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; }
}
}
16 changes: 16 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Large.cs
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; }
}
}
21 changes: 21 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Media.cs
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; }
}
}
35 changes: 35 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/MediaDetail.cs
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; }
}
}
17 changes: 17 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Medium.cs
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; }
}
}
14 changes: 14 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/OriginalInfo.cs
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; }
}
}
27 changes: 27 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Photo.cs
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; }
}
}
20 changes: 20 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Sizes.cs
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; }
}
}
17 changes: 17 additions & 0 deletions MoEmbed.Core/Models/TwitterExperimental/Small.cs
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; }
}
}
Loading

0 comments on commit 10414c4

Please sign in to comment.