Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use rss instead of json for reddit tracking #98

Open
wants to merge 1 commit into
base: SelfHost
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 19 additions & 22 deletions Data/Tracker/RedditTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using MongoDB.Bson.Serialization.Attributes;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.ServiceModel.Syndication;

namespace MopsBot.Data.Tracker
{
Expand All @@ -34,8 +35,8 @@ public RedditTracker(string name) : base()

try
{
var test = fetchPosts().Result;
if (test.data.children.Count == 0)
var test = FetchPosts().Result;
if (!test.Items.Any())
throw new Exception("");
}
catch (Exception e)
Expand Down Expand Up @@ -78,23 +79,23 @@ public async override void CheckForChange_Elapsed(object stateinfo)
{
try
{
var allThings = await fetchPosts();
var allThings = await FetchPosts();

foreach (var channel in ChannelConfig)
{
var minPostAge = (int)ChannelConfig[channel.Key][POSTAGE];
var newPosts = allThings.data.children.TakeWhile(x => x.data.created_utc > LastCheck[channel.Key]).ToList();
newPosts.RemoveAll(x => (DateTime.UtcNow - DateTimeOffset.FromUnixTimeSeconds((long)x.data.created_utc)).TotalMinutes < minPostAge);
var newPosts = allThings.Items.TakeWhile(x => x.PublishDate.ToUnixTimeSeconds() > LastCheck[channel.Key]).ToList();
newPosts.RemoveAll(x => (DateTimeOffset.UtcNow - x.PublishDate).TotalMinutes < minPostAge);

if (newPosts.Count > 0)
{
LastCheck[channel.Key] = newPosts.Max(x => x.data.created_utc);
LastCheck[channel.Key] = newPosts.Max(x => x.PublishDate.ToUnixTimeSeconds());
await UpdateTracker();

newPosts.Reverse();
foreach (var post in newPosts)
await OnMajorChangeTracked(channel.Key, await createEmbed(post.data), (string)ChannelConfig[channel.Key]["Notification"]);

foreach (var post in newPosts){
await OnMinorChangeTracked(channel.Key, $"{ChannelConfig[channel.Key]["Notification"]}\n{post.Links.First().Uri}");
}
}
}
}
Expand All @@ -104,24 +105,20 @@ public async override void CheckForChange_Elapsed(object stateinfo)
}
}

public static async Task<List<Embed>> checkReddit(string subreddit, string query = null, int limit = 1)
public static async Task<List<Uri>> CheckReddit(string subreddit, string query = null, int limit = 1)
{
var results = await FetchJSONDataAsync<RedditResult>($"https://www.reddit.com/r/{subreddit}/" +
$"{(query != null ? $"search.json?sort=new&restrict_sr=on&q={query}" : "new.json?restrict_sr=on")}" + $"&limit={limit}");
var results = await FetchRSSData($"https://www.reddit.com/r/{subreddit}/" +
$"{(query != null ? $"search.rss?sort=new&restrict_sr=on&q={query}" : "new.rss?restrict_sr=on")}" + $"&limit={limit}");

List<Embed> embeds = new List<Embed>();
foreach (var post in results.data.children)
{
embeds.Add(await createEmbed(post.data));
}

return embeds;
return results.Items.Select(x => x.Links.First().Uri).ToList();
}

private async Task<RedditResult> fetchPosts()
private async Task<SyndicationFeed> FetchPosts()
{
return await FetchJSONDataAsync<RedditResult>($"https://www.reddit.com/r/{Name.Split(" ")[0]}/" +
$"{(Name.Split(" ").Length > 1 ? $"search.json?sort=new&restrict_sr=on&q={string.Join(" ", Name.Split(" ").Skip(1))}" : "new.json?restrict_sr=on")}");
var posts = await FetchRSSData($"https://www.reddit.com/r/{Name.Split(" ")[0]}/" +
$"{(Name.Split(" ").Length > 1 ? $"search.rss?sort=new&restrict_sr=on&q={string.Join(" ", Name.Split(" ").Skip(1))}" : "new.rss?restrict_sr=on")}");

return posts;
}

///<summary>Builds an embed out of the changed stats, and sends it as a Discord message </summary>
Expand Down
4 changes: 2 additions & 2 deletions Module/Slash.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ public async Task SetNotification([Autocomplete(typeof(TrackerAutocompleteHandle
[RequireUserPermission(ChannelPermission.ManageChannels)]
public async Task Check(int limit, string subreddit, string query = null)
{
var result = await RedditTracker.checkReddit(subreddit, query, limit);
await FollowupAsync(embeds: result.ToArray());
var result = await RedditTracker.CheckReddit(subreddit, query, limit);
await FollowupAsync(string.Join("\n", result.Select(r => r.ToString())));
}

[SlashCommand("showconfig", "Shows all the settings for this tracker, and their values")]
Expand Down
4 changes: 2 additions & 2 deletions Module/Tracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,8 @@ public async Task SetNotification(BaseTracker subreddit, string notification = "
[RequireUserPermission(ChannelPermission.ManageChannels)]
public async Task Check(int limit, string subreddit, [Remainder] string query = null)
{
var result = await RedditTracker.checkReddit(subreddit, query, limit);
await MopsBot.Data.Interactive.MopsPaginator.CreatePagedMessage(Context.Channel.Id, result);
var result = await RedditTracker.CheckReddit(subreddit, query, limit);
await ReplyAsync(string.Join("\n", result.Select(r => r.ToString())));
}

[Command("ShowConfig")]
Expand Down