UNPKG

1.5 kBJavaScriptView Raw
1#!/usr/bin/env node
2var programm = require('commander');
3var ProgressBar = require('progress');
4var google = require('./lib/providers/google');
5var download = require('./lib/download');
6var iTunes = require('./lib/itunes');
7
8programm
9 .version(require('./package').version)
10 .usage('[options]')
11 .option('-p, --playlist [num]', 'Show playlist')
12 .option('-d, --download', 'Download songs and sync with iTunes')
13 .parse(process.argv);
14
15function downloadSongs(playlist) {
16 var playlistName = 'gm-' + playlist.name;
17 var bar = new ProgressBar('Downloading [:bar] :percent :etas', {
18 complete: '#',
19 total: playlist.songs.length,
20 width: 50
21 });
22 bar.tick(0);
23 iTunes.createPlaylist(playlistName);
24 download(playlist)
25 .on('item', function (filename) {
26 bar.tick();
27 iTunes.addFilenameToPlaylist(playlistName, filename);
28 })
29 .on('end', process.exit);
30}
31
32function printSongs(playlist) {
33 playlist.songs.forEach(function (item, index) {
34 console.log('%s) %s — %s', index, item.artist, item.title);
35 });
36}
37
38if (programm.playlist) {
39 google.getPlayListWithSongs(programm.playlist, function (err, playlist) {
40 if (err) {
41 return console.error(err);
42 }
43 if (programm.download) {
44 downloadSongs(playlist);
45 } else {
46 printSongs(playlist);
47 }
48 });
49} else {
50 google.getPlayLists(function (err, playlists) {
51 if (err) {
52 return console.error(err);
53 }
54 console.log('Your playlists:');
55 playlists.forEach(function (item, index) {
56 console.log('%s) %s', index, item.name);
57 });
58 });
59}
60