-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
363 lines (253 loc) · 10.2 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
const chrome = require('puppeteer')
const jsonfile = require('jsonfile')
const readTextFile = require('read-text-file')
const arg = require("yargs").argv
const fs = require('fs')
const path = require('path')
const mv = require('mv')
const cliProgress = require("cli-progress")
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic)
const prompt = require('prompt')
let FileNames = jsonfile.readFileSync('./FileNames.json')
cli()
function getFileInArray(name) {
let file = readTextFile.readSync( name )
let arrayFile = file.split('\n')
return arrayFile
}
async function saveFileNamesJSON(json) {
let filePath = './FileNames.json'
try {
await jsonfile.writeFile( filePath, json, { spaces: 4 } )
console.log('FileNames.json successfully updated')
} catch (e) {
console.error('Error happened while updating FileNames.json\nHere is the error:\n\n', e)
}
}
function extractVideoURLsFromPlaylist() {
let urls = []
let div = document.querySelectorAll('#contents > ytd-playlist-video-renderer')
let hrefClass = '.yt-simple-endpoint.style-scope.ytd-playlist-video-renderer'
for (let d in div) {
div[ d ].querySelector && urls.push( div[ d ].querySelector(hrefClass).href )
}
return urls.join('\n')
}
function sleep(sec) {
return new Promise( res => {
progressBar.start(sec, 0)
let progressValue = 0
let incrementer = setInterval(() => {
progressBar.update( ++progressValue )
}, 1000)
setTimeout( () => {
clearInterval(incrementer)
progressBar.stop()
res()
}, sec * 1000 );
})
}
async function run(i) {
let options = {
width: 1600,
height: 1200
}
let q = `Press any key once download location is set in the browser`
const browser = await chrome.launch({
headless: false,
// devtools: true,
args: [`--window-size=${options.width},${options.height}`] // new option
});
const page = await browser.newPage()
await page.setViewport({
width: options.width,
height: options.height,
deviceScaleFactor: 1,
})
prompt.get([q], (e, result) => {
let res = result[q]
console.log('\n\n Main program starting . . .\n\n')
main()
})
async function main() {
/*
You can run it in a for loop but it is difficult to organize
the files into categories or playlists
This is why I am going to do one at a time
So afterwards I can organize it into appopriate folder
This would not be neccessary if there was a way to get access to downloaded
file
*/
if ( i >= 0 && typeof FileNames[ i ] === 'object' ) {
let name = `${ __dirname }/Playlists/${ FileNames[i].name }`
let fileArray = getFileInArray(name);
// let s = await downloadItFromUrl('https://www.youtube.com/watch?v=H9vevyszht4', page)
// console.log(JSON.stringify(s, null, 4), ' => Download Started')
// s = await downloadItFromUrl('https://www.youtube.com/watch?v=7YuKKZosPko', page)
// console.log(JSON.stringify(s, null, 4), ' => Download Started')
for (let url in fileArray) {
let name = FileNames[i].stats[url] && FileNames[i].stats[url].name
if ( name === undefined || name === "" ) {
let stats = await downloadItFromUrl( fileArray[ url ], page, url, fileArray.length )
FileNames[ i ].stats[ url ] = stats
FileNames[ i ].completed = parseInt( url ) + 1
FileNames[ i ].lastDone = fileArray[ url ]
} else console.log(`Did work (${url} of ${fileArray.length}): `, name)
}
FileNames[ i ].done = true
await saveFileNamesJSON(FileNames)
let q = `Press [ENTER] when all downloads are finished . . .`
prompt.get([q], async function (err, result) {
if (err) {
return onErr(err)
}
await moveEachFileInOwnFolder(`${__dirname}/Media/${FileNames[i].name}`, FileNames[i])
console.log('\n\nAll done.\nClosing browser . . .')
await browser.close()
return
})
function onErr(err) {
console.log(err)
return 1
}
} else {
console.log('You did not pass valid index for the file: FileNames.json\nTry again!')
await browser.close()
return
}
}
}
async function downloadItFromUrl( url, page, i, size ) {
let videoStats = {
name: "",
size: "",
length: "",
url
}
try {
await page.goto('https://ytmp3.eu/', {
timeout: (1 * 60) * 1000
})
await page.evaluate( downloadUrl => {
function walk(elm) {
var node;
// Handle child elements
for (node = elm.firstChild; node; node = node.nextSibling) {
if (node.nodeType === 1) { // 1 == Element
walk(node);
}
}
}
window.video_url.value = downloadUrl
window.submitButton.click()
}, url )
try {
await page.waitFor('.download3.cresponsive_result', { timeout: ( 4 * 60 ) * 1000 })
videoStats = await page.evaluate( (url) => {
return {
name: document.querySelector('.download3.cresponsive_result').textContent,
size: window.myImageId44.textContent.split('|')[1].trim(),
length: window.myImageId44.textContent.split('|')[2].trim().replace(')', ''),
url
}
}, url )
console.log(`Did work (${i} of ${size}): `, videoStats.name )
} catch (e) {
console.log(`Did NOT work (${i} of ${size}): `, url)
}
} catch (e) {
console.log(`Did NOT work (${i} of ${size}): `, url)
console.log(`https://ytmp3.eu/ is not responding. Waited for 1 minute.`)
}
return videoStats
}
function mkdir( dir ) {
if ( fs.existsSync( dir ) === false ) {
fs.mkdirSync( dir )
console.log('Folder created:', dir)
}
}
async function moveEachFileInOwnFolder(dir, obj){
let folderAcronym = nameToAcronym(obj.name)
await renameFiles(dir, folderAcronym)
// createFolderForEachFile(obj, dir)
}
function nameToAcronym(name) {
let acronym = ''
let splittedName = name.split(' ')
for ( let w in splittedName ) {
acronym += splittedName[w][0]
}
return acronym
}
async function renameFiles(dir, acronym) {
console.log('Renaming files with Folder acronym so it is easier to find . . . \n', dir)
try {
let files = fs.readdirSync(dir)
console.log('Files are:', JSON.stringify(files, null, 4) )
for (let f in files) {
if ( files[f].endsWith('.mp3') && files[f].startsWith(`(${acronym})`) === false ) {
let filePath = path.join(dir, files[f])
let acronimedName = acronym + ' '
acronimedName += files[f].replace(/[^\w\s\][^,]/gi, "")
acronimedName = acronimedName.replace('mp3', '')
acronimedName += '.mp3'
let folder = files[f].replace(/[^\w\s\][^,]/gi, "")
folder = folder.replace('mp3', '')
let newFilePath = path.join(dir, acronimedName)
try {
fs.renameSync(filePath, newFilePath)
console.log('Rename:', newFilePath)
mkdir( path.join(dir, folder) )
mv(
newFilePath,
path.join(dir, folder, acronimedName),
e => {
if (e) {
console.error('Moved: Error happened while moving after renaming\n', e)
} else console.log("Moved:", files[f]);
}
)
} catch (e) {
console.error('Rename: DID NOT WORK =>', newFilePath , '\nError is:\n', e)
}
} else console.log(`Rename: Folder or Already renamed => ${files[f]}`)
}
} catch (e) {
console.log('Error happened while trying to read files for renaming', e)
}
}
function createFolderForEachFile(obj, dir) {
console.log('Creating folders for each file\n\n')
let filePath = path.join(__dirname, 'Media', obj.name)
let files = fs.readdirSync(dir)
for ( let f in obj.stats ) {
let fsPath = path.join(filePath, obj.stats[f].name.replace(/[^\w\s\][^,]/gi, ''))
mkdir(fsPath)
}
}
function cli() {
if ( FileNames.length ) {
prompt.start()
let questions = [
`Enter a valid index between 0 - ${FileNames.length - 1} for FileNames.json`
]
prompt.get([questions[0]], function(err, result) {
if (err) {
return onErr(err)
}
let i = parseInt( result[ questions[0] ] )
if ( i >= 0 && i < FileNames.length ){
console.log( 'Playlist to cover:\n', JSON.stringify( FileNames[i], null, 4 ) )
mkdir( `${ __dirname }/Media/${ FileNames[i].name }` )
console.log('\n\nThis folder need to be set as download location:')
console.log('\n\n => ', `${ __dirname }/Media/${ FileNames[i].name }`)
run(i)
} else console.log('Wrong input. Exiting . . . ')
})
function onErr(err) {
console.log(err)
return 1
}
} else console.warn('FileNames.json is empty. Please first populate it.')
}