Skip to content

Commit e03e6fa

Browse files
authored
Merge pull request #28 from nyanpasudo/master
Removed dependancy on HTMLAgilityPack
2 parents 1374742 + 74b3312 commit e03e6fa

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
1-
namespace RedditSharp
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Converters;
3+
4+
namespace RedditSharp
25
{
6+
public enum FlairPosition
7+
{
8+
right,
9+
left
10+
}
311
public class UserFlairTemplate // TODO: Consider using this class to set templates as well
412
{
13+
[JsonProperty("flair_text")]
514
public string Text { get; set; }
15+
16+
[JsonProperty("flair_css_class")]
617
public string CssClass { get; set; }
18+
19+
[JsonProperty("flair_template_id")]
20+
public string TemplateId { get; set; }
21+
22+
[JsonProperty("flair_text_editable")]
23+
public bool IsEditable { get; set; }
24+
25+
[JsonProperty("flair_position")]
26+
[JsonConverter(typeof(StringEnumConverter))]
27+
public FlairPosition FlairPosition { get; set; }
728
}
8-
}
29+
}

RedditSharp/RedditSharp.csproj

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
</PropertyGroup>
3636
<PropertyGroup />
3737
<ItemGroup>
38-
<Reference Include="HtmlAgilityPack">
39-
<HintPath>..\HtmlAgilityPack.dll</HintPath>
40-
</Reference>
4138
<Reference Include="Newtonsoft.Json">
4239
<HintPath>..\Newtonsoft.Json.dll</HintPath>
4340
</Reference>
@@ -52,6 +49,7 @@
5249
</ItemGroup>
5350
<ItemGroup>
5451
<Compile Include="BotWebAgent.cs" />
52+
<Compile Include="Flairs\FlairTemplate.cs" />
5553
<Compile Include="Moderator Actions\ModActionType.cs" />
5654
<Compile Include="Things\BannedUser.cs" />
5755
<Compile Include="Things\Contributor.cs" />
@@ -70,7 +68,6 @@
7068
<Compile Include="Things\Comment.cs" />
7169
<Compile Include="Things\CreatedThing.cs" />
7270
<Compile Include="Extensions\Extensions.cs" />
73-
<Compile Include="Flairs\FlairTemplate.cs" />
7471
<Compile Include="Interfaces\IWebAgent.cs" />
7572
<Compile Include="Data\LinkData.cs" />
7673
<Compile Include="Listing.cs" />

RedditSharp/Things/Subreddit.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using System.Security.Authentication;
55
using System.Threading.Tasks;
6-
using HtmlAgilityPack;
76
using Newtonsoft.Json;
87
using Newtonsoft.Json.Linq;
98
using RedditSharp.Extensions.DateTimeExtensions;
@@ -262,9 +261,9 @@ public SubredditSettings Settings
262261
}
263262
}
264263
/// <summary>
265-
/// Hacky way to obtain flair templates
264+
/// Get a list of the available user flair templates for the subreddit
266265
/// </summary>
267-
public UserFlairTemplate[] UserFlairTemplates // Hacky, there isn't a proper endpoint for this
266+
public UserFlairTemplate[] UserFlairTemplates
268267
{
269268
get
270269
{
@@ -279,19 +278,13 @@ public SubredditSettings Settings
279278
stream.Close();
280279
var response = request.GetResponse();
281280
var data = WebAgent.GetResponseString(response.GetResponseStream());
282-
var document = new HtmlDocument();
283-
document.LoadHtml(data);
284-
if (document.DocumentNode.Descendants("div").First().Attributes["error"] != null)
285-
throw new InvalidOperationException("This subreddit does not allow users to select flair.");
286-
var templateNodes = document.DocumentNode.Descendants("li");
281+
var json = JObject.Parse(data);
282+
var choices = json["choices"];
287283
var list = new List<UserFlairTemplate>();
288-
foreach (var node in templateNodes)
284+
foreach (var choice in choices)
289285
{
290-
list.Add(new UserFlairTemplate
291-
{
292-
CssClass = node.Descendants("span").First().Attributes["class"].Value.Split(' ')[1],
293-
Text = node.Descendants("span").First().InnerText
294-
});
286+
UserFlairTemplate template = JsonConvert.DeserializeObject<UserFlairTemplate>(choice.ToString());
287+
list.Add(template);
295288
}
296289
return list.ToArray();
297290
}

0 commit comments

Comments
 (0)