This repository has been archived by the owner on Jun 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync-playlists.js
139 lines (115 loc) · 4.62 KB
/
sync-playlists.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict';
const os = require('os');
const fs = require('fs');
const Promise = require("bluebird");
const rimraf = require('rimraf');
const config = require('./config.json');
const downloadFile = require('./infrastructure/download-helper');
const YandexMusicAPIManager = require('./yandex.api/yandex.music.api');
const yandexMusicAPIManager = new YandexMusicAPIManager();
console.log('[starting]');
console.log(`[loading playlists] [user: ${config.username}]`);
clearPlaylistForNewTracks()
.then(loadUserPlaylists)
.then(processPlayLists)
.then(processPlayListWithTracks)
.then(donwloadTracks)
.then(onSuccess)
.catch(onError);
// functions
function clearPlaylistForNewTracks() {
return new Promise((resolve, reject) => {
try {
var playlistForNewTracks = getPlaylistNameForNewFiles();
if (playlistForNewTracks) {
fs.exists(playlistForNewTracks, exists => {
if (exists) {
// remove non-empty directory
rimraf(playlistForNewTracks, () => {
resolve(true);
});
}
else {
resolve(true);
}
});
}
else {
resolve(true);
}
} catch (error) {
console.log(`[clearplaylistfornewtracks] [warn] [${error}]`);
resolve(false);// resolve in any case
}
});
}
function loadUserPlaylists(response) {
return yandexMusicAPIManager.getUserPlaylists(config.username);
}
function processPlayLists(playlists) {
return Promise.map(playlists
.filter(playlist => playlist.trackCount > 0), playlist => {
console.log(`[loading playlist tracks] [playlist: ${playlist.title}]`);
return yandexMusicAPIManager.getUserPlaylistTracks(config.username, playlist.kind);
}, { concurrency: config.concurrency });
}
function processPlayListWithTracks(playlistsWithTracks) {
let trackInfoItems = [];
let playlistForNewTracks = getPlaylistNameForNewFiles();
let isDownloadRequired = false;
playlistsWithTracks.forEach(element => {
let directory = `${config.destinationFolder}/${element.title}`;
if (!fs.existsSync(directory)) {
fs.mkdirSync(directory);
}
element.tracks
.forEach(track => {
var fileName = `${track.artists[0].name} - ${track.title}.mp3`.replace(/[?~^:*<>=_|\\/]/gi, '');
var fileLocation = `${directory}/${fileName}`;
var copyLocation = playlistForNewTracks ? `${playlistForNewTracks}/${fileName}` : undefined;
if (!fs.existsSync(fileLocation)) {
trackInfoItems.push({
playlist: element.title,
fileLocation: fileLocation,
copyLocation: copyLocation,
storageDir: track.storageDir
});
isDownloadRequired = true;
}
});
});
if (isDownloadRequired && playlistForNewTracks) {
if (!fs.existsSync(playlistForNewTracks)) {
fs.mkdirSync(playlistForNewTracks);
}
}
return Promise.map(trackInfoItems, trackInfo => {
return yandexMusicAPIManager.modifyTrackWithDonwloadUrl(trackInfo);
}, { concurrency: config.concurrency });
}
// function loadingDownloadURLs(trackInfoItems) {
// return Promise.map(trackInfoItems, trackInfo => {
// return yandexMusicAPIManager.modifyTrackWithDonwloadUrl(trackInfo);
// }, { concurrency: config.concurrency });
// }
function donwloadTracks(downloadRequest) {
console.log(`[loading tracks] [count: ${downloadRequest.length}]`);
return Promise.map(downloadRequest, request => {
return downloadFile(request.fileLocation, request.copyLocation, request.downloadUrl)
}, { concurrency: config.concurrency });
}
function getPlaylistNameForNewFiles() {
if (config.newPlaylistName) {
return `${config.destinationFolder}/${config.newPlaylistName}`;
}
}
function onSuccess(response) {
let successCount = response.filter(c => { return c }).length;
let errorCount = response.filter(c => { return !c }).length;
console.log(`----------------------------------------------------------------\r\n[finished] [success: ${successCount}] [error: ${errorCount}]`);
process.exit();
}
function onError(error) {
console.log(`----------------------------------------------------------------\r\n[finished] [error] [${error}]`);
process.exit();
}