Skip to content

Commit 1b9b171

Browse files
committed
Merge remote-tracking branch 'refs/remotes/CrustyJew/master'
# Conflicts: # RedditSharp/RedditSharp.csproj # RedditSharp/Things/Thing.cs
2 parents 720878b + e2469b3 commit 1b9b171

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1693
-294
lines changed

README.md

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![redditsharp MyGet Build Status](https://www.myget.org/BuildSource/Badge/redditsharp?identifier=038d3763-6401-4c84-b579-f5134e1c8efd)](https://www.myget.org/) [![NuGet version](https://badge.fury.io/nu/redditsharp.svg)](https://badge.fury.io/nu/redditsharp)
2+
13
**This is a hard fork and IS maintained**.
24

35
Due to the project being abandoned and the previous owner's refusal to transfer the repository to someone else to maintain it, I've created this fork to continue on support for the project.
@@ -8,8 +10,10 @@ A partial implementation of the [Reddit](http://reddit.com) API. Includes suppor
810
LINQ-style paging of results.
911

1012
```csharp
11-
var reddit = new Reddit();
12-
var user = reddit.LogIn("username", "password");
13+
var webAgent = new BotWebAgent("BotUsername", "BotPass", "ClientID", "ClientSecret", "RedirectUri");
14+
//This will check if the access token is about to expire before each request and automatically request a new one for you
15+
//"false" means that it will NOT load the logged in user profile so reddit.User will be null
16+
var reddit = new Reddit(webAgent, false);
1317
var subreddit = reddit.GetSubreddit("/r/example");
1418
subreddit.Subscribe();
1519
foreach (var post in subreddit.New.Take(25))
@@ -28,7 +32,7 @@ foreach (var post in subreddit.New.Take(25))
2832

2933
```csharp
3034
var all = reddit.RSlashAll;
31-
foreach (var post in all) // BAD
35+
foreach (var post in all.New) // BAD
3236
{
3337
// ...
3438
}
@@ -38,12 +42,40 @@ This will cause you to page through everything that has ever been posted on Redd
3842

3943
```csharp
4044
var all = reddit.RSlashAll;
41-
foreach (var post in all.Take(25))
45+
foreach (var post in all.New.Take(25))
4246
{
4347
// ...
4448
}
4549
```
4650

51+
52+
**Using ListingStreams**
53+
54+
Use ListingStreams to infinitely yeild new Things posted to reddit
55+
56+
Example:
57+
58+
```csharp
59+
// get all new comments as they are posted.
60+
foreach (var comment in subreddit.CommentStream)
61+
{
62+
Console.WriteLine(DateTime.Now + " New Comment posted to /r/example: " + comment.ShortLink);
63+
}
64+
```
65+
66+
you can call .GetListingStream() on any Listing<Thing>
67+
68+
```csharp
69+
// get new modmail
70+
var newModmail = user.ModMail.GetListingStream();
71+
foreach (var message in newModmail)
72+
{
73+
if (message.FirstMessageName == "")
74+
message.Reply("Thanks for the message - we will get back to you soon.");
75+
}
76+
77+
```
78+
4779
## Development
4880

4981
RedditSharp is developed with the following workflow:

Rebracer.xml

Lines changed: 249 additions & 0 deletions
Large diffs are not rendered by default.

RedditSharp.sln

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11

2-
Microsoft Visual Studio Solution File, Format Version 11.00
3-
# Visual Studio 2010
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
46
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedditSharp", "RedditSharp\RedditSharp.csproj", "{A368CB75-75F0-4489-904D-B5CEBB0FE624}"
57
EndProject
68
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestRedditSharp", "TestRedditSharp\TestRedditSharp.csproj", "{0101B252-0CCE-4572-8882-D5851FE41E4F}"
79
EndProject
10+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTesting", "UnitTesting\UnitTesting.csproj", "{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}"
11+
ProjectSection(ProjectDependencies) = postProject
12+
{A368CB75-75F0-4489-904D-B5CEBB0FE624} = {A368CB75-75F0-4489-904D-B5CEBB0FE624}
13+
EndProjectSection
14+
EndProject
15+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{46584E90-4894-4EB7-A0B9-FDAF29B4AF8B}"
16+
ProjectSection(SolutionItems) = preProject
17+
Rebracer.xml = Rebracer.xml
18+
EndProjectSection
19+
EndProject
820
Global
921
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1022
Debug|Any CPU = Debug|Any CPU
@@ -15,17 +27,6 @@ Global
1527
Release|x86 = Release|x86
1628
EndGlobalSection
1729
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Any CPU.ActiveCfg = Debug|x86
19-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Any CPU.Build.0 = Debug|x86
20-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
21-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Mixed Platforms.Build.0 = Debug|x86
22-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|x86.ActiveCfg = Debug|x86
23-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|x86.Build.0 = Debug|x86
24-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|Any CPU.ActiveCfg = Release|x86
25-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|Mixed Platforms.ActiveCfg = Release|x86
26-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|Mixed Platforms.Build.0 = Release|x86
27-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|x86.ActiveCfg = Release|x86
28-
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|x86.Build.0 = Release|x86
2930
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
3031
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Any CPU.Build.0 = Debug|Any CPU
3132
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
@@ -38,11 +39,34 @@ Global
3839
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Mixed Platforms.Build.0 = Release|Any CPU
3940
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|x86.ActiveCfg = Release|Any CPU
4041
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|x86.Build.0 = Release|Any CPU
41-
EndGlobalSection
42-
GlobalSection(MonoDevelopProperties) = preSolution
43-
StartupItem = TestRedditSharp\TestRedditSharp.csproj
42+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Any CPU.ActiveCfg = Debug|x86
43+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Any CPU.Build.0 = Debug|x86
44+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Mixed Platforms.ActiveCfg = Debug|x86
45+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|Mixed Platforms.Build.0 = Debug|x86
46+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|x86.ActiveCfg = Debug|x86
47+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Debug|x86.Build.0 = Debug|x86
48+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|Any CPU.ActiveCfg = Release|x86
49+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|Mixed Platforms.ActiveCfg = Release|x86
50+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|Mixed Platforms.Build.0 = Release|x86
51+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|x86.ActiveCfg = Release|x86
52+
{0101B252-0CCE-4572-8882-D5851FE41E4F}.Release|x86.Build.0 = Release|x86
53+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
54+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Any CPU.Build.0 = Debug|Any CPU
55+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
56+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
57+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|x86.ActiveCfg = Debug|Any CPU
58+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Debug|x86.Build.0 = Debug|Any CPU
59+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Any CPU.ActiveCfg = Release|Any CPU
60+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Any CPU.Build.0 = Release|Any CPU
61+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
62+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|Mixed Platforms.Build.0 = Release|Any CPU
63+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|x86.ActiveCfg = Release|Any CPU
64+
{A4640FC9-F8A5-4B11-A9FE-8FEB626B2080}.Release|x86.Build.0 = Release|Any CPU
4465
EndGlobalSection
4566
GlobalSection(SolutionProperties) = preSolution
4667
HideSolutionNode = FALSE
4768
EndGlobalSection
69+
GlobalSection(MonoDevelopProperties) = preSolution
70+
StartupItem = TestRedditSharp\TestRedditSharp.csproj
71+
EndGlobalSection
4872
EndGlobal

RedditSharp/AuthProvider.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
using Newtonsoft.Json.Linq;
2+
using RedditSharp.Things;
13
using System;
24
using System.Net;
35
using System.Security.Authentication;
46
using System.Text;
5-
using Newtonsoft.Json.Linq;
6-
using RedditSharp.Things;
77

88
namespace RedditSharp
99
{
1010
public class AuthProvider
1111
{
12-
private const string AccessUrl = "https://ssl.reddit.com/api/v1/access_token";
12+
public const string AccessUrl = "https://ssl.reddit.com/api/v1/access_token";
1313
private const string OauthGetMeUrl = "https://oauth.reddit.com/api/v1/me";
1414
private const string RevokeUrl = "https://www.reddit.com/api/v1/revoke_token";
1515

@@ -82,7 +82,7 @@ public AuthProvider(string clientId, string clientSecret, string redirectUri,IWe
8282
/// <returns></returns>
8383
public string GetAuthUrl(string state, Scope scope, bool permanent = false)
8484
{
85-
return String.Format("https://ssl.reddit.com/api/v1/authorize?client_id={0}&response_type=code&state={1}&redirect_uri={2}&duration={3}&scope={4}", _clientId, state, _redirectUri, permanent ? "permanent" : "temporary", scope.ToString().Replace(" ",""));
85+
return string.Format("https://ssl.reddit.com/api/v1/authorize?client_id={0}&response_type=code&state={1}&redirect_uri={2}&duration={3}&scope={4}", _clientId, state, _redirectUri, permanent ? "permanent" : "temporary", scope.ToString().Replace(" ",""));
8686
}
8787

8888
/// <summary>
@@ -198,7 +198,7 @@ public void RevokeToken(string token, bool isRefresh)
198198
public AuthenticatedUser GetUser(string accessToken)
199199
{
200200
var request = _webAgent.CreateGet(OauthGetMeUrl);
201-
request.Headers["Authorization"] = String.Format("bearer {0}", accessToken);
201+
request.Headers["Authorization"] = string.Format("bearer {0}", accessToken);
202202
var response = (HttpWebResponse)request.GetResponse();
203203
var result = _webAgent.GetResponseString(response.GetResponseStream());
204204
var thingjson = "{\"kind\": \"t2\", \"data\": " + result + "}";

RedditSharp/BotWebAgent.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Net;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace RedditSharp
9+
{
10+
public class BotWebAgent : WebAgent
11+
{
12+
//private so it doesn't leak app secret to other code
13+
private AuthProvider TokenProvider;
14+
private string Username;
15+
private string Password;
16+
17+
public DateTime TokenValidTo { get; set; }
18+
19+
public BotWebAgent(string username, string password, string clientID, string clientSecret, string redirectURI)
20+
{
21+
Username = username;
22+
Password = password;
23+
EnableRateLimit = true;
24+
RateLimit = RateLimitMode.Burst;
25+
RootDomain = "oauth.reddit.com";
26+
TokenProvider = new AuthProvider(clientID, clientSecret, redirectURI, this);
27+
GetNewToken();
28+
}
29+
30+
public override HttpWebRequest CreateRequest(string url, string method)
31+
{
32+
//add 5 minutes for clock skew to ensure requests succeed
33+
if (url != AuthProvider.AccessUrl && DateTime.UtcNow.AddMinutes(5) > TokenValidTo)
34+
{
35+
GetNewToken();
36+
}
37+
return base.CreateRequest(url, method);
38+
}
39+
40+
protected override HttpWebRequest CreateRequest(Uri uri, string method)
41+
{
42+
//add 5 minutes for clock skew to ensure requests succeed
43+
if (uri.ToString() != AuthProvider.AccessUrl && DateTime.UtcNow.AddMinutes(5) > TokenValidTo)
44+
{
45+
GetNewToken();
46+
}
47+
return base.CreateRequest(uri, method);
48+
}
49+
50+
private void GetNewToken()
51+
{
52+
AccessToken = TokenProvider.GetOAuthToken(Username, Password);
53+
TokenValidTo = DateTime.UtcNow.AddHours(1);
54+
}
55+
}
56+
57+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)