Skip to content

Commit 1a5f033

Browse files
author
Zac Brown
committed
Merge remote-tracking branch 'SirCmpwn/master'
2 parents 3b34d38 + a5e1e6a commit 1a5f033

File tree

11 files changed

+633
-581
lines changed

11 files changed

+633
-581
lines changed

.gitattributes

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
###############################################################################
22
# Set default behavior to automatically normalize line endings.
33
###############################################################################
4-
#* text=auto
4+
* text=auto
55

66
###############################################################################
77
# Set default behavior for command prompt diff.
@@ -10,8 +10,9 @@
1010
# default for csharp files.
1111
# Note: This is only used by command line
1212
###############################################################################
13-
#*.cs diff=csharp
14-
13+
*.cs text diff=csharp
14+
*.sln text eol=crlf
15+
*.csproj text eol=crlf
1516
###############################################################################
1617
# Set the merge driver for project and solution files
1718
#

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# RedditSharp
22

3-
This library is **unmaintained**. Someone should fork it.
4-
53
A partial implementation of the [Reddit](http://reddit.com) API. Includes support for many API endpoints, as well as
64
LINQ-style paging of results.
75

RedditSharp.sln

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
1-
2-
Microsoft Visual Studio Solution File, Format Version 11.00
3-
# Visual Studio 2010
4-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedditSharp", "RedditSharp\RedditSharp.csproj", "{A368CB75-75F0-4489-904D-B5CEBB0FE624}"
5-
EndProject
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestRedditSharp", "TestRedditSharp\TestRedditSharp.csproj", "{0101B252-0CCE-4572-8882-D5851FE41E4F}"
7-
EndProject
8-
Global
9-
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10-
Debug|Any CPU = Debug|Any CPU
11-
Debug|Mixed Platforms = Debug|Mixed Platforms
12-
Debug|x86 = Debug|x86
13-
Release|Any CPU = Release|Any CPU
14-
Release|Mixed Platforms = Release|Mixed Platforms
15-
Release|x86 = Release|x86
16-
EndGlobalSection
17-
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
29-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Any CPU.Build.0 = Debug|Any CPU
31-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
32-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
33-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|x86.ActiveCfg = Debug|Any CPU
34-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|x86.Build.0 = Debug|Any CPU
35-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Any CPU.ActiveCfg = Release|Any CPU
36-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Any CPU.Build.0 = Release|Any CPU
37-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
38-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Mixed Platforms.Build.0 = Release|Any CPU
39-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|x86.ActiveCfg = Release|Any CPU
40-
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|x86.Build.0 = Release|Any CPU
41-
EndGlobalSection
42-
GlobalSection(MonoDevelopProperties) = preSolution
43-
StartupItem = TestRedditSharp\TestRedditSharp.csproj
44-
EndGlobalSection
45-
GlobalSection(SolutionProperties) = preSolution
46-
HideSolutionNode = FALSE
47-
EndGlobalSection
48-
EndGlobal
1+
2+
Microsoft Visual Studio Solution File, Format Version 11.00
3+
# Visual Studio 2010
4+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RedditSharp", "RedditSharp\RedditSharp.csproj", "{A368CB75-75F0-4489-904D-B5CEBB0FE624}"
5+
EndProject
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestRedditSharp", "TestRedditSharp\TestRedditSharp.csproj", "{0101B252-0CCE-4572-8882-D5851FE41E4F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Debug|Mixed Platforms = Debug|Mixed Platforms
12+
Debug|x86 = Debug|x86
13+
Release|Any CPU = Release|Any CPU
14+
Release|Mixed Platforms = Release|Mixed Platforms
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
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
29+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
30+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Any CPU.Build.0 = Debug|Any CPU
31+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
32+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
33+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|x86.ActiveCfg = Debug|Any CPU
34+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Debug|x86.Build.0 = Debug|Any CPU
35+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Any CPU.ActiveCfg = Release|Any CPU
36+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Any CPU.Build.0 = Release|Any CPU
37+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
38+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|Mixed Platforms.Build.0 = Release|Any CPU
39+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|x86.ActiveCfg = Release|Any CPU
40+
{A368CB75-75F0-4489-904D-B5CEBB0FE624}.Release|x86.Build.0 = Release|Any CPU
41+
EndGlobalSection
42+
GlobalSection(MonoDevelopProperties) = preSolution
43+
StartupItem = TestRedditSharp\TestRedditSharp.csproj
44+
EndGlobalSection
45+
GlobalSection(SolutionProperties) = preSolution
46+
HideSolutionNode = FALSE
47+
EndGlobalSection
48+
EndGlobal

RedditSharp/Reddit.cs

Lines changed: 71 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class Reddit
3636
private const string PopularSubredditsUrl = "/subreddits/popular.json";
3737
private const string GoldSubredditsUrl = "/subreddits/gold.json";
3838
private const string DefaultSubredditsUrl = "/subreddits/default.json";
39-
private const string SearchSubredditsUrl = "/subreddits/search.json?q={0}";
39+
private const string SearchSubredditsUrl = "/subreddits/search.json?q={0}";
4040

4141

4242
#endregion
@@ -129,35 +129,35 @@ public Reddit(string accessToken)
129129
WebAgent.AccessToken = accessToken;
130130
InitOrUpdateUser();
131131
}
132-
/// <summary>
133-
/// Creates a Reddit instance with the given WebAgent implementation
134-
/// </summary>
132+
/// <summary>
133+
/// Creates a Reddit instance with the given WebAgent implementation
134+
/// </summary>
135135
/// <param name="agent">Implementation of IWebAgent interface. Used to generate requests.</param>
136-
public Reddit(IWebAgent agent)
137-
{
138-
WebAgent = agent;
139-
JsonSerializerSettings = new JsonSerializerSettings
140-
{
141-
CheckAdditionalContent = false,
142-
DefaultValueHandling = DefaultValueHandling.Ignore
136+
public Reddit(IWebAgent agent)
137+
{
138+
WebAgent = agent;
139+
JsonSerializerSettings = new JsonSerializerSettings
140+
{
141+
CheckAdditionalContent = false,
142+
DefaultValueHandling = DefaultValueHandling.Ignore
143143
};
144-
CaptchaSolver = new ConsoleCaptchaSolver();
144+
CaptchaSolver = new ConsoleCaptchaSolver();
145145
}
146-
/// <summary>
147-
/// Creates a Reddit instance with the given WebAgent implementation
148-
/// </summary>
146+
/// <summary>
147+
/// Creates a Reddit instance with the given WebAgent implementation
148+
/// </summary>
149149
/// <param name="agent">Implementation of IWebAgent interface. Used to generate requests.</param>
150150
/// <param name="initUser">Whether to run InitOrUpdateUser, requires <paramref name="agent"/> to have credentials first.</param>
151-
public Reddit(IWebAgent agent, bool initUser)
152-
{
153-
WebAgent = agent;
154-
JsonSerializerSettings = new JsonSerializerSettings
155-
{
156-
CheckAdditionalContent = false,
157-
DefaultValueHandling = DefaultValueHandling.Ignore
151+
public Reddit(IWebAgent agent, bool initUser)
152+
{
153+
WebAgent = agent;
154+
JsonSerializerSettings = new JsonSerializerSettings
155+
{
156+
CheckAdditionalContent = false,
157+
DefaultValueHandling = DefaultValueHandling.Ignore
158158
};
159-
CaptchaSolver = new ConsoleCaptchaSolver();
160-
if(initUser) InitOrUpdateUser();
159+
CaptchaSolver = new ConsoleCaptchaSolver();
160+
if(initUser) InitOrUpdateUser();
161161
}
162162

163163
/// <summary>
@@ -295,17 +295,43 @@ public Post GetPost(Uri uri)
295295
return new Post().Init(this, GetToken(uri), WebAgent);
296296
}
297297

298-
public void ComposePrivateMessage(string subject, string body, string to, string captchaId = "", string captchaAnswer = "")
298+
/// <summary>
299+
///
300+
/// </summary>
301+
/// <param name="subject"></param>
302+
/// <param name="body"></param>
303+
/// <param name="to"></param>
304+
/// <param name="fromSubReddit">The subreddit to send the message as (optional).</param>
305+
/// <param name="captchaId"></param>
306+
/// <param name="captchaAnswer"></param>
307+
/// <remarks>If <paramref name="fromSubReddit"/> is passed in then the message is sent from the subreddit. the sender must be a mod of the specified subreddit.</remarks>
308+
/// <exception cref="AuthenticationException">Thrown when a subreddit is passed in and the user is not a mod of that sub.</exception>
309+
public void ComposePrivateMessage(string subject, string body, string to, string fromSubReddit = "", string captchaId = "", string captchaAnswer = "")
299310
{
300311
if (User == null)
301312
throw new Exception("User can not be null.");
313+
314+
if (!String.IsNullOrWhiteSpace(fromSubReddit))
315+
{
316+
var subReddit = this.GetSubreddit(fromSubReddit);
317+
var modNameList = subReddit.Moderators.Select(b => b.Name).ToList();
318+
319+
if (!modNameList.Contains(User.Name))
320+
throw new AuthenticationException(
321+
String.Format(
322+
@"User {0} is not a moderator of subreddit {1}.",
323+
User.Name,
324+
subReddit.Name));
325+
}
326+
302327
var request = WebAgent.CreatePost(ComposeMessageUrl);
303328
WebAgent.WritePostBody(request.GetRequestStream(), new
304329
{
305330
api_type = "json",
306331
subject,
307332
text = body,
308333
to,
334+
from_sr = fromSubReddit,
309335
uh = User.Modhash,
310336
iden = captchaId,
311337
captcha = captchaAnswer
@@ -403,6 +429,17 @@ public Listing<T> Search<T>(string query, Sorting sortE = Sorting.Relevance, Tim
403429
return new Listing<T>(this, string.Format(SearchUrl, query, sort, time), WebAgent);
404430
}
405431

432+
public Listing<T> SearchByTimestamp<T>(DateTime from, DateTime to, string query = "", string subreddit = "", Sorting sortE = Sorting.Relevance, TimeSorting timeE = TimeSorting.All) where T : Thing
433+
{
434+
string sort = sortE.ToString().ToLower();
435+
string time = timeE.ToString().ToLower();
436+
437+
var fromUnix = (from - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
438+
var toUnix = (to - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
439+
440+
string searchQuery = "(and+timestamp:" + fromUnix + ".." + toUnix + "+'" + query + "'+" + "subreddit:'" + subreddit + "')&syntax=cloudsearch";
441+
return new Listing<T>(this, string.Format(SearchUrl, searchQuery, sort, time), WebAgent);
442+
}
406443

407444

408445
#region SubredditSearching
@@ -441,15 +478,15 @@ public Listing<Subreddit> GetGoldSubreddits()
441478
public Listing<Subreddit> GetDefaultSubreddits()
442479
{
443480
return new Listing<Subreddit>(this, DefaultSubredditsUrl, WebAgent);
444-
}
445-
446-
/// <summary>
447-
/// Returns the Listing of subreddits related to a query.
448-
/// </summary>
449-
/// <returns></returns>
450-
public Listing<Subreddit> SearchSubreddits(string query)
451-
{
452-
return new Listing<Subreddit>(this, string.Format(SearchSubredditsUrl, query), WebAgent);
481+
}
482+
483+
/// <summary>
484+
/// Returns the Listing of subreddits related to a query.
485+
/// </summary>
486+
/// <returns></returns>
487+
public Listing<Subreddit> SearchSubreddits(string query)
488+
{
489+
return new Listing<Subreddit>(this, string.Format(SearchSubredditsUrl, query), WebAgent);
453490
}
454491

455492
#endregion SubredditSearching

RedditSharp/TBUserNote.cs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
7-
namespace RedditSharp
8-
{
9-
public class TBUserNote
10-
{
11-
public int NoteTypeIndex { get; set; }
12-
public string NoteType { get; set; }
13-
public string SubName { get; set; }
14-
public string Submitter { get; set; }
15-
public int SubmitterIndex { get; set; }
16-
public string Message { get; set; }
17-
public string AppliesToUsername { get; set; }
18-
public string Url { get; set; }
19-
private DateTime _timestamp;
20-
public DateTime Timestamp
21-
{
22-
get { return _timestamp; }
23-
set
24-
{
25-
_timestamp = DateTime.SpecifyKind(value, DateTimeKind.Utc);
26-
}
27-
}
28-
}
29-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RedditSharp
8+
{
9+
public class TBUserNote
10+
{
11+
public int NoteTypeIndex { get; set; }
12+
public string NoteType { get; set; }
13+
public string SubName { get; set; }
14+
public string Submitter { get; set; }
15+
public int SubmitterIndex { get; set; }
16+
public string Message { get; set; }
17+
public string AppliesToUsername { get; set; }
18+
public string Url { get; set; }
19+
private DateTime _timestamp;
20+
public DateTime Timestamp
21+
{
22+
get { return _timestamp; }
23+
set
24+
{
25+
_timestamp = DateTime.SpecifyKind(value, DateTimeKind.Utc);
26+
}
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)