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

Subreddit.Posts.MonitorNew() starts in the same place every time #117

Closed
kyleratti opened this issue Jan 3, 2021 · 12 comments
Closed

Subreddit.Posts.MonitorNew() starts in the same place every time #117

kyleratti opened this issue Jan 3, 2021 · 12 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request

Comments

@kyleratti
Copy link

kyleratti commented Jan 3, 2021

I am using Reddit.NET to monitor submissions to a subreddit. It seems, however, that every time my application starts it is retrieving a list of submissions from the same point in time and firing the NewUpdated event for each of them.

module Client =
    let create () =
        let reddit = new RedditClient(
                        AppConfig.getAppId,
                        AppConfig.getRefreshToken,
                        AppConfig.getAppSecret,
                        AppConfig.getAccessToken,
                        "Subreddit Scanner by github.com/kyleratti"
                        )
        reddit
private void Run()
{
	var _reddit = Client.create();
	var sub = _reddit.Subreddit("PublicFreakout").About();
	subs.Posts.NewUpdated += Posts_NewUpdated;
	subs.Posts.MonitorNew();
}

private void Posts_NewUpdated(object sender, Reddit.Controllers.EventArgs.PostsUpdateEventArgs e)
{
	var newPosts = e.Added;

	foreach(var post in newPosts)
	{
		Console.WriteLine(post.About().Fullname);
	}
}

Every time the application starts, it will immediately fire the NewUpdated event for the same reddit post.

I suspect this isn't a bug and I am misunderstanding something and that is causing the bot to authenticate in a certain state/point of time wherein those posts are new, but I'd appreciate any guidance! I've been looking at this for a little while now and am stumped.

Thanks

PS - this is a wonderful library and I am grateful to your hard work on it!

@kyleratti kyleratti changed the title Subreddit.Posts.MonitorNew() seems to start in the same place every time Subreddit.Posts.MonitorNew() starts in the same place every time Jan 3, 2021
@kyleratti
Copy link
Author

Looking at the source, it seems like MonitorNew() will just call GetNew and fire an event for any posts that haven't been seen before.

public List<Post> GetNew(CategorizedSrListingInput categorizedSrListingInput, bool isInterface = false)
{
List<Post> posts = Lists.GetPosts(Dispatch.Listings.New(categorizedSrListingInput, Subreddit), Dispatch);
NewLastUpdated = DateTime.Now;

I am wondering if modifying my run function to call GetNew() and then discard the results would be the correct way/a sufficient workaround? That would seemingly set the newPosts parameter:

private void Run()
{
	var _reddit = Client.create();
	var sub = _reddit.Subreddit("PublicFreakout").About();
	_ = sub.Posts.GetNew();
	sub.Posts.NewUpdated += Posts_NewUpdated;
	sub.Posts.MonitorNew();
}

@kyleratti
Copy link
Author

Follow up: yes, retrieving new, discarding, and then subscribing to the new feed has this working as intended.

ghost pushed a commit to AlexZorzi/RedditToTelegram that referenced this issue Jan 12, 2021
@ghost
Copy link

ghost commented Jan 12, 2021

@kyleratti did you happen to experience the same post being monitored multiple times?

@kyleratti
Copy link
Author

@kyleratti did you happen to experience the same post being monitored multiple times?

Could you clarify what you mean or provide an example? Once I started retrieving all of the posts before monitoring, things worked as expected.

@ghost
Copy link

ghost commented Jan 12, 2021

 _ = sub.Posts.GetHot();
 sub.Posts.HotUpdated += C_postsUpdated;
 sub.Posts.MonitorHot();

My code looks like this, and I get events without problem, the only thing is that sometimes the same post gets monitored multiple times, I attach a example from the console output, where you can see some links coming in multiple times, this is a fairly active sub
Screenshot_20210113-000939_JuiceSSH

@kyleratti
Copy link
Author

kyleratti commented Jan 13, 2021

@Alleyuuki hmmm - I can't say I have noticed that. The only thing I could think of is I am calling .About() on the subreddit controller before using it to monitor:

var sub = redditClient.Subreddit("MySub").About();

@ghost
Copy link

ghost commented Jan 13, 2021

@Alleyuuki hmmm - I can't say I have noticed that. The only thing I could think of is I am calling .About() on the subreddit controller before using it to monitor:

var sub = redditClient.Subreddit("MySub").About();

even with .About(); the same post comes trough multiple times, i might open an issue for this

@sirkris
Copy link
Owner

sirkris commented Jan 13, 2021 via email

@ghost
Copy link

ghost commented Jan 13, 2021

hi @sirkris thanks for the explanation,
here is my code https://github.com/Alleyuuki/RedditToTelegram/blob/master/RedditToTelegram/Program.cs#L47
the multiple posts are coming in in different events, i tought of keeping track of permalinks too, im fairly new with c# language soo my next question might be silly but,

 _ = sub.Posts.GetHot();
 sub.Posts.HotUpdated += C_postsUpdated;
  sub.Posts.MonitorHot();

can the C_postsUpdated function not be a static function? so i can access a list of permaLinks that will be a class variable?

@sirkris
Copy link
Owner

sirkris commented Jan 13, 2021 via email

ghost pushed a commit to AlexZorzi/RedditToTelegram that referenced this issue Jan 13, 2021
@sirkris sirkris self-assigned this Jan 14, 2021
@sirkris sirkris added bug Something isn't working enhancement New feature or request labels Jan 14, 2021
@sirkris
Copy link
Owner

sirkris commented Feb 13, 2021

I just merged in some changes that should fix this. I basically implemented the caching we discussed here. You don't have to do anything differently to make use of it. I made caching enabled by default. You can disable it by passing useCache: false to the monitor method.

The latest changes are available on the develop branch. Could you check it out and let me know if this fixed your issue?

Thanks!

@sirkris sirkris added the Awaiting Feedback Additional information is required before the issue can be resolved. label Feb 13, 2021
@kyleratti
Copy link
Author

Linked against 1.4.0-develop and removed my workaround, looks to be behaving as intended. Thanks!

@sirkris sirkris closed this as completed Feb 14, 2021
@sirkris sirkris removed the Awaiting Feedback Additional information is required before the issue can be resolved. label Feb 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants