-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·80 lines (70 loc) · 1.83 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
70
71
72
73
74
75
76
77
78
79
80
const XLSX = require('xlsx');
const axios = require('axios');
const Promise = require('bluebird');
const fs = require('fs');
const json2xls = require('json2xls');
const winston = require('winston');
const program = require('commander');
let apiKey;
let filename;
const converted = [];
const toJson = (workbook) => {
const result = {};
workbook.SheetNames.forEach((sheetName) => {
const roa = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName]);
if (roa.length > 0) {
result[sheetName] = roa;
}
});
return result;
};
const getShortURL = longUrl => axios.post(`https://www.googleapis.com/urlshortener/v1/url?key=${apiKey}`, { longUrl })
.then((res) => {
if (res.data) {
return res.data.id;
}
return 'COULD NOT SHORTEN';
})
.catch((err) => {
winston.error(err);
});
const createExcel = (jsonArray) => {
const xls = json2xls(jsonArray);
fs.writeFileSync('output.xlsx', xls, 'binary');
};
function run() {
const workbook = XLSX.readFile(filename);
const jsonWorkbook = toJson(workbook).Sheet1;
const urls = jsonWorkbook.map(entry => entry.URL);
return Promise.try(() => urls)
.mapSeries((url) => {
winston.info(`Converting ${url}`);
return Promise.all([url, Promise.delay(1500).then(() => getShortURL(url))]);
})
.map(([long, short]) => {
converted.push({
long,
short,
});
})
.then(() => {
createExcel(converted);
winston.info('Done!');
});
}
program
.usage(`
[options]
Converts long URLs to short URLs
`)
.option('-k, --API_KEY <n>', 'Google API KEY')
.option('-f, --filename <n>', 'Path to XLSX file to convert')
.parse(process.argv);
if (!program.API_KEY || !program.filename) {
program.help();
process.exit(1);
} else {
apiKey = program.API_KEY;
filename = program.filename;
run();
}