-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (190 loc) · 7.7 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//#region Check are required packages installed
try{
require('readline-sync')
require('util')
require('fs')
require('path')
require('tidal-api-wrapper')
}catch(e){
return console.log(`\n You don't have required packages! \n\n Use "npm i" to install them! \n\n`)
}
//#endregion
const readline = require('readline-sync')
const util = require('util')
const fs = require('fs')
const path = require('path')
const Tidal = require('tidal-api-wrapper')
const tidal = new Tidal()
var loginFromCfg;
var passwordFromCfg;
var backupFromCfg;
checkIsCfg()
var login = loginFromCfg || readline.question("Tidal login: ")
var password = passwordFromCfg || readline.question("Tidal password: ", {hideEchoBack: true})
console.log(`\nLogging in...`)
// Main function
tidal.login(login, password).then(user =>{
console.log(`Loading your playlists...`)
tidal.getPlaylists().then(playlists =>{
if(!backupFromCfg){
console.log(`Here are your playlists and their UUIDs: `)
playlists.forEach(playlist => {
console.log(` - ${playlist.title}: (${playlist.uuid})`)
});
}else console.log(`Creating backup of: ${backupFromCfg}\n`)
const uuid = backupFromCfg || readline.question("Copy UUID of your playlist (without brackets) and paste it here, or if you want to backup all type all, or favorites: ", {defaultInput: "all"})
if(uuid == "all") backupAllPlaylists(playlists)
else if(uuid == "favorites") backupFavorites()
else (backupPlaylist(uuid))
}).catch(e =>{
saveLog(`Error while loading list of playlists. \nMore info: ${e}`)
console.log(`Error while loading list of playlists. \nMore info: ${e}`)
readline.keyInPause("\nProgram ended...")
})
}).catch(e =>{
saveLog(`Error. Bad login, password or you have problem with network. \nMore info: ${e}`)
console.log(`\nError. Bad login, password or you have problem with network. \nMore info: ${e}`)
readline.keyInPause("\nProgram ended...")
})
//Backup all playlists and favorites
async function backupAllPlaylists(playlists){
var list = [] //List to backup
//Fetch songs from playlists
console.log("\nFetching songs from your playlists...")
for await(var playlist of playlists){
var songsList = [];
await tidal.getPlaylistTracks(playlist.uuid).then(songs =>{
songs.forEach(song => {
songsList.push(song.title+" - "+song.artist.name)
});
var title = playlist.title
list.push({[title]: songsList})
}).catch(e =>{
saveLog(`Error while fetching songs. \nMore info: ${e}`)
console.log(`Error while fetching songs. \nMore info: ${e}`)
})
}
//Fetch songs from favorites
console.log("Fetching songs from your favorites...")
await tidal.getFavoriteTracks().then(favorites =>{
var songsList = [];
favorites.forEach(song => {
songsList.push(song.title+" - "+song.artist.name)
});
var title = "Favorites"
list.push({[title]: songsList})
}).catch(e => {
saveLog(`Error while fetching favorites \nMore info: ${e}`)
console.log(`\nError while fetching favorites \nMore info: ${e}`)
return readline.question("\n Press Enter to end...")
})
//Save file with both (favorites & playlists)
saveBackup(list)
}
function backupPlaylist(uuid){
console.log("\nFetching songs from your playlist...")
var playlist;
tidal.getPlaylist(uuid).then(_playlist =>{
playlist = _playlist
var songsList = [];
tidal.getPlaylistTracks(uuid).then(songs =>{
songs.forEach(song => {
songsList.push(song.title+" - "+song.artist.name)
});
var title = playlist.title
var list = {
[title]: songsList
}
saveBackup(list)
}).catch(e =>{
saveLog(`Error while fetching songs. \nMore info: ${e}`)
console.log(`Error while fetching songs. \nMore info: ${e}`)
})
}).catch(e => {
saveLog(`Error while loading playlist \nMore info: ${e} \nAre you sure you entered UUID correctly and you have internet connection?\nCan't find playlist: ${uuid}`)
console.log(`\nError while loading playlist \nMore info: ${e} \nAre you sure you entered UUID correctly and you have internet connection?`)
return readline.keyInPause("\nProgram ended...")
})
}
function backupFavorites(){
console.log("\nFetching songs from your favorites...")
tidal.getFavoriteTracks().then(favorites =>{
var songsList = [];
favorites.forEach(song => {
songsList.push(song.title+" - "+song.artist.name)
});
var title = "Favorites"
var list = {
[title]: songsList
}
saveBackup(list)
}).catch(e => {
saveLog(`Error while fetching favorites \nMore info: ${e}`)
console.log(`\nError while fetching favorites \nMore info: ${e}`)
return readline.keyInPause("\nProgram ended...")
})
}
function checkIsCfg(){
try{
var cfg = fs.readFileSync("./cfg.json")
cfg = JSON.parse(cfg)
loginFromCfg = cfg.login
passwordFromCfg = cfg.password
backupFromCfg = cfg.backup
if(cfg.login) console.log(`Logging in as ${cfg.login}...`)
if(cfg.password) console.log('Using password from cfg.json')
}
catch{}
}
function saveBackup(list){
console.log("Saving backup...")
try{
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var seconds = dateObj.getSeconds();
var minutes = dateObj.getMinutes();
var hour = dateObj.getHours();
//If < 10 - add "0" before. E.g 10:3 -> 10:03
if(month<10) month = "0"+month
if(hour<10) hour = "0"+hour
if(minutes<10) minutes = "0"+minutes
if(seconds<10) seconds = "0"+seconds
var newDate = `${year}.${month}.${day}_${hour};${minutes};${seconds}`
list.unshift({"accountLogin": login, "createdAt": newDate}) // add this on top of the file
list = JSON.stringify(list, null, 2)
var saveIn = path.join(`./TidalBackup_${newDate}__${login}.txt`)
fs.writeFileSync(saveIn, list, 'utf8')
console.log(`Success! File saved with name: TidalBackup_${newDate}__${login}`)
if(!loginFromCfg || !passwordFromCfg || !backupFromCfg)return readline.keyInPause("\nProgram ended...")
}catch(e){
saveLog(`Error while saving file. \nMore info: ${e}`)
console.log(`Error while saving file. \nMore info: ${e}`)
return readline.keyInPause("\nProgram ended...")
}
}
function saveLog(_log){
try{
//Get time
var dateObj = new Date();
var month = dateObj.getUTCMonth() + 1; //months from 1-12
var day = dateObj.getUTCDate();
var year = dateObj.getUTCFullYear();
var seconds = dateObj.getSeconds();
var minutes = dateObj.getMinutes();
var hour = dateObj.getHours();
//If < 10 - add "0" before. E.g 10:3 -> 10:03
if(month<10) month = "0"+month
if(hour<10) hour = "0"+hour
if(minutes<10) minutes = "0"+minutes
if(seconds<10) seconds = "0"+seconds
var newDate = `[${year}.${month}.${day} ${hour}:${minutes}:${seconds}] - `
//save log
_log = newDate + _log + "\r\n\r\n"
var saveIn = path.join(`./errorLog.txt`)
fs.appendFileSync(saveIn, _log, 'utf8')
}catch(e){
console.log(`Error while saving error log. \nMore info: ${e}`)
}
}