-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRedditImageProvider.cs
59 lines (46 loc) · 1.76 KB
/
RedditImageProvider.cs
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
using Reddit;
using Reddit.Controllers;
using Reddit.Exceptions;
namespace EconomyBot;
public class RedditImageProvider : IImageProvider {
private RedditClient reddit;
public RedditImageProvider() {
reddit = new RedditClient(Constants.redditappid, Constants.redditrefreshtoken,
Constants.redditappsecret);
}
public async Task<string> getRandomImage() {
// 70% chance of sfw, 30% chance of nsfw
Subreddit sub;
sub = reddit.Subreddit(new Random().NextDouble() < 0.7 ? "wholesomeyuri" : "actualyuri");
return await getImgFromSub(sub);
}
public async Task<string> getImageFromSub(string sub) {
var subreddit = reddit.Subreddit(sub);
return await getImgFromSub(subreddit);
}
private async Task<string> getImgFromSub(Subreddit sub) {
try {
while (true) {
var posts = sub.Posts.Hot;
var index = new Random().Next(posts.Count);
var post = posts[index];
var imgjson = post.Listing.Preview;
if (imgjson == null) {
continue;
}
var url = imgjson["images"]?[0]?["source"]?.Value<string?>("url");
if (url != null) {
// send the image
return url;
}
if (post.Listing.Preview != null && url != null) {
break;
}
}
}
catch (Exception ex) when (ex is RedditGatewayTimeoutException or RedditServiceUnavailableException) {
throw; // just throw anyway
}
throw new NullReferenceException("You've managed to return a null image even though it shouldn't be possible.");
}
}