Skip to content

Commit

Permalink
Fixed serialization for TweetMediaType.AnimatedGif. Added Url to Twee…
Browse files Browse the repository at this point in the history
…tMedia because TweetMediaType.Photo has Url and not PreviewImageUrl.
  • Loading branch information
JoeMayo committed Aug 12, 2021
1 parent 3e4b0b4 commit 4a47339
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ static async Task DoPagedSearchAsync(TwitterContext twitterCtx)
static async Task DoRecentSearchAsync(TwitterContext twitterCtx)
{
string searchTerm = "\"LINQ to Twitter\" OR Linq2Twitter OR LinqToTwitter OR JoeMayo";
searchTerm = "Twitter";

// default is id and text and this also brings in created_at and geo
string tweetFields =
Expand All @@ -177,6 +178,7 @@ static async Task DoRecentSearchAsync(TwitterContext twitterCtx)
(from search in twitterCtx.TwitterSearch
where search.Type == SearchType.RecentSearch &&
search.Query == searchTerm &&
search.MaxResults == 100 &&
search.TweetFields == TweetField.AllFieldsExceptPermissioned &&
search.Expansions == ExpansionField.AllTweetFields &&
search.MediaFields == MediaField.AllFieldsExceptPermissioned &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,8 @@ TwitterSearch JsonDeserialize(string responseJson)
{
Converters =
{
new JsonStringEnumConverter()
new JsonStringEnumConverter(),
new TweetMediaTypeConverter()
}
};
TwitterSearch? search = JsonSerializer.Deserialize<TwitterSearch>(responseJson, options);
Expand Down
9 changes: 8 additions & 1 deletion src/LinqToTwitter6/LinqToTwitter/Tweet/TweetMedia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public record TweetMedia
public object? OrganicMetrics { get; init; }

/// <summary>
/// URL to video preview image
/// URL to animated GIF and video preview image
/// </summary>
[JsonPropertyName("preview_image_url")]
public string? PreviewImageUrl { get; init; }
Expand All @@ -50,9 +50,16 @@ public record TweetMedia
/// <summary>
/// Type of media - e.g. gif, photo, or video
/// </summary>
[JsonConverter(typeof(TweetMediaTypeConverter))]
[JsonPropertyName("type")]
public TweetMediaType Type { get; init; }

/// <summary>
/// URL to photo preview image
/// </summary>
[JsonPropertyName("url")]
public string? Url { get; init; }

/// <summary>
/// Width in pixels
/// </summary>
Expand Down
32 changes: 32 additions & 0 deletions src/LinqToTwitter6/LinqToTwitter/Tweet/TweetMediaTypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace LinqToTwitter
{
public class TweetMediaTypeConverter : JsonConverter<TweetMediaType>
{
public override TweetMediaType Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return reader.GetString() switch
{
"animated_gif" => TweetMediaType.AnimatedGif,
"photo" => TweetMediaType.Photo,
"video" => TweetMediaType.Video,
_ => TweetMediaType.None
};
}

public override void Write(Utf8JsonWriter writer, TweetMediaType value, JsonSerializerOptions options)
{
writer.WriteStringValue(
value switch
{
TweetMediaType.AnimatedGif => "animated_gif",
TweetMediaType.Photo => "photo",
TweetMediaType.Video => "video",
_ => ""
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ TweetQuery JsonDeserialize(string responseJson)
{
Converters =
{
new JsonStringEnumConverter()
new JsonStringEnumConverter(),
new TweetMediaTypeConverter()
}
};
TweetQuery? tweet = JsonSerializer.Deserialize<TweetQuery>(responseJson, options);
Expand Down

0 comments on commit 4a47339

Please sign in to comment.