reddit 0.6.0

dart-reddit #

A Reddit library for Dart, inspired by reddit.js (API) and raw.js (Auth).

Documentation #

See the Dart documentation.

Usage #

Add the following to your pubspec.yaml:

dependencies:
  reddit: any

Creating a client #

The top class Reddit takes a Client as parameter. This client can be constructed using thehttp package.

// in Dart VM
Reddit reddit = new Reddit(new Client());
// in browser
Reddit reddit = new Reddit(new BrowserClient());

OAuth #

To enable OAuth, you will need an app identifier and a secret. Get them here.

OAuth is required for some of the public endpoints (those marked OAuth-only) and will be required for all endpoints starting August 3, 2015 (see this announcement).

There are two options: App-only and User authorization.

  • App-only auth
reddit.authSetup(identifier, secret);
// with user info
await reddit.authFinish(username: "sroose", password: "correct horse battery staple");
// or without
await reddit.authFinish();

Note that not providing (developer) user info will result in getting 503 Service Unavailable responses after a while.

  • User-enabled auth
reddit.authSetup(identifier, secret);
Uri authUrl = reddit.authUrl("https://myapp.com/auth_redirect");
// redirect user to authUrl and gather the response from the auth server
await reddit.authFinish(response: authServerResponse);
// or if you already extracted the auth code from the response
await reddit.authFinish(code: authCode);

Queries, filters and listings #

Most methods in the API construct a Query, which can be fetched to get a future with the results.

Most queries allow filtering. For the supported filters, we refer to the Reddit API docs or the documentation for this library.

// without filters
reddit.frontPage.newPosts().fetch().then(print);
// filtered
reddit.frontPage.hot().limit(10).fetch().then(print);

A lot of queries also are listings. Listings allow for browsing through content across multiple queries.

// using the regular fetch() method (not recommended)
reddit.sub("dartlang").top("day").fetch().then((result) {
  print(result);
  if (notEnough) {
    result.fetchMore().then((result) {
      print(result);
      if (stillNotEnough) {
        result.fetchmore().then(print);
      }
    });
  }
});

// or using the dart:async API
reddit.sub("dartlang").top("month").listen((result) {
  print(result);
  if (notEnough) {
    result.fetchMore();
  }
})

Browsing Reddit #

You can use the standard read-only API to browse Reddit.

// the front page
reddit.front.hot().fetch().then(print);
// or subreddits
reddit.sub("dartlang").hot().fetch().then(print);

Some examples using filters:

reddit.sub("dartlang").top().t("day").limit(10).fetch().then(print);

Comments #

Fetching comments for a link:

reddit.sub("dartlang").comments("2ek93l").depth(3).fetch().then(print);

Or a single comment:

reddit.sub("dartlang").comments("2ek93l").comment("ck0mkcy").context(2).fetch().then(print);

Search through reddit:

reddit.sub("dartlang").search("reddit api").limit(5).sort("hot").fetch().then(print);

Subreddits #

Find subreddits:

reddit.newSubreddits().fetch().then(print);

reddit.popularSubreddits().fetch().then(print);

reddit.recommendedSubreddits(["dartlang", "reddit"]).fetch().then(print);

reddit.subredditsByTopic("programming").fetch().then(print);

Get information about a subreddit:

reddit.sub("dartlang").about().fetch().then(print);

Users #

Get information about a user:

reddit.user("sroose").about().fetch().then(print);

Get listings from users:

reddit.user("sroose").comments("month").listen(print);

reddit.user("sroose").submitted("week").sort("hot").listen(print);

Get a list of multi's from a user:

reddit.user("sroose").multis().fetch().then(print);

Changelog #

0.5.0 #

  • Remove json_object dependency and use Map instead.
  • Remove mirrors dependency
  • Add ResultListing.results getter for the underlying stream.

0.4.0 (2017-01-22) #

  • Fix subreddit.search() to restrict results to the subreddit

0.3.3 (2015-12-18) #

  • Upgraded dependencies to latest versions
  • Adapted to breaking changes in oauth2 v1.0.0
  • Added better support for multireddits

0.3.2 (2015-04-03) #

  • Updated RedditUri with official regex and extra test
  • Updated README
  • Constant constructor for Fullname
  • Improved exception handling in Query

0.3.1 (2015-04-02) #

  • Added FullnameType enum
  • Added Fullname.id and Fullname.type getters
  • Bugfix in FilterableQuery.filter

0.3.0 (2015-04-01) #

  • Added full OAuth2 support (app-only and user)
  • Changed auth interface
  • Expose RedditUri and Fullname classes

0.2.0 (2015-04-01) #

  • Added App-only OAuth support, inspired by raw.js

0.1.0 (2015-03-31) #

  • Initial version, inspired by reddit.js

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  reddit: ^0.6.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:reddit/reddit.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
57
Health:
Code health derived from static analysis. [more]
78
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
60
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Sep 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:reddit/reddit.dart.

Health suggestions

Fix lib/src/user.dart. (-6.31 points)

Analysis of lib/src/user.dart reported 13 hints, including:

line 3 col 1: Prefer using /// for doc comments.

line 13 col 16: Don't type annotate initializing formals.

line 13 col 37: Don't type annotate initializing formals.

line 23 col 3: Prefer using /// for doc comments.

line 35 col 3: Prefer using /// for doc comments.

Fix lib/src/reddit.dart. (-5.84 points)

Analysis of lib/src/reddit.dart reported 12 hints, including:

line 18 col 10: Don't type annotate initializing formals.

line 34 col 3: Prefer using /// for doc comments.

line 39 col 3: Prefer using /// for doc comments.

line 44 col 3: Prefer using /// for doc comments.

line 51 col 3: Prefer using /// for doc comments.

Fix lib/src/query.dart. (-3.45 points)

Analysis of lib/src/query.dart reported 7 hints, including:

line 9 col 11: Don't type annotate initializing formals.

line 9 col 32: Don't type annotate initializing formals.

line 9 col 54: Don't type annotate initializing formals.

line 11 col 3: Prefer using /// for doc comments.

line 52 col 8: Don't type annotate initializing formals.

Fix additional 6 files with analysis or formatting issues. (-8.96 points)

Additional issues in the following files:

  • lib/src/subreddit.dart (5 hints)
  • lib/src/listing.dart (4 hints)
  • lib/src/listings.dart (4 hints)
  • lib/src/fullname.dart (2 hints)
  • lib/src/multireddit.dart (2 hints)
  • lib/src/reddit_uri.dart (1 hint)

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (http).

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and reddit.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
http >=0.11.1+3 <0.12.0 0.11.3+17 0.12.0+2
logging >=0.11.0 <0.12.0 0.11.3+2
oauth2 >=1.0.1 <2.0.0 1.3.0
Transitive dependencies
async 2.3.0
charcode 1.1.2
collection 1.14.12
http_parser 3.1.3
meta 1.1.7
path 1.6.4
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
Dev dependencies
test ^1.5.1+1