Skip to content

Commit 5effd68

Browse files
committed
Added the ability to search a subredit by a date range.
Fixed a bug where json deserialization would fail because values were not found.
1 parent a252245 commit 5effd68

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

RedditSharp/RedditSharp.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Reference Include="System.Xml" />
5252
</ItemGroup>
5353
<ItemGroup>
54+
<Compile Include="Utils\DateTimeExtensions.cs" />
5455
<Compile Include="SpamFilterSettings.cs" />
5556
<Compile Include="Things\AuthenticatedUser.cs" />
5657
<Compile Include="AuthProvider.cs" />

RedditSharp/Things/Subreddit.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using HtmlAgilityPack;
77
using Newtonsoft.Json;
88
using Newtonsoft.Json.Linq;
9+
using RedditSharp.Utils;
910

1011
namespace RedditSharp.Things
1112
{
@@ -37,6 +38,7 @@ public class Subreddit : Thing
3738
private const string FlairListUrl = "/r/{0}/api/flairlist.json";
3839
private const string CommentsUrl = "/r/{0}/comments.json";
3940
private const string SearchUrl = "/r/{0}/search.json?q={1}&restrict_sr=on&sort={2}&t={3}";
41+
private const string SearchUrlDate = "/r/{0}/search.json?q=timestamp:{1}..{2}&restrict_sr=on&sort={3}&syntax=cloudsearch";
4042

4143
[JsonIgnore]
4244
private Reddit Reddit { get; set; }
@@ -66,8 +68,8 @@ public class Subreddit : Thing
6668
[JsonProperty("header_title")]
6769
public string HeaderTitle { get; set; }
6870

69-
[JsonProperty("over18")]
70-
public bool? NSFW { get; set; }
71+
[JsonProperty("over_18")]
72+
public bool NSFW { get; set; }
7173

7274
[JsonProperty("public_description")]
7375
public string PublicDescription { get; set; }
@@ -89,20 +91,20 @@ public class Subreddit : Thing
8991
/// Property determining whether the current logged in user is a moderator on this subreddit.
9092
/// </summary>
9193
[JsonProperty("user_is_moderator")]
92-
public bool UserIsModerator { get; set; }
94+
public bool? UserIsModerator { get; set; }
9395

94-
/// <summary>
95-
/// Property giving the moderator permissions of the logged in user on this subreddit.
96+
/// <summary>
97+
/// Property giving the moderator permissions of the logged in user on this subreddit.
9698
/// </summary>
97-
[JsonProperty("mod_permissions")]
98-
[JsonConverter(typeof(ModeratorPermissionConverter))]
99+
[JsonProperty("mod_permissions")]
100+
[JsonConverter(typeof(ModeratorPermissionConverter))]
99101
public ModeratorPermission ModPermissions { get; set; }
100102

101103
/// <summary>
102104
/// Property determining whether the current logged in user is banned from the subreddit.
103105
/// </summary>
104106
[JsonProperty("user_is_banned")]
105-
public bool UserIsBanned { get; set; }
107+
public bool? UserIsBanned { get; set; }
106108

107109
[JsonIgnore]
108110
public string Name { get; set; }
@@ -177,6 +179,11 @@ public Listing<Post> Search(string terms)
177179
return new Listing<Post>(Reddit, string.Format(SearchUrl, Name, Uri.EscapeUriString(terms), "relevance", "all"), WebAgent);
178180
}
179181

182+
public Listing<Post> Search(DateTime from, DateTime to)
183+
{
184+
return new Listing<Post>(Reddit, string.Format(SearchUrlDate, Name, from.DateTimeToUnixTimestamp(), to.DateTimeToUnixTimestamp(), "new"), WebAgent);
185+
}
186+
180187
public SubredditSettings Settings
181188
{
182189
get
@@ -271,12 +278,12 @@ public IEnumerable<ModeratorUser> Moderators
271278
}
272279
}
273280

274-
public IEnumerable<TBUserNote> UserNotes
275-
{
276-
get
277-
{
278-
return ToolBoxUserNotes.GetUserNotes(WebAgent, Name);
279-
}
281+
public IEnumerable<TBUserNote> UserNotes
282+
{
283+
get
284+
{
285+
return ToolBoxUserNotes.GetUserNotes(WebAgent, Name);
286+
}
280287
}
281288

282289
public Subreddit Init(Reddit reddit, JToken json, IWebAgent webAgent)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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.Utils
8+
{
9+
internal static class DateTimeExtensions
10+
{
11+
public static double DateTimeToUnixTimestamp(this DateTime dateTime)
12+
{
13+
double time = (dateTime - new DateTime(1970, 1, 1).ToLocalTime()).TotalSeconds;
14+
15+
return Convert.ToInt32(time);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)