-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfetchReddit.js
48 lines (46 loc) · 1.5 KB
/
fetchReddit.js
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
42
43
44
45
46
47
48
const axios = require('axios');
const _ = require('lodash');
const ora = require('ora');
const t = require('./i18n');
const { RedditBaseUrl } = require('../common/const');
const chalk = require('chalk');
async function fetchReddit(sort = 'hot', topic = 'popular') {
const url = `${RedditBaseUrl}/r/${topic}/${sort}.json`;
const spinner = ora(t('spinner.load')).start();
try {
const { data } = await axios.get(url);
const items = _.get(data, 'data.children', []);
spinner.stop();
console.log(
chalk.cyan(`-----------------------------------------
🧬 ${t('reddit.title')}
-----------------------------------------
`),
);
items.forEach((item) => {
const {
data: { title, ups, selftext, num_comments, subreddit, permalink },
} = item;
console.log(t('reddit.postName'), ': ', chalk.green(title));
console.log(
`${t('reddit.comment')}: `,
chalk.yellow(num_comments),
' | ',
`${t('reddit.votes')}: `,
chalk.yellow(ups),
` | ${t('reddit.topic')}: `,
chalk.yellow(subreddit),
);
console.log(t('reddit.url'), ': ', chalk.dim(`${RedditBaseUrl}${permalink}`));
if (selftext !== '') {
console.log(t('reddit.content'), ': ', chalk.cyan(selftext));
}
console.log('----------------------------------------------');
});
spinner.stop();
} catch (error) {
console.log(error);
spinner.fail(t('spinner.fail'));
}
}
module.exports = fetchReddit;