# reddit-crawler

> crawl all submissions in a subreddit

Latest version **0.0.1** (published 2017-12-05) · MIT license · 0 weekly downloads

## Install

```sh
npm install reddit-crawler
pnpm add reddit-crawler
yarn add reddit-crawler
bun add reddit-crawler
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.1 |
| Published | 2017-12-05 |
| First published | 2017-12-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | danneu |
| Maintainers | danneu |
| Keywords | reddit, crawler |

## Links

- npm: https://www.npmjs.com/package/reddit-crawler
- Repository: https://github.com/danneu/node-reddit-crawler
- Homepage: https://github.com/danneu/node-reddit-crawler#readme
- Issues: https://github.com/danneu/node-reddit-crawler/issues
- npm.io page: https://npm.io/package/reddit-crawler

## Dependencies (3)

- [debug](https://npm.io/package/debug.md) ^3.1.0
- [node-fetch](https://npm.io/package/node-fetch.md) ^1.7.3
- [better-assert](https://npm.io/package/better-assert.md) ^1.0.2

## Recent versions

- 0.0.1 (latest) — 2017-12-05

## README

# reddit-crawler

    npm install reddit-crawler

Iterate over all submissions in a subreddit.

* Uses Reddit's CloudSearch API.
* Auto-renews OAuth access token as it crawls.

## Usage

Until Node gets native async iterators, the crawler approximates one with its
method `next(): Promise<array | null>`.

If falsey, then the crawler is done crawling the subreddit.

The array of submissions may be empty. The crawler will expand its search
interval until it finds results, attempting to hover around 50-99 results per
request.

```javascript
const makeCrawler = require('reddit-crawler')

const creds = {
    username: 'foo',
    password: 'secret',
    appId: 'xxx',
    appSecret: 'yyy',
}

async function work() {
    const crawler = makeCrawler('webdev', {
        creds,
        userAgent: 'my-crawler:0.0.1 (by /u/foo)',
    })

    while (true) {
        const submissions = await crawler.next()

        if (!submissions) {
            console.log('end of subreddit')
            break
        }

        for (const sub of submissions) {
            await processSubmission(sub)
        }
    }
}

function processSubmission(sub) {
    console.log(`title: "${sub.title}"`)
}

work().catch(console.error)
```

Credentials are for a [Reddit app](https://www.reddit.com/prefs/apps) and a user
that owns it. By giving the crawler your creds, it can renew its access-token as
it crawls.

The access-token expires in one hour, but large subreddits take a while to crawl
(respecting Reddit's rate-limit).

Reddit's API requires a user-agent: https://github.com/reddit/reddit/wiki/API.

    <platform>:<app ID>:<version string> (by /u/<reddit username>)

### Options

```javascript
const Duration = require('reddit-crawler/duration')

const crawler = makeCrawler('webdev', {
    // Required
    creds,
    userAgent: 'my-crawler:0.0.1 (by /u/foo)',
    // Optional (here are the defaults)
    initInterval: Duration.ofMinutes(15),
    minInterval: Duration.ofMinutes(10),
    maxInterval: Duration.ofDays(365),
    initMax: new Date(),
})
```

* `initInterval`: the crawler starts off requesting submissions created within
  this span of time.
* `minInterval`/`maxInterval`: the crawler shrinks/grows its interval to hover
  around 50-99 results per request, never exceeding min nor max.
* `maxInterval` also tells the crawler when to give up: if the crawler has grown
  to its max interval yet it still does not find any results, then it assumes
  that there are no more submissions.
* `initMax`: the crawler starts at initMax date and crawls backwards into the
  past. useful when you want to resume progress without re-crawling the top N
  submissions.

## Notes

* Reddit asks that you hit its API no more than once per second. The crawler has
  a rudimentary, built-in sleep after each cloud-search request.
* Set `DEBUG=reddit-crawler` to see debug logging.

---
_Source: https://npm.io/package/reddit-crawler · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
