-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
59 lines (54 loc) · 1.59 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
// Dependencies
const setTitle = require('node-bash-title')
const scrapeIt = require("scrape-it")
const inquirer = require("inquirer")
const openurl = require('openurl')
const chalk = require('chalk')
// Start the code here
const dncli = () => {
setTitle('📰 dncli - CLI to browse designernews.co')
scrapeIt(
process.argv[2] ? `https://www.designernews.co/badges/${process.argv[2]}` : 'https://www.designernews.co'
, {
stories: {
listItem: '.montana-list-item',
data: {
id: {
attr: 'data-story-id'
},
upvotes: '.upvoted-number',
title: {
selector: '.montana-item-title',
attr: 'alt'
},
url: {
selector: '.montana-item-title',
attr: 'href'
},
timeAgo: '.list-story-time-ago',
from: '.montana-item-meta span a'
}
}
}).then(data => {
const storiesQuestions = {
type: 'list',
name: 'selectedStorie',
message: 'Select a story to read',
pageSize: 30,
choices: data.stories.map(item => {
return {
name: `${chalk.green('▴')} ${chalk.gray(`${ ('0' + Number(item.upvotes)).slice(-2)} |`)} ${ item.title.length > 80 ? item.title.substring(0, 80) + '...' : item.title} ${chalk.gray(`by ${item.from} (${item.timeAgo})`)}`,
value: item.id
}
})
}
inquirer.prompt([storiesQuestions]).then(function (answer) {
for (let i = 0; i < data.stories.length; i++) {
if (data.stories[i].id === answer.selectedStorie) {
openurl.open(data.stories[i].url.includes('http') ? data.stories[i].url : 'https://www.designernews.co' + data.stories[i].url)
}
}
})
})
}
exports.dncli = dncli