Skip to content

Commit

Permalink
Merge pull request #1 from ObsidianMinor/reactions-wip
Browse files Browse the repository at this point in the history
Finished Reaction gifs implementation
  • Loading branch information
ObsidianMinor committed Mar 28, 2017
2 parents d963ccb + aa9ba23 commit 34741ce
Show file tree
Hide file tree
Showing 18 changed files with 191 additions and 21 deletions.
11 changes: 11 additions & 0 deletions src/Gfycat.Net/API/GfycatApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,17 @@ internal async Task ShareGfyAsync(string gfyId, TwitterShareRequest shareParams,

#endregion

#region Reaction gifs

internal async Task<TrendingTagsFeed> GetReactionGifsPopulatedAsync(ReactionLanguage lang, string cursor, RequestOptions options)
{
string query = Utils.CreateQueryString(("locale", Utils._reactionLangToString[lang]), ("cursor", cursor));
RestResponse response = await SendAsync("GET", "reactions/populated" + query, options);
return response.ReadAsJson<TrendingTagsFeed>(Config);
}

#endregion

#region Trending feeds

internal async Task<TrendingFeed> GetTrendingFeedAsync(string tagName,string cursor, RequestOptions options)
Expand Down
2 changes: 1 addition & 1 deletion src/Gfycat.Net/CurrentUserGfyFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static CurrentUserGfyFeed Create(GfycatClient client, RequestOptions op
return new CurrentUserGfyFeed(client, options)
{
Content = feed.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection(),
Cursor = feed.Cursor
_cursor = feed.Cursor
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Gfycat.Net/CurrentUserSearchFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static CurrentUserSearchFeed Create(GfycatClient client, Model model, s
return new CurrentUserSearchFeed(client, searchText, options)
{
Content = model.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection(),
Cursor = model.Cursor
_cursor = model.Cursor
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gfycat.Net/CurrentUserTimelineFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static CurrentUserTimelineFeed Create(GfycatClient client, RequestOptio
return new CurrentUserTimelineFeed(client, defaultOptions)
{
Content = feed.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection(),
Cursor = feed.Cursor
_cursor = feed.Cursor
};
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Gfycat.Net/GfyFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ public abstract class GfyFeed : IFeed<Gfy>
{
internal readonly GfycatClient _client;
internal readonly RequestOptions _options;
string IFeed<Gfy>.Cursor => _cursor;
internal string _cursor { get; set; }

internal GfyFeed(GfycatClient client, RequestOptions defaultOptions)
{
Expand All @@ -14,9 +16,7 @@ internal GfyFeed(GfycatClient client, RequestOptions defaultOptions)
}

public IReadOnlyCollection<Gfy> Content { get; internal set; }

public string Cursor { get; internal set; }


public abstract IAsyncEnumerator<Gfy> GetEnumerator();
}
}
5 changes: 5 additions & 0 deletions src/Gfycat.Net/GfycatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,11 @@ public async Task<PopulatedTagFeed> GetTrendingTagsPopulatedAsync(RequestOptions

#endregion

public async Task<ReactionTagsFeed> GetReactionGfysAsync(ReactionLanguage language = ReactionLanguage.English, RequestOptions options = null)
{
return ReactionTagsFeed.Create(this, options, await ApiClient.GetReactionGifsPopulatedAsync(language, null, options), language);
}

/// <summary>
/// Searches the Gfycat website using the provided search text
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Gfycat.Net/GfycatClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class GfycatClientConfig
/// <summary>
/// Sets the default timeout for all requests.
/// </summary>
public int? DefaultTimeout { get; set; } = -1;
public int DefaultTimeout { get; set; } = -1;

public GfycatClientConfig(string clientId, string clientSecret)
{
Expand Down
17 changes: 14 additions & 3 deletions src/Gfycat.Net/GfycatException.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net;
Expand Down Expand Up @@ -34,7 +35,7 @@ public GfycatException(HttpStatusCode status) : base($"The server responded with
internal static Exception CreateFromResponse(Rest.RestResponse restResponse)
{
string result = restResponse.ReadAsString();
if (!string.IsNullOrWhiteSpace(result) && !result.StartsWith("<html>"))
if (!string.IsNullOrWhiteSpace(result) && !result.StartsWith("<"))
{
JToken jsonObject = JToken.Parse(result);
if (jsonObject.Type == JTokenType.Array)
Expand All @@ -57,7 +58,17 @@ internal static Exception CreateFromResponse(Rest.RestResponse restResponse)
{
JToken gfyException = jsonObject["errorMessage"];
if (gfyException.Type == JTokenType.String)
return new GfycatException(gfyException.Value<string>(), restResponse.Status);
{
try
{
JToken internalMessage = JToken.Parse(gfyException.Value<string>());
return new GfycatException(internalMessage.Value<string>("code"), internalMessage.Value<string>("description"), restResponse.Status);
}
catch(JsonReaderException)
{
return new GfycatException(gfyException.Value<string>(), restResponse.Status);
}
}
else
return new GfycatException(gfyException.Value<string>("code"), gfyException.Value<string>("description"), restResponse.Status);
}
Expand Down
6 changes: 6 additions & 0 deletions src/Gfycat.Net/IFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ namespace Gfycat
{
public interface IFeed<out T> : IAsyncEnumerable<T>
{
/// <summary>
/// Contains the current page of content for this feed
/// </summary>
IReadOnlyCollection<T> Content { get; }
/// <summary>
/// The cursor use to get the next feed
/// </summary>
string Cursor { get; }
}
}
10 changes: 5 additions & 5 deletions src/Gfycat.Net/PopulatedTagFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,23 @@ public class PopulatedTagFeed : IFeed<TaggedGfyFeed>
{
readonly RequestOptions _defaultOptions;
readonly GfycatClient _client;

string IFeed<TaggedGfyFeed>.Cursor => _cursor;
internal string _cursor { get; set; }

internal PopulatedTagFeed(GfycatClient client, RequestOptions defaultOptions)
{
_defaultOptions = defaultOptions;
_client = client;
}

public IReadOnlyCollection<TaggedGfyFeed> Content { get; private set; }

public string Cursor { get; private set; }


internal static PopulatedTagFeed Create(GfycatClient client, RequestOptions options, Model model)
{
return new PopulatedTagFeed(client, options)
{
Content = model.Tags.Select(t => TaggedGfyFeed.Create(client, t, options)).ToReadOnlyCollection(),
Cursor = model.Cursor
_cursor = model.Cursor
};
}

Expand Down
30 changes: 30 additions & 0 deletions src/Gfycat.Net/ReactionFeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Linq;

using Model = Gfycat.API.Models.Feed;

namespace Gfycat
{
public class ReactionFeed : GfyFeed
{
public string Tag { get; }

internal ReactionFeed(GfycatClient client, string searchText, RequestOptions options) : base(client, options)
{
Tag = searchText;
}

internal static ReactionFeed Create(GfycatClient client, Model model, string searchText, RequestOptions options)
{
return new ReactionFeed(client, searchText, options)
{
Content = model.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection()
};
}

public override IAsyncEnumerator<Gfy> GetEnumerator()
{
return new SiteSearchEnumerator(_client, Tag, _options, this);
}
}
}
25 changes: 25 additions & 0 deletions src/Gfycat.Net/ReactionLanguage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma warning disable CS1591
namespace Gfycat
{
public enum ReactionLanguage
{
English,
Russian,
Japanese,
SimplifiedChinese,
TraditionalChinese,
French,
German,
Spanish,
Korean,
Arabic,
Farsi,
Hebrew,
Italian,
Thai,
Turkish,
Vietnamese,
Portuguese,
HanChinese
}
}
38 changes: 38 additions & 0 deletions src/Gfycat.Net/ReactionTagsFeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Collections.Generic;
using System.Linq;
using Model = Gfycat.API.Models.TrendingTagsFeed;

namespace Gfycat
{
public class ReactionTagsFeed : IFeed<ReactionFeed>
{
readonly RequestOptions _defaultOptions;
readonly GfycatClient _client;
readonly ReactionLanguage _language;
string IFeed<ReactionFeed>.Cursor => _cursor;
internal string _cursor { get; set; }

internal ReactionTagsFeed(GfycatClient client, RequestOptions defaultOptions, ReactionLanguage language)
{
_defaultOptions = defaultOptions;
_client = client;
_language = language;
}

public IReadOnlyCollection<ReactionFeed> Content { get; private set; }

internal static ReactionTagsFeed Create(GfycatClient client, RequestOptions options, Model model, ReactionLanguage lang)
{
return new ReactionTagsFeed(client, options, lang)
{
Content = model.Tags.Select(t => ReactionFeed.Create(client, t, t.Tag, options)).ToReadOnlyCollection(),
_cursor = model.Cursor
};
}

public IAsyncEnumerator<ReactionFeed> GetEnumerator()
{
return new ReactionTagsFeedEnumerator(_client, this, _defaultOptions, _language);
}
}
}
22 changes: 22 additions & 0 deletions src/Gfycat.Net/ReactionTagsFeedEnumerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace Gfycat
{
internal class ReactionTagsFeedEnumerator : FeedEnumerator<ReactionFeed>
{
readonly ReactionLanguage _lang;

public ReactionTagsFeedEnumerator(GfycatClient client, IFeed<ReactionFeed> feed, RequestOptions defaultOptions, ReactionLanguage lang) : base(client, feed, defaultOptions)
{
_lang = lang;
}

protected async override Task<IFeed<ReactionFeed>> GetNext(string cursor, RequestOptions options = null)
{
return ReactionTagsFeed.Create(_client, options, await _client.ApiClient.GetReactionGifsPopulatedAsync(_lang, cursor, options), _lang);
}
}
}
8 changes: 4 additions & 4 deletions src/Gfycat.Net/SiteSearchFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ namespace Gfycat
{
internal class SiteSearchFeed : GfyFeed
{
readonly string _searchText;
protected readonly string _searchText;

internal SiteSearchFeed(GfycatClient client, string searchText, RequestOptions options) : base(client, options)
{
_searchText = searchText;
}

internal static CurrentUserSearchFeed Create(GfycatClient client, Model model, string searchText, RequestOptions options)
internal static SiteSearchFeed Create(GfycatClient client, Model model, string searchText, RequestOptions options)
{
return new CurrentUserSearchFeed(client, searchText, options)
return new SiteSearchFeed(client, searchText, options)
{
Content = model.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection(),
Cursor = model.Cursor
_cursor = model.Cursor
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gfycat.Net/TaggedGfyFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal static TaggedGfyFeed Create(GfycatClient client, Model trendingFeed, Re
{
Content = trendingFeed.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection(),
Tag = trendingFeed.Tag,
Cursor = trendingFeed.Cursor
_cursor = trendingFeed.Cursor
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/Gfycat.Net/UserGfyFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static UserGfyFeed Create(GfycatClient client, RequestOptions defaultOp
return new UserGfyFeed(client, defaultOptions, userId)
{
Content = feed.Gfycats.Select(g => Gfy.Create(client, g)).ToReadOnlyCollection(),
Cursor = feed.Cursor
_cursor = feed.Cursor
};
}

Expand Down
22 changes: 22 additions & 0 deletions src/Gfycat.Net/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,27 @@ internal static IAlbumInfo CreateAlbum(GfycatClient client, API.Models.AlbumInfo
else
return AlbumFolder.Create(client, albumModel, ownerId);
}

internal static readonly Dictionary<ReactionLanguage, string> _reactionLangToString = new Dictionary<ReactionLanguage, string>
{
{ ReactionLanguage.English, null },
{ ReactionLanguage.Russian, "ru-RU" },
{ ReactionLanguage.Japanese, "ja-JP" },
{ ReactionLanguage.SimplifiedChinese, "zh-CN" },
{ ReactionLanguage.TraditionalChinese, "zh-TW" },
{ ReactionLanguage.French, "fr-FR" },
{ ReactionLanguage.German, "de-DE" },
{ ReactionLanguage.Spanish, "es-LA" },
{ ReactionLanguage.Korean, "ko-KR" },
{ ReactionLanguage.Arabic, "ar-SA" },
{ ReactionLanguage.Farsi, "fa-IR" },
{ ReactionLanguage.Hebrew, "he-IL" },
{ ReactionLanguage.Italian, "it-IT" },
{ ReactionLanguage.Thai, "th-TH" },
{ ReactionLanguage.Turkish, "tr-TR" },
{ ReactionLanguage.Vietnamese, "vi-VN" },
{ ReactionLanguage.Portuguese, "pt-BR" },
{ ReactionLanguage.HanChinese, "zh-Ha" }
};
}
}

0 comments on commit 34741ce

Please sign in to comment.