-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
69 lines (57 loc) · 1.92 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env node
/*eslint-env node*/
/* eslint-disable no-console */
'use strict';
const axios = require('axios');
const cheerio = require('cheerio');
const inquirer = require('inquirer');
const Table = require('cli-table');
const colors = require('colors');
const baseUrl = 'http://www.liburnasional.com/';
axios.get(baseUrl).then(response => {
var $ = cheerio.load(response.data);
var sources = $('.dropdown-menu').eq(0).children().children();
var years = [];
sources.each(function(x, y) {
years.push({name: y.children[0].data, link: y.attribs.href});
});
// value : "Liburan Nasional 2012"
return inquirer.prompt([{
type : 'list',
name : 'years',
message : 'Pilih Tahun: ',
choices : years
}]);
}).catch(() => {
console.error(colors.red('\n Please check your internet connection.\n'));
})
.then(answer => {
// get year from answer
var year = answer.years.split(' ');
return year[year.length - 1];
}).then(year => {
let subUrl = 'http://www.liburnasional.com/kalender-';
let url = subUrl+year+'/';
// get data u need
axios.get(url).then(res => {
var $ = cheerio.load(res.data);
var holidays = [];
$('.libnas-calendar-holiday-title').each(function(x, y) {
holidays.push({
day : y.children[1].children[0].data,
date : y.children[2].children[0].data,
title : y.children[0].children[0].children[0].children[0].data
});
});
return holidays;
}).then(holidays => {
var table = new Table({
head: ['Tanggal', 'Hari', 'Acara']
});
// push value to table
holidays.forEach(x=> {
table.push([x.date, x.day, x.title]);
});
console.log(colors.cyan(table.toString()));
});
});