-
Notifications
You must be signed in to change notification settings - Fork 0
/
reddit.mjs
41 lines (39 loc) · 1.02 KB
/
reddit.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#! /usr/bin/env node
import open from 'open';
import fetch from 'node-fetch';
import yargs from 'yargs';
// const argv = yargs(process.argv).argv; This is equivalent to below line
const {argv} = yargs(process.argv) //Here by specifying {argv} we are telling to extract argv property of returned instance from yargs(process.argv)
// console.log(argv);
fetch('https://www.reddit.com/.json')
.then(res=>{
res.json()
.then(data=>{
const children = data.data.children;
const randomPost = children[Math.floor(Math.random()*children.length)];
const link = `https://reddit.com${randomPost.data.permalink}`;
if(argv.print){
console.log(`
title: ${randomPost.data.title}
link: ${link}
`);
}
else{
console.log(`
title: ${randomPost.data.title}
link: ${link}
`);
open(link)
.then(()=>{
console.log('Link opened on Browser successfully');
}
);
}
})
.catch(err=>{
console.log(err);
});
})
.catch(err=>{
console.error(err);
});