forked from CrustyJew/RedditSharp-DEPRECATED-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReddit.cs
More file actions
445 lines (388 loc) · 18.4 KB
/
Reddit.cs
File metadata and controls
445 lines (388 loc) · 18.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RedditSharp.Things;
using System;
using System.Linq;
using System.Security.Authentication;
using System.Threading.Tasks;
using DefaultWebAgent = RedditSharp.WebAgent;
namespace RedditSharp
{
/// <summary>
/// Class to communicate with Reddit.com
/// </summary>
public class Reddit
{
#region Constant Urls
private const string MeUrl = "/api/me.json";
private const string OAuthMeUrl = "/api/v1/me.json";
private const string SubredditAboutUrl = "/r/{0}/about.json";
private const string ComposeMessageUrl = "/api/compose";
private const string RegisterAccountUrl = "/api/register";
private const string GetThingUrl = "/api/info.json?id={0}";
private const string GetCommentUrl = "/r/{0}/comments/{1}/foo/{2}";
private const string GetPostUrl = "{0}.json";
private const string OAuthDomainUrl = "oauth.reddit.com";
private const string SearchUrl = "/search.json?q={0}&restrict_sr=off&sort={1}&t={2}";
private const string UrlSearchPattern = "url:'{0}'";
private const string NewSubredditsUrl = "/subreddits/new.json";
private const string PopularSubredditsUrl = "/subreddits/popular.json";
private const string GoldSubredditsUrl = "/subreddits/gold.json";
private const string DefaultSubredditsUrl = "/subreddits/default.json";
private const string SearchSubredditsUrl = "/subreddits/search.json?q={0}";
private const string CreateLiveEventUrl = "/api/live/create";
private const string GetLiveEventUrl = "https://www.reddit.com/live/{0}/about";
#endregion
#region Properties
internal IWebAgent WebAgent { get; set; }
/// <summary>
/// Captcha solver instance to use when solving captchas.
/// </summary>
public ICaptchaSolver CaptchaSolver;
/// <summary>
/// The authenticated user for this instance.
/// </summary>
public AuthenticatedUser User { get; set; }
/// <summary>
/// Sets the Rate Limiting Mode of the underlying WebAgent
/// </summary>
public RateLimitMode RateLimit
{
get { return WebAgent.RateLimiter.Mode; }
set { WebAgent.RateLimiter.Mode = value; }
}
/// <summary>
/// Gets the FrontPage using the current Reddit instance.
/// </summary>
public Subreddit FrontPage
{
get { return Subreddit.GetFrontPage(WebAgent); }
}
/// <summary>
/// Gets /r/All using the current Reddit instance.
/// </summary>
public Subreddit RSlashAll
{
get { return Subreddit.GetRSlashAll(WebAgent); }
}
#endregion
#pragma warning disable 1591
public Reddit()
: this(true) { }
public Reddit(bool useSsl)
{
DefaultWebAgent defaultAgent = new DefaultWebAgent();
DefaultWebAgent.Protocol = useSsl ? "https" : "http";
WebAgent = defaultAgent;
CaptchaSolver = new ConsoleCaptchaSolver();
}
#pragma warning restore 1591
/// <summary>
///
/// </summary>
/// <param name="limitMode">Rate limit</param>
/// <param name="useSsl">use ssl. Defaults to true.</param>
public Reddit(RateLimitMode limitMode, bool useSsl = true)
: this(useSsl)
{
DefaultWebAgent.DefaultUserAgent = "";
DefaultWebAgent.DefaultRateLimiter.Mode = limitMode;
DefaultWebAgent.RootDomain = "www.reddit.com";
}
/// <summary>
/// Constructor.
/// </summary>
/// <param name="accessToken">oauth access token.</param>
public Reddit(string accessToken)
: this(true)
{
DefaultWebAgent.RootDomain = OAuthDomainUrl;
WebAgent.AccessToken = accessToken;
Task.Run(InitOrUpdateUserAsync);
}
/// <summary>
/// Creates a Reddit instance with the given WebAgent implementation
/// </summary>
/// <param name="agent">Implementation of IWebAgent interface. Used to generate requests.</param>
public Reddit(IWebAgent agent) : this(agent, false)
{
}
/// <summary>
/// Creates a Reddit instance with the given WebAgent implementation
/// </summary>
/// <param name="agent">Implementation of IWebAgent interface. Used to generate requests.</param>
/// <param name="initUser">Whether to run InitOrUpdateUser, requires <paramref name="agent"/> to have credentials first.</param>
public Reddit(IWebAgent agent, bool initUser)
{
WebAgent = agent;
CaptchaSolver = new ConsoleCaptchaSolver();
if (initUser) Task.Run(InitOrUpdateUserAsync).Wait();
}
/// <summary>
/// Get a reddit user by name.
/// </summary>
/// <param name="name">user name</param>
/// <returns></returns>
public Task<RedditUser> GetUserAsync(string name)
{
return RedditUser.GetUserAsync(WebAgent, name);
}
/// <summary>
/// Initializes the User property if it's null,
/// otherwise replaces the existing user object
/// with a new one fetched from reddit servers.
/// </summary>
public async Task InitOrUpdateUserAsync()
{
var json = await WebAgent.Get(!string.IsNullOrEmpty(WebAgent.AccessToken) || WebAgent.GetType() == typeof(RefreshTokenWebAgent) ? OAuthMeUrl : MeUrl).ConfigureAwait(false);
User = new AuthenticatedUser(WebAgent, json);
}
/// <summary>
/// Get a subreddit by name.
/// </summary>
/// <param name="name">subreddit name with or without preceding /r/</param>
/// <returns></returns>
public Task<Subreddit> GetSubredditAsync(string name)
{
return Subreddit.GetByNameAsync(WebAgent, name);
}
/// <summary>
/// Get information about a domain.
/// </summary>
/// <param name="domain">domain name</param>
/// <returns></returns>
public Domain GetDomain(string domain)
{
if (!domain.StartsWith("http://") && !domain.StartsWith("https://"))
domain = "http://" + domain;
var uri = new Uri(domain);
return new Domain(WebAgent, uri);
}
/// <summary>
/// Get a <see cref="Post"/> by uri.
/// </summary>
/// <param name="uri">uri to fetch</param>
/// <returns></returns>
public async Task<Post> GetPostAsync(Uri uri)
{
return new Post(WebAgent, await Helpers.GetTokenAsync(WebAgent, uri).ConfigureAwait(false));
}
/// <summary>
/// Create a Reddit Live thread.
/// </summary>
/// <param name="title">Required.</param>
/// <param name="description">Required</param>
/// <param name="resources"></param>
/// <param name="nsfw"></param>
/// <returns></returns>
public async Task<LiveUpdateEvent> CreateLiveEventAsync(string title, string description, string resources = "", bool nsfw = false)
{
if (String.IsNullOrEmpty(title))
throw new ArgumentException(nameof(title));
if (String.IsNullOrEmpty(description))
throw new ArgumentException(nameof(description));
var json = await WebAgent.Post(CreateLiveEventUrl, new
{
api_type = "json",
title = title,
description = description,
resources = resources,
nsfw = nsfw
}).ConfigureAwait(false);
if (json["json"]["errors"].Any())
throw new Exception(json["json"]["errors"][0][0].ToString());
var id = json["json"]["data"]["id"].ToString();
return await GetLiveEvent(new Uri(String.Format(GetLiveEventUrl, id))).ConfigureAwait(false);
}
/// <summary>
/// Get a reddit live thread.
/// </summary>
/// <param name="uri">Uri of the live thread.</param>
/// <returns></returns>
public async Task<LiveUpdateEvent> GetLiveEvent(Uri uri)
{
if (!uri.AbsoluteUri.EndsWith("about"))
uri = new Uri(uri.AbsoluteUri + "/about");
var token = await Helpers.GetTokenAsync(WebAgent, uri).ConfigureAwait(false);
return new LiveUpdateEvent(WebAgent, token);
}
/// <summary>
/// Compose a private message.
/// </summary>
/// <param name="subject">message subject</param>
/// <param name="body">markdown body</param>
/// <param name="to">target author or subreddit</param>
/// <param name="fromSubReddit">The subreddit to send the message as (optional).</param>
/// <param name="captchaId"></param>
/// <param name="captchaAnswer"></param>
/// <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>
/// <exception cref="AuthenticationException">Thrown when a subreddit is passed in and the user is not a mod of that sub.</exception>
public async Task ComposePrivateMessageAsync(string subject, string body, string to, string fromSubReddit = "", string captchaId = "", string captchaAnswer = "")
{
if (User == null)
throw new Exception("User can not be null.");
if (!string.IsNullOrWhiteSpace(fromSubReddit))
{
var subReddit = await GetSubredditAsync(fromSubReddit).ConfigureAwait(false);
var modNameList = (await subReddit.GetModeratorsAsync().ConfigureAwait(false)).Select(b => b.Name).ToList();
if (!modNameList.Contains(User.Name))
throw new AuthenticationException(
string.Format(
@"User {0} is not a moderator of subreddit {1}.",
User.Name,
subReddit.Name));
}
var json = await WebAgent.Post(ComposeMessageUrl, new
{
api_type = "json",
subject,
text = body,
to,
from_sr = fromSubReddit,
uh = User.Modhash,
iden = captchaId,
captcha = captchaAnswer
}).ConfigureAwait(false);
ICaptchaSolver solver = CaptchaSolver; // Prevent race condition
if (json["json"]["errors"].Any() && json["json"]["errors"][0][0].ToString() == "BAD_CAPTCHA" && solver != null)
{
captchaId = json["json"]["captcha"].ToString();
CaptchaResponse captchaResponse = solver.HandleCaptcha(new Captcha(captchaId));
if (!captchaResponse.Cancel) // Keep trying until we are told to cancel
await ComposePrivateMessageAsync(subject, body, to, fromSubReddit, captchaId, captchaResponse.Answer).ConfigureAwait(false);
}
}
/// <summary>
/// Registers a new Reddit user
/// </summary>
/// <param name="userName">The username for the new account.</param>
/// <param name="passwd">The password for the new account.</param>
/// <param name="email">The optional recovery email for the new account.</param>
/// <returns>The newly created user account</returns>
public async Task<AuthenticatedUser> RegisterAccountAsync(string userName, string passwd, string email = "")
{
var json = await WebAgent.Post(RegisterAccountUrl, new
{
api_type = "json",
email = email,
passwd = passwd,
passwd2 = passwd,
user = userName
}).ConfigureAwait(false);
return new AuthenticatedUser(WebAgent, json);
// TODO: Error
}
/// <summary>
/// Get a <see cref="Thing"/> by full name.
/// </summary>
/// <param name="fullname"></param>
/// <returns></returns>
public Task<Thing> GetThingByFullnameAsync(string fullname)
{
return Helpers.GetThingByFullnameAsync(WebAgent, fullname);
}
/// <summary>
/// Get a <see cref="Comment"/>.
/// </summary>
/// <param name="subreddit">subreddit name in which the comment resides</param>
/// <param name="name">comment base36 id</param>
/// <param name="linkName">post base36 id</param>
/// <returns></returns>
public Task<Comment> GetCommentAsync(string subreddit, string name, string linkName)
{
if (linkName.StartsWith("t3_"))
linkName = linkName.Substring(3);
if (name.StartsWith("t1_"))
name = name.Substring(3);
var url = string.Format(GetCommentUrl, subreddit, linkName, name);
return GetCommentAsync(new Uri(url));
}
/// <summary>
/// Get a <see cref="Comment"/>.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
public async Task<Comment> GetCommentAsync(Uri uri)
{
var url = string.Format(GetPostUrl, uri.AbsoluteUri);
var json = await WebAgent.Get(url).ConfigureAwait(false);
var sender = new Post(WebAgent, json[0]["data"]["children"][0]);
return new Comment(WebAgent, json[1]["data"]["children"][0], sender);
}
/// <summary>
/// Return a <see cref="Listing{T}"/> of items matching url search.
/// </summary>
/// <typeparam name="T"><see cref="Thing"/></typeparam>
/// <param name="url">query url</param>
/// <returns></returns>
public Listing<T> SearchByUrl<T>(string url) where T : Thing
{
var urlSearchQuery = string.Format(UrlSearchPattern, url);
return Search<T>(urlSearchQuery);
}
/// <summary>
/// Return a <see cref="Listing{T}"/> of items matching search.
/// </summary>
/// <typeparam name="T"><see cref="Thing"/></typeparam>
/// <param name="query">string to query</param>
/// <param name="sortE">Order by <see cref="Sorting"/></param>
/// <param name="timeE">Order by <see cref="TimeSorting"/></param>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
/// <returns></returns>
public Listing<T> Search<T>(string query, Sorting sortE = Sorting.Relevance, TimeSorting timeE = TimeSorting.All, int max = -1) where T : Thing
{
string sort = sortE.ToString().ToLower();
string time = timeE.ToString().ToLower();
return Listing<T>.Create(this.WebAgent, string.Format(SearchUrl, query, sort, time), max, 100);
}
/// <summary>
/// Return a <see cref="Listing{T}"/> of items matching search with a given time period.
/// </summary>
/// <typeparam name="T"><see cref="Thing"/></typeparam>
/// <param name="from">DateTime from</param>
/// <param name="to">DateTime to</param>
/// <param name="query">string to query</param>
/// <param name="subreddit">subreddit in which to search</param>
/// <param name="sortE">Order by <see cref="Sorting"/></param>
/// <param name="timeE">Order by <see cref="TimeSorting"/></param>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
/// <returns></returns>
public Listing<T> SearchByTimestamp<T>(DateTime from, DateTime to, string query = "", string subreddit = "", Sorting sortE = Sorting.Relevance, TimeSorting timeE = TimeSorting.All, int max = -1) where T : Thing
{
string sort = sortE.ToString().ToLower();
string time = timeE.ToString().ToLower();
var fromUnix = (from - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
var toUnix = (to - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
string searchQuery = "(and+timestamp:" + fromUnix + ".." + toUnix + "+'" + query + "'+" + "subreddit:'" + subreddit + "')&syntax=cloudsearch";
return Listing<T>.Create(this.WebAgent, string.Format(SearchUrl, searchQuery, sort, time), max, 100);
}
#region SubredditSearching
/// <summary>
/// Returns a Listing of newly created subreddits.
/// </summary>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
public Listing<Subreddit> GetNewSubreddits(int max = -1) => Listing<Subreddit>.Create(this.WebAgent, NewSubredditsUrl, max, 100);
/// <summary>
/// Returns a Listing of the most popular subreddits.
/// </summary>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
public Listing<Subreddit> GetPopularSubreddits(int max = -1) => Listing<Subreddit>.Create(this.WebAgent, PopularSubredditsUrl, max, 100);
/// <summary>
/// Returns a Listing of Gold-only subreddits. This endpoint will not return anything if the authenticated Reddit account does not currently have gold.
/// </summary>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
public Listing<Subreddit> GetGoldSubreddits(int max = -1) => Listing<Subreddit>.Create(this.WebAgent, GoldSubredditsUrl, max, 100);
/// <summary>
/// Returns the Listing of default subreddits.
/// </summary>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
public Listing<Subreddit> GetDefaultSubreddits(int max = -1) => Listing<Subreddit>.Create(this.WebAgent, DefaultSubredditsUrl, max, 100);
/// <summary>
/// Returns the Listing of subreddits related to a query.
/// </summary>
/// <param name="query">Search query</param>
/// <param name="max">Maximum number of records to return. -1 for unlimited.</param>
public Listing<Subreddit> SearchSubreddits(string query, int max = -1) => Listing<Subreddit>.Create(this.WebAgent, string.Format(SearchSubredditsUrl, query), max, 100);
#endregion SubredditSearching
}
}