Skip to content

Commit af2008e

Browse files
committed
Merge pull request #194 from dburner/master
Added the ability to search a subredit by a date range.
2 parents e4c9aaa + 9fc6c01 commit af2008e

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-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: 23 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
{
@@ -38,6 +39,7 @@ public class Subreddit : Thing
3839
private const string FlairListUrl = "/r/{0}/api/flairlist.json";
3940
private const string CommentsUrl = "/r/{0}/comments.json";
4041
private const string SearchUrl = "/r/{0}/search.json?q={1}&restrict_sr=on&sort={2}&t={3}";
42+
private const string SearchUrlDate = "/r/{0}/search.json?q=timestamp:{1}..{2}&restrict_sr=on&sort={3}&syntax=cloudsearch";
4143

4244
[JsonIgnore]
4345
private Reddit Reddit { get; set; }
@@ -67,8 +69,8 @@ public class Subreddit : Thing
6769
[JsonProperty("header_title")]
6870
public string HeaderTitle { get; set; }
6971

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

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

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

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

108110
[JsonIgnore]
109111
public string Name { get; set; }
@@ -187,6 +189,13 @@ public Listing<Post> Search(string terms)
187189
return new Listing<Post>(Reddit, string.Format(SearchUrl, Name, Uri.EscapeUriString(terms), "relevance", "all"), WebAgent);
188190
}
189191

192+
public Listing<Post> Search(DateTime from, DateTime to, Sorting sortE = Sorting.New)
193+
{
194+
string sort = sortE.ToString().ToLower();
195+
196+
return new Listing<Post>(Reddit, string.Format(SearchUrlDate, Name, from.DateTimeToUnixTimestamp(), to.DateTimeToUnixTimestamp(), sort), WebAgent);
197+
}
198+
190199
public SubredditSettings Settings
191200
{
192201
get
@@ -281,12 +290,12 @@ public IEnumerable<ModeratorUser> Moderators
281290
}
282291
}
283292

284-
public IEnumerable<TBUserNote> UserNotes
285-
{
286-
get
287-
{
288-
return ToolBoxUserNotes.GetUserNotes(WebAgent, Name);
289-
}
293+
public IEnumerable<TBUserNote> UserNotes
294+
{
295+
get
296+
{
297+
return ToolBoxUserNotes.GetUserNotes(WebAgent, Name);
298+
}
290299
}
291300

292301
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)