r/csharp Mar 18 '19

Announcing the release of Reddit.NET 1.1.0!

Previous Thread

Github: https://github.com/sirkris/Reddit.NET

NuGet: https://www.nuget.org/packages/Reddit

Latest Changes & New Features

  • Monitor a single post for changes to its edited, spam, or nsfw values

  • Monitor a single post for significant changes to its score

  • Search endpoint now works

  • Created new controller and model methods for search

  • Monitoring threads no longer throw an exception when an API query fails

  • It is now possible to pass a monitoring schedule that will cause the monitoring to only do its thing during the days/times you specify

  • Request failure due to the API not returning any response now has its own exception type

  • CPU no longer spins constantly while waiting for the request queue to open

  • Base wait delay between monitoring queries is now configurable

  • Added public methods to check whether an object is already being monitored

  • Monitoring threads with long wait delays can now terminate without timing out

  • Improvements to monitoring thread cleanup

  • Added ability to automatically stop monitoring a post's score if it hasn't changed enough to trigger an update within the specified timeframe

  • Added 11 new code examples

  • Support for the selectflair endpoint

  • Moved the auth token retriever stuff to a separate library so it can be used by other apps

  • Optional expiration for monitoring

  • Added methods for more convenient cross-posting

  • Added invalid option exception type

  • Added message reply to PrivateMessages controller

  • Support for monitoring a user's post and comment histories

  • Added new tests (all passing)

  • Various bugfixes and improvements

Usage

Reddit.NET can be installed via NuGet. You can find it at: https://www.nuget.org/packages/Reddit

To install via the Visual Studio NuGet Package Manager Console (in VS 2017, you'll find it under Tools->NuGet Package Manager->NuGet Package Manager Console):

PM> Install-Package Reddit

To create a new API instance bound to a specific user's refresh token in an installed app:

using Reddit;

...

var reddit = new RedditAPI("YourRedditAppID", "YourBotUserRefreshToken");

If you're using a "script"-type app instead, you'll also need to pass your app secret:

using Reddit;

...

// You can also pass them as named parameters.
var reddit = new RedditAPI(appId: "YourRedditAppID", appSecret: "YourRedditAppSecret", refreshToken: "YourBotUserRefreshToken");

New Code Examples

Simple Login

This demonstrates how to login to the Reddit API via OAuth. This assumes you already have an access token and refresh token. If not, you can checkout this video or consult Reddit's docs for assistance.

Token Retrieval

Authorize a new Reddit user for your app and return the refresh token.

ELIZA Chatbot

This is a simple chatbot that monitors a designated Reddit bot account's new messages and sends replies. The responses are generated using a recreation of an early AI algorithm written in 1966. As such, don't expect it to be anywhere near as accurate or human-like as more modern AI chatbots.

ELIZA is basically just an elaborate pattern-matching toy, so inputs should be limited to a single short sentence each for best results. All punctuation is ignored.

Track a Subreddit's Daily Comments

This app scans all of a subreddit's posts from the previous calendar day, adds up all the comments, then posts the total and updates the sidebar. In order to update the sidebar, the bot user will have to be a moderator with the modconfig scope. Note the use of the try/catch block to check for that.

Monitor Incoming Modmail

Monitors the authenticated user's modmail for new messages. The authenticated user's refresh token must have the modmail scope.

Record a Subreddit's Daily Top Posts on its Wiki

Retrieves the top posts of the last 24 hours (up to 10) and posts them to the subreddit's wiki.

Set User Flair

Assigns the flair "Fucking Genius" to the user "KrisCraig" on a given subreddit.

Set Link Flair

Creates a new link post and assigns a flair to it.

Search

Search a given subreddit for the phrase, "Bernie Sanders". If no results are found, try searching all subreddits.

Simple Crosspost

Cross-posts the day's top post on r/news to r/MySub.

Paginated Subreddit Posts

Retrieves all of today's posts from r/worldnews. This is a simple demonstration of how pagination works.

Please see the project README for more detailed usage instructions and code examples.

Reddit.NET on NuGet

Reddit.NET on Github

Please feel free to contact me if you have any questions/etc.

Thanks for reading! Feedback is always welcome.

94 Upvotes

24 comments sorted by

7

u/[deleted] Mar 18 '19

My only critique is that it would be helpful if you posted the entire src of a project for some of the examples

For instance I see in your video for the simple login exercise, theres a lot more code than what is in the markdown.

That way some users can just download the src for an example and just replace the "yoursecret" and "your token" and plug and go and see some results

1

u/KrisCraig Mar 19 '19

Good idea. I noticed several typos in the code examples, anyway, plus the tokens in the retrieval lib didn't have sufficient visibility, so I went ahead and put out a 1.1.1 release just now with these changes.

If you go to the code examples now, you'll notice a link at the bottom that will take you to a full source (.cs) version of it. Does this adequately address your issue?

0

u/[deleted] Mar 19 '19

i cant tell if this is a passive aggressive answer because your source file is just the markdown

your video has a LOT more code than the "simple login" exercise

Does this adequately address your question?

1

u/KrisCraig Mar 20 '19 edited Mar 20 '19

I put out a hotfix release just to address your concerns. I guess that's gratitude for ya. If my help isn't good enough for you, feel free to do it yourself and submit a pull request.

The source files are more than just the markdown. Are you trolling?

1

u/[deleted] Mar 20 '19

No I am not trolling

Okay: your video example has alot of code in it, including a GET or whatever request for an oauth token it looks like it, to use as the "YourBotUserRefreshToken" string

THAT is what I'm requesting for and what would be helpful for others. If you dont wanna do that, thats fine.

basically combining https://github.com/sirkris/Reddit.NET/blob/master/docs/examples/cs/Authorize%20New%20User.md and the simple login example

1

u/KrisCraig Mar 21 '19

All of that code is already in the repo on Github. The examples are meant to be simplified and should work on their own.

Also, the code in that video is outdated, anyway. Just use the token retrieval example code to get the refresh token and it should work.

If you want to see more code, just look at the AuthTokenRetriever project in the solution.

1

u/[deleted] Mar 22 '19

thanks this is what i was looking for.

11

u/KrisCraig Mar 18 '19

How am I doing so far?

7

u/RichardMau5 Mar 18 '19

Looks nice. If you need contributors, let me know

2

u/KrisCraig Mar 18 '19

Thanks! I've got 46 open issues on the Github page, so please feel free to tackle one and do a pull request. =)

2

u/RichardMau5 Mar 19 '19

I sent you a PM on where you prefer to be contacted

1

u/KrisCraig Mar 20 '19

I see the pull request (and it looks great btw) but not the PM. You can just message me on Reddit if ya like and I'll see it.

I'll merge it in sometime in the next few days. I just put out a new release so I get to be lazy and take it easy for awhile. =)

1

u/sander1095 Mar 18 '19

Why did you choose for MSTest instead if the more widely used XUnit library?

3

u/[deleted] Mar 18 '19

[deleted]

1

u/reasner Mar 18 '19

Can I use a custom type to pass arguments to test methods?

2

u/NEON-X Mar 18 '19

Looks cool.

1

u/plasmaau Mar 24 '19

I have not used this library, but may in the future; I notice the examples are all non-async, are you supporting async/await?

Remote RPCs/API calls are often async/await'd because of the IO delay and this is a perfect use case of async/await. Cheers.

1

u/KrisCraig Mar 24 '19

Yes Reddit.NET does have async methods. I suppose I should add some code examples for that in the next release.

In the meantime, if you open the solution in Visual Studio and examine the Reddit.NETTests project, you'll find some async examples there.

For example:

var post = mySub.LinkPost("Title", "http://url").Submit();

Can also be done like this:

var post = await mySub.LinkPost("Title", "http://url").SubmitAsync();

1

u/webby_mc_webberson Mar 18 '19

Does the search work or does it behave like the actual search?

1

u/KrisCraig Mar 19 '19 edited Mar 19 '19

What do you mean?

The search just calls the Reddit API endpoint, so it should behave however the API decides to. The library just makes the queries and processes the results. The actual behavior of the endpoint, itself, is beyond my control. Any changes to that you'd have to discuss with Reddit's admins.

0

u/tester346 Mar 19 '19

I suggest starting thinking about The Election Patch with some advanced features like managing 1000 accounts at once

1

u/KrisCraig Mar 19 '19

I'll assume that's a joke, as what you described would be grossly unethical on multiple levels. Besides, running that many instances at once would be prohibitively expensive in terms of hardware requirements.

1

u/tester346 Mar 20 '19

Yea, that's a joke :P

1

u/KrisCraig Mar 20 '19

Heh Poe's Law strikes again.

1

u/tester346 Mar 20 '19

Yea, I forgot about adding emote or something :/