Skip to content

Commit

Permalink
Implemented stream option
Browse files Browse the repository at this point in the history
Implemented stream option that prints post's titles as soon as someone posts a new post in the subreddit
  • Loading branch information
dfireBird committed Jan 5, 2020
1 parent 2c01756 commit 7581b6b
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,27 @@ node index.js [options] [command]
scrappdit [options] [command]
```

Use `u` flag to get the top post titles of the user.
Use `u` or `user` command to get the top post titles of the user.

```bash
node index.js u <username>
scrappdit u <username>
```

Use `s` flag to get the hot post titles of the subreddit.
Use `s` or `subreddit` command to get the hot post titles of the subreddit.

```bash
node index.js s <subredditname>
scrappdit s <subredditname>
```

Use `t` or `stream` command to get post titles from the subreddit and it constantly prints new post's titles as soons the someone posts it in subreddit.

```bash
node index.js t <subredditname>
scrappdit s <subredditname>
```

Use `-c, --count <count>` option to get a specified number of post titles. The default count is 25.

```bash
Expand All @@ -54,6 +61,7 @@ List of commands avaiable:

- `user | u <username>` Get post titles from a user.
- `subreddit | s <subredditname>` Get post titles from a subreddit.
- `stream | t <subredditname>` Stream post titles from a subreddit.

## Contributing

Expand Down
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const Reddit = require('./src/Reddit');
const program = require('commander');
const livestream = require('./src/livestream');

program
.version('1.0.0')
Expand Down Expand Up @@ -36,6 +37,15 @@ program
}
});

program
.command('stream <subredditname>')
.alias('t')
.description('Streams the post titles from a specified subreddit')
.action((subredditname) => {
console.log('\x1b[34m', 'Ctrl-C to quit');
livestream(subredditname);
});

program.parse(process.argv);

if(program.count >= 99) {
Expand Down
50 changes: 49 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrapddit",
"version": "1.1.0",
"version": "1.2.0",
"description": "A app that retrieves posts from a subreddit or a reddit user.",
"main": "index.js",
"bin": "index.js",
Expand All @@ -24,6 +24,7 @@
},
"dependencies": {
"commander": "^4.0.1",
"snoostorm": "^1.3.0",
"snoowrap": "^1.20.1"
},
"devDependencies": {
Expand Down
13 changes: 13 additions & 0 deletions src/livestream.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Reddit = require('./Reddit');
const {SubmissionStream} = require('snoostorm');

const livestream = (name) => {
const submissions = new SubmissionStream(Reddit, {subreddit: name, pollTime: 1000});
submissions.on('item', (submission) => {
console.log('\x1b[32m%s\x1b[0m', submission.title);
});
}

livestream('memes');

module.exports = livestream;

0 comments on commit 7581b6b

Please sign in to comment.