Giter Club home page Giter Club logo

reddit's Introduction

reddit travis npm downloads

Simple Reddit API client

Install

npm install reddit

This package is used by BitMidi to post MIDI files to the /r/BitMidi subreddit.

Features

  • Lightweight – no unecessary dependencies, easy-to-audit source code
  • Modern API – uses async/await and promises
  • Powered by the official Reddit API

Usage

First, create a Reddit App.

  • Click "Create app"
  • For simple scripts, you can select a type of "script".
  • You can enter anything in the "about URL" and "redirect URL" fields.
  • Note your app ID (appears below the app name) and your app secret.

Now, let's take an action on Reddit:

const Reddit = require('reddit')

const reddit = new Reddit({
  username: 'TODO',
  password: 'TODO',
  appId: 'TODO',
  appSecret: 'TODO',
  userAgent: 'MyApp/1.0.0 (http://example.com)'
})

// Submit a link to the /r/BitMidi subreddit
const res = await reddit.post('/api/submit', {
  sr: 'WeAreTheMusicMakers',
  kind: 'link',
  resubmit: true,
  title: 'BitMidi – 100K+ Free MIDI files',
  url: 'https://bitmidi.com'
})

console.log(res)
// Prints:
// {
//   json: {
//     errors: [],
//     data: {
//       url: 'https://www.reddit.com/r/WeAreTheMusicMakers/comments/96ak55/',
//       drafts_count: 0,
//       id: '96ak55',
//       name: 't3_96ak55'
//     }
//   }
// }

API

reddit = new Reddit(opts)

Create a new Reddit API client.

You must provide opts, an object with the following required properties:

  • username - Username of the reddit user
  • password - Password of the reddit user
  • appId - Reddit App ID
  • appSecret - Reddit App Secret

The following opts properties are optional:

  • opts.userAgent - String to use as the User-Agent header in http requests

reddit.get(url, [data])

Invoke the Reddit API using the GET HTTP method.

The url parameter is used to specify which API to invoke. For example, /api/submit or /api/vote. A complete listing of possible APIs is available in the Reddit API documentation.

The optional data parameter is used to specify relevant data for the selected API. The necessary data varies depending on which API is being used. Again, see the Reddit API documentation.

reddit.post(url, [data])

Invoke the Reddit API using the POST HTTP method.

reddit.patch(url, [data])

Invoke the Reddit API using the PATCH HTTP method.

reddit.put(url, [data])

Invoke the Reddit API using the PUT HTTP method.

reddit.delete(url, [data])

Invoke the Reddit API using the DELETE HTTP method.

License

The Prosperity Public License. Copyright (c) Feross Aboukhadijeh.

To obtain a commercial license, visit feross.org/commercial.

reddit's People

Contributors

feross avatar reni-min avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

reddit's Issues

API error: undefined

Hi
I tried to fetch some data using two methods: async await and reddit.get().then().catch().

For example:
reddit.get('/api/trending_subreddits',{}).then((res)=> {console.log(res)}).catch((err)=>{console.log(err)});

Both caused this error:
Error: API error: undefined. Status code: 404
at get.concat (C:\Users\hp\Desktop\API_TEST\node_modules\reddit\index.js:152:13)
at concat (C:\Users\hp\Desktop\API_TEST\node_modules\simple-get\index.js:89:7)
at IncomingMessage. (C:\Users\hp\Desktop\API_TEST\node_modules\simple-concat\index.js:7:13)
at Object.onceWrapper (events.js:273:13)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)

I have another question regarding credentials: Does appId correspond to the string generated next to ? symbol in reddit api preferences and can I use your module for both web apps and scripts .

Thank you.

post of the feed instead of community?

I want to post in my profile feed added below. I am not sure how to do that. Can you please explain?

async function post(){
    // Submit a link to the /r/BitMidi subreddit
    const res = await reddit.post('/api/submit', {
    sr: 'cryptoanalyticatech',
    kind: 'link',
    resubmit: true,
    title: 'This is the test',
    url: 'www.google.com'
    })
    
    console.log(res);
}

Should errors getting token print error_description as well?

Currently when an error occurs while getting a token, it prints body.error. Should it also print body.error_description? For example, I got this error because I forgot to switch the toggle to "script": {"error_description":"Only script apps may use password auth","error":"unauthorized_client"}. Because it only printed body.error, I spent several minutes trying to figure out why I was getting "unauthorized_client", when I would have immediately figured it out with "Only script apps may use password auth", which I didn't find until I modified the log in this package to print the whole error object. I'm not sure if all types of errors provide a description, though. Thoughts?

Return response headers or obey rate limit

I'm interested in properly handling the X-Ratelimit-Used, X-Ratelimit-Remaining, and X-Ratelimit-Reset headers.

I'm not sure what would be the best way to expose them in the returned object.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.