Skip to content
This repository has been archived by the owner on Nov 8, 2017. It is now read-only.

Commit

Permalink
Added user flair template selection
Browse files Browse the repository at this point in the history
  • Loading branch information
ddevault committed Feb 3, 2013
1 parent ec0320e commit a4fbbc6
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
Binary file added HtmlAgilityPack.dll
Binary file not shown.
13 changes: 13 additions & 0 deletions RedditSharp/FlairTemplate.cs
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedditSharp
{
public class UserFlairTemplate // TODO: Consider using this class to set templates as well
{
public string Text { get; set; }
public string CssClass { get; set; }
}
}
4 changes: 4 additions & 0 deletions RedditSharp/RedditSharp.csproj
Expand Up @@ -30,6 +30,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="HtmlAgilityPack">
<HintPath>..\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>..\Newtonsoft.Json.dll</HintPath>
</Reference>
Expand All @@ -45,6 +48,7 @@
<ItemGroup>
<Compile Include="AuthenticatedUser.cs" />
<Compile Include="Comment.cs" />
<Compile Include="FlairTemplate.cs" />
<Compile Include="MultipartFormBuilder.cs" />
<Compile Include="FlairType.cs" />
<Compile Include="Post.cs" />
Expand Down
30 changes: 30 additions & 0 deletions RedditSharp/Subreddit.cs
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Security.Authentication;
using System.Text;
using HtmlAgilityPack;
using Newtonsoft.Json.Linq;

namespace RedditSharp
Expand All @@ -20,6 +21,7 @@ public class Subreddit : Thing
private const string SetUserFlairUrl = "http://www.reddit.com/api/flair";
private const string StylesheetUrl = "http://www.reddit.com/r/{0}/about/stylesheet.json";
private const string UploadImageUrl = "http://www.reddit.com/api/upload_sr_img";
private const string FlairSelectorUrl = "http://www.reddit.com/api/flairselector";

private Reddit Reddit { get; set; }

Expand Down Expand Up @@ -214,6 +216,34 @@ public void SetUserFlair(string user, string cssClass, string text)
var data = Reddit.GetResponseString(response.GetResponseStream());
}

public UserFlairTemplate[] GetUserFlairTemplates() // Hacky, there isn't a proper endpoint for this
{
var request = Reddit.CreatePost(FlairSelectorUrl);
var stream = request.GetRequestStream();
Reddit.WritePostBody(stream, new
{
name = Reddit.User.Name,
r = Name,
uh = Reddit.User.Modhash
});
stream.Close();
var response = request.GetResponse();
var data = Reddit.GetResponseString(response.GetResponseStream());
var document = new HtmlDocument();
document.LoadHtml(data);
var templateNodes = document.DocumentNode.Descendants("li");
var list = new List<UserFlairTemplate>();
foreach (var node in templateNodes)
{
list.Add(new UserFlairTemplate
{
CssClass = node.Descendants("span").First().Attributes["class"].Value.Split(' ')[1],
Text = node.Descendants("span").First().InnerText
});
}
return list.ToArray();
}

public void UploadHeaderImage(string name, ImageType imageType, byte[] file)
{
var request = Reddit.CreatePost(UploadImageUrl);
Expand Down
4 changes: 2 additions & 2 deletions TestRedditSharp/Program.cs
Expand Up @@ -32,8 +32,8 @@ static void Main(string[] args)
Console.WriteLine("Incorrect login.");
}
}
var subreddit = reddit.GetSubreddit("/r/funny");
var style = subreddit.GetStylesheet();
var subreddit = reddit.GetSubreddit("/r/vocaloid");
subreddit.GetUserFlairTemplates();
}

public static string ReadPassword()
Expand Down

0 comments on commit a4fbbc6

Please sign in to comment.