/
main.js
593 lines (536 loc) · 17.8 KB
/
main.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
Importer.loadQtBinding( "qt.core" );
Importer.loadQtBinding( "qt.gui" );
var configFileName = Amarok.Info.scriptPath() + "/usbexport.conf";
var artistMap = {};
var specialsList = [];
function mapArtist(artist)
{
for(var k in artistMap)
{
var v = artistMap[k];
if(artist.match(v))
return "REMAPPED:" + k;
}
return artist;
}
function isSpecialAlbum(artist, album)
{
var s = artist + " - " + album;
for(var i = 0; i < specialsList.length; ++i)
{
var v = specialsList[i];
if(s.match(v))
return true;
}
return false;
}
function loadConfig()
{
var s = new QSettings(configFileName, QSettings.NativeFormat);
var children;
s.beginGroup("artistRemap");
children = s.childKeys();
for(i = 0; i < children.length; ++i)
{
var k = children[i];
var v = s.value(k);
artistMap[k] = new RegExp("^(" + v + ")$", 'i');
}
s.endGroup();
s.beginGroup("specialAlbums");
children = s.childKeys();
for(i = 0; i < children.length; ++i)
{
var k = children[i];
var v = s.value(k);
specialsList.push(new RegExp("^(" + v + ")$", 'i'));
}
s.endGroup();
}
USBExportMainWindow.prototype = new QMainWindow();
USBExportMainWindow.prototype.executeSave = function(e)
{
Amarok.debug("Saving");
var s = new QSettings(configFileName, QSettings.NativeFormat);
s.setValue("main/OutputID3", (this.outputID3CheckBox.checked ? "yes" : "no"));
s.setValue("main/OutputTemp", this.outputTempLineEdit.text);
s.setValue("main/OutputCache", this.outputCacheLineEdit.text);
s.setValue("main/OutputDeviceDir", this.outputDeviceDirLineEdit.text);
s.setValue("main/OutputSubdirs", (this.outputSubdirsCheckBox.checked ? "yes" : "no"));
s.setValue("main/TotalDuration", this.totalDurationSpinBox.value);
s.setValue("main/Unrated", this.unratedSpinBox.value);
// selection:
// first, rating10 tracks >= 10
// then, rating9 tracks >= 9
// then, rating8 tracks >= 8
// ...
// values are percentages of remaining count
for(var i = 0; i < 10; ++i)
s.setValue("main/Rating" + i, this.ratingSpinBox[i].value);
Amarok.debug("Saved");
};
USBExportMainWindow.prototype.getListForExport = function(field)
{
var totalTimeRemaining = this.totalDurationSpinBox.value * 60 * 60 * 1000; // hours to millseconds
var finished = false;
var list = [];
var dupeskip = new Object();
var timeRemaining = 0;
var noDupeID = 0;
OUTER:
for(var i = 10; !finished && i >= -1; --i)
{
var timeChunk;
if(i >= 10)
timeChunk = totalTimeRemaining * this.unratedSpinBox.value / 100.0;
else if(i >= 0)
timeChunk = totalTimeRemaining * this.ratingSpinBox[i].value / 100.0;
else
timeChunk = totalTimeRemaining;
timeRemaining += timeChunk;
totalTimeRemaining -= timeChunk;
if(timeRemaining <= 0)
continue;
var maxRemaining = Math.ceil(timeRemaining / 30000); // 30 sec per track min
var sql;
if(i >= 10)
sql = "SELECT d.lastmountpoint, u.rpath, t.length, s.rating, a.name, alb.name, t.title FROM tracks t LEFT JOIN statistics s ON s.url = t.url LEFT JOIN artists a ON a.id = t.artist LEFT JOIN albums alb ON alb.id = t.album INNER JOIN urls u on u.id = t.url LEFT JOIN devices d ON d.id = u.deviceid WHERE s.rating IS NULL OR s.rating = 0 ORDER BY RAND() LIMIT " + Amarok.Collection.escape(maxRemaining) + ";";
else if(i >= 0)
sql = "SELECT d.lastmountpoint, u.rpath, t.length, s.rating, a.name, alb.name, t.title FROM tracks t LEFT JOIN statistics s ON s.url = t.url LEFT JOIN artists a ON a.id = t.artist LEFT JOIN albums alb ON alb.id = t.album INNER JOIN urls u on u.id = t.url LEFT JOIN devices d ON d.id = u.deviceid WHERE s.rating > " + Amarok.Collection.escape(i) + " ORDER BY RAND() LIMIT " + Amarok.Collection.escape(maxRemaining) + ";";
else
sql = "SELECT d.lastmountpoint, u.rpath, t.length, s.rating, alb.name, a.name, t.title FROM tracks t LEFT JOIN statistics s ON s.url = t.url LEFT JOIN artists a ON a.id = t.artist LEFT JOIN albums alb ON alb.id = t.album INNER JOIN urls u on u.id = t.url LEFT JOIN devices d ON d.id = u.deviceid ORDER BY RAND() LIMIT " + Amarok.Collection.escape(maxRemaining) + ";";
//Amarok.debug(sql);
var result = Amarok.Collection.query(sql);
//Amarok.debug(result.length);
for(var j = 0; j < result.length; j += 7)
{
var mountpoint = result[j];
var path = result[j+1];
var len = result[j+2];
var rating = result[j+3];
var artist = result[j+4];
var album = result[j+5];
var title = result[j+6];
if(mountpoint == null)
mountpoint = "/";
path = mountpoint + "/" + path;
if(artist == null || artist == "")
artist = "???";
var mappedArtist = mapArtist(artist);
if(isSpecialAlbum(artist, album))
mappedArtist += " - " + noDupeID++;
if(title == null || title == "")
{
title = "" + path;
title = title.replace(/^.*\//, "");
title = title.replace(/\.[^.]*$/, "");
}
if(rating != null && rating != "" && parseInt(rating) != 0)
rating = parseInt(rating);
else
rating = null;
var simplified = mappedArtist + " - " + title;
simplified = simplified.toLowerCase();
simplified = simplified.replace(/[^a-z0-9 ]/g, "");
if(dupeskip[simplified])
continue;
dupeskip[simplified] = true;
if(len > totalTimeRemaining + timeRemaining)
{
// if we exceeded the TOTAL time, bail out
// so we won't prefer the tiny tracks next round
break OUTER;
}
if(len > timeRemaining)
break;
timeRemaining -= len;
list.push({ path: path, len: len, rating: rating, artist: artist, title: title, rand: Math.random()});
}
}
function compareItem(a, b)
{
if(a[0] < b[0])
return -1;
if(a[0] < b[0])
return 1;
return 0;
}
if(field != null)
{
function compareItem(a, b)
{
if(a[field] < b[field])
return -1;
if(a[field] > b[field])
return 1;
return 0;
}
list.sort(compareItem);
}
return list;
};
USBExportMainWindow.prototype.executeExport = function()
{
var list = this.getListForExport("path");
try
{
var outdir = this.outputDeviceDirLineEdit.text;
var f_ratings = new QFile(outdir + "/ratings.txt");
f_ratings.open(new QIODevice.OpenMode(QIODevice.ReadOnly));
if(f_ratings.isReadable())
{
var allRatings = new Object();
var allRatingsKeys = [];
try
{
var ratings = new QTextStream(f_ratings);
for(;;)
{
var path = ratings.readLine();
var rating = ratings.readLine();
if(!rating)
break;
rating = Math.round(parseFloat(rating) * 2);
if(allRatings[path] == null)
allRatingsKeys.push(path);
allRatings[path] = rating;
}
}
finally
{
f_ratings.close();
}
if(allRatingsKeys.length)
{
var str = "";
allRatingsKeys.sort();
for(var i = 0; i < allRatingsKeys.length; ++i)
str += allRatingsKeys[i] + ": " + (allRatings[allRatingsKeys[i]] / 2.0) + "\n";
str = "Update the following ratings?\n\n" + str;
var result = (Amarok.alert(str, "questionYesNo") == 3); // yes == 3, no == 4
if(result)
{
var errors = "";
for(var e in allRatings)
{
var m = e.match(/^(.*?)\/(\.\/.*?)$/);
if(!m)
continue;
var dev = m[1];
var path = m[2];
if(dev == "")
dev = "/";
var urlidsql = "SELECT u.id FROM urls u LEFT JOIN devices d ON d.id = u.deviceid WHERE u.rpath='" + Amarok.Collection.escape(path) + "' AND IFNULL(d.lastmountpoint, '/') = '" + Amarok.Collection.escape(dev) + "'";
var urliddata = Amarok.Collection.query(urlidsql);
if(urliddata.length < 1)
{
errors += urlidsql + "\n";
continue;
}
var urlid = urliddata[0];
var sql = "INSERT INTO statistics SET url=" + urlid + ", rating=" + allRatings[e] + " ON DUPLICATE KEY UPDATE rating=" + allRatings[e];
var changed = Amarok.Collection.query(sql);
if(!changed)
{
errors += sql + "\n";
continue;
}
}
if(errors)
Amarok.alert(errors);
else
f_ratings.remove();
}
}
}
var tempdir = this.outputTempLineEdit.text;
new QDir("/").mkpath(tempdir);
var mp3dir = tempdir + "/mp3";
new QDir("/").mkpath(mp3dir);
var cachedir = this.outputCacheLineEdit.text + (this.outputID3CheckBox.checked ? "/id3" : "/plain");
new QDir("/").mkpath(cachedir);
var m3u = tempdir + "/index.m3u";
var f = new QFile(m3u);
f.open(new QIODevice.OpenMode(QIODevice.WriteOnly));
if(f.isWritable())
{
try
{
var out = new QTextStream(f);
out.writeString("#EXTM3U\n");
for(var i = 0; i < list.length; ++i)
{
var ratingval;
if(list[i].rating == null)
ratingval = "unrated";
else
ratingval = (list[i].rating / 2);
out.writeString(
"#EXTINF:" + Math.floor(list[i].len/1000) + "," + list[i].artist + " - (" + ratingval + ") " + list[i].title + "\n" +
list[i].path + "\n");
}
out.flush();
f.flush();
}
finally
{
f.close();
}
// now run the converter
var threads = 8; // FIXME make configurable
QProcess.startDetached("xterm", [
"-hold",
"-e",
Amarok.Info.scriptPath() + "/usbexport.pl",
m3u,
mp3dir,
cachedir,
"160",
"compression_level=2,global_quality=3", // --vbr-new -V 3 -q 2
outdir,
(this.outputID3CheckBox.checked ? "0" : "1"),
(this.outputSubdirsCheckBox.checked ? "1" : "0"),
threads
]);
// then rsync it to the USB device
}
else
Amarok.debug("outfile " + m3u + " is not writable");
}
catch(e)
{
Amarok.debug("" + e);
}
this.close(true);
};
USBExportMainWindow.prototype.executeExportToPlaylist = function()
{
var list = this.getListForExport("rand");
try
{
Amarok.Playlist.clearPlaylist();
for(var i = 0; i < list.length; ++i)
{
Amarok.Playlist.addMedia(QUrl.fromLocalFile(list[i].path));
}
}
catch(e)
{
Amarok.debug("" + e);
}
};
USBExportMainWindow.prototype.selectOutputTemp = function()
{
var out = QFileDialog.getExistingDirectory(null, "Select temporary directory (WILL GET DELETED)", this.outputTempLineEdit.text);
if(out != "")
this.outputTempLineEdit.text = out;
};
USBExportMainWindow.prototype.selectOutputCache = function()
{
var out = QFileDialog.getExistingDirectory(null, "Select cache directory", this.outputCacheLineEdit.text);
if(out != "")
this.outputCacheLineEdit.text = out;
};
USBExportMainWindow.prototype.selectOutputDeviceDir = function()
{
var out = QFileDialog.getExistingDirectory(null, "Select output directory", this.outputDeviceDirLineEdit.text);
if(out != "")
this.outputDeviceDirLineEdit.text = out;
};
function USBExportMainWindow()
{
Amarok.debug("open main window");
QMainWindow.call(this, null);
var s = new QSettings(configFileName, QSettings.NativeFormat);
var mainWidget = new QWidget(this);
this.exportButton = new QPushButton(mainWidget);
this.exportButton.clicked.connect(this, this.executeExport);
this.exportButton.text = "Export";
this.saveButton = new QPushButton(mainWidget);
this.saveButton.clicked.connect(this, this.executeSave);
this.saveButton.text = "Save";
this.exportToPlaylistButton = new QPushButton(mainWidget);
this.exportToPlaylistButton.clicked.connect(this, this.executeExportToPlaylist);
this.exportToPlaylistButton.text = "To Playlist";
this.parametersBox = new QGroupBox("Settings", mainWidget);
this.buttonsBox = new QWidget(mainWidget);
//var layout = new QVBoxLayout(mainWidget);
var layout = new QVBoxLayout();
layout.addWidget(this.parametersBox, 0, 0);
layout.addWidget(this.buttonsBox, 0, 0);
//var buttonsLayout = new QHBoxLayout(mainWidget);
var buttonsLayout = new QHBoxLayout();
buttonsLayout.addWidget(this.exportButton, 0, 0);
buttonsLayout.addWidget(this.saveButton, 0, 0);
buttonsLayout.addWidget(this.exportToPlaylistButton, 0, 0);
this.buttonsBox.setLayout(buttonsLayout);
//var groupLayout = new QGridLayout(mainWidget);
var groupLayout = new QGridLayout();
var row = 0;
this.outputTempLineEdit = new QLineEdit(this.parametersBox);
this.outputTempLineEdit.text = s.value("main/OutputTemp", "/tmp/usbexport");
var outputTempButton = new QPushButton("Temp directory...", this.parametersBox);
outputTempButton.clicked.connect(this, this.selectOutputTemp);
groupLayout.addWidget(outputTempButton, row, 0);
groupLayout.addWidget(this.outputTempLineEdit, row, 1);
++row;
this.outputCacheLineEdit = new QLineEdit(this.parametersBox);
this.outputCacheLineEdit.text = s.value("main/OutputCache", "/tmp2/CACHE/usbexport");
var outputCacheButton = new QPushButton("Cache directory...", this.parametersBox);
outputCacheButton.clicked.connect(this, this.selectOutputCache);
groupLayout.addWidget(outputCacheButton, row, 0);
groupLayout.addWidget(this.outputCacheLineEdit, row, 1);
++row;
this.outputDeviceDirLineEdit = new QLineEdit(this.parametersBox);
this.outputDeviceDirLineEdit.text = s.value("main/OutputDeviceDir", "/media/sdb1/mp3");
var outputDeviceDirButton = new QPushButton("Output directory...", this.parametersBox);
outputDeviceDirButton.clicked.connect(this, this.selectOutputDeviceDir);
groupLayout.addWidget(outputDeviceDirButton, row, 0);
groupLayout.addWidget(this.outputDeviceDirLineEdit, row, 1);
++row;
this.outputID3CheckBox = new QCheckBox("Output ID3 tags", this.parametersBox);
this.outputID3CheckBox.checked = (s.value("main/OutputID3", "yes") == "yes");
groupLayout.addWidget(this.outputID3CheckBox, row, 0, 1, 2);
++row;
this.outputSubdirsCheckBox = new QCheckBox("Output subdirectories", this.parametersBox);
this.outputSubdirsCheckBox.checked = (s.value("main/OutputSubdirs", "yes") == "yes");
groupLayout.addWidget(this.outputSubdirsCheckBox, row, 0, 1, 2);
++row;
this.totalDurationSpinBox = new QSpinBox(this.parametersBox);
this.totalDurationSpinBox.setRange(0, 168);
this.totalDurationSpinBox.value = s.value("main/TotalDuration");
var totalDurationLbl = new QLabel("Duration (h)", this.parametersBox);
totalDurationLbl.setBuddy(this.totalDurationSpinBox);
groupLayout.addWidget(totalDurationLbl, row, 0);
groupLayout.addWidget(this.totalDurationSpinBox, row, 1);
++row;
this.unratedSpinBox = new QSpinBox(this.parametersBox);
this.unratedSpinBox.setRange(0, 100);
this.unratedSpinBox.value = s.value("main/Unrated");
var unratedLbl = new QLabel("Unrated", this.parametersBox);
unratedLbl.setBuddy(this.unratedSpinBox);
groupLayout.addWidget(unratedLbl, row, 0);
groupLayout.addWidget(this.unratedSpinBox, row, 1);
++row;
this.ratingSpinBox = [];
for(var i = 9; i >= 0; --i)
{
this.ratingSpinBox[i] = new QSpinBox(this.parametersBox);
this.ratingSpinBox[i].setRange(0, 100);
this.ratingSpinBox[i].value = s.value("main/Rating" + i);
var lbl = new QLabel("Rating >= " + (i+1)/2 + " stars", this.parametersBox);
lbl.setBuddy(this.ratingSpinBox[i]);
groupLayout.addWidget(lbl, row, 0);
groupLayout.addWidget(this.ratingSpinBox[i], row, 1);
++row;
}
this.parametersBox.setLayout(groupLayout);
mainWidget.setLayout(layout);
this.setCentralWidget(mainWidget);
//this.resize(600, 400);
this.show();
}
function USBExportCallback() {
loadConfig();
var mainWindow = new USBExportMainWindow();
}
function FixRatingsCallback() {
loadConfig();
var sql = "UPDATE statistics SET rating=NULL WHERE rating=0;";
Amarok.Collection.query(sql);
var sql = "SELECT d.lastmountpoint, u.rpath, s.rating, a.name, alb.name, t.title, u.id FROM tracks t LEFT JOIN statistics s ON s.url = t.url LEFT JOIN artists a ON a.id = t.artist LEFT JOIN albums alb ON alb.id = t.album INNER JOIN urls u on u.id = t.url LEFT JOIN devices d ON d.id = u.deviceid;";
var result = Amarok.Collection.query(sql);
var dupeskip = {};
var noDupeID = 0;
for(var j = 0; j < result.length; j += 7)
{
var mountpoint = result[j];
var path = result[j+1];
var rating = result[j+2];
var artist = result[j+3];
var album = result[j+4];
var title = result[j+5];
var urlid = result[j+6];
if(mountpoint == null)
mountpoint = "/";
path = mountpoint + "/" + path;
if(artist == null || artist == "")
artist = "???";
var mappedArtist = mapArtist(artist);
if(isSpecialAlbum(artist, album))
mappedArtist += " - " + noDupeID++;
if(title == null || title == "")
{
title = "" + path;
title = title.replace(/^.*\//, "");
title = title.replace(/\.[^.]*$/, "");
}
var simplified = mappedArtist + " - " + title;
simplified = simplified.toLowerCase();
simplified = simplified.replace(/[^a-z0-9 ]/g, "");
if(dupeskip[simplified] == null)
dupeskip[simplified] = [];
var thisone = { "path": path, "rating": rating, "artist": artist, "title": title, "urlid": urlid };
dupeskip[simplified].push(thisone);
}
Amarok.Playlist.clearPlaylist();
for(var i in dupeskip)
{
var d = dupeskip[i];
if(d.length < 2)
continue;
var minRating = null;
var maxRating = null;
var nullCount = 0;
for(var j = 0; j < d.length; ++j)
{
if(d[j].rating == "")
{
++nullCount;
continue;
}
if(minRating == null)
{
minRating = maxRating = d[j].rating;
continue;
}
if(d[j].rating < minRating)
minRating = d[j].rating;
if(d[j].rating > maxRating)
maxRating = d[j].rating;
}
if(minRating == null)
continue;
if(minRating == maxRating)
{
for(var j = 0; j < d.length; ++j)
if(d[j].rating == "")
{
Amarok.debug("Autofixing missing ratign for " + d[j].path);
var sql = "INSERT INTO statistics SET url=" + d[j].urlid + ", rating=" + minRating + " ON DUPLICATE KEY UPDATE rating=" + minRating;
Amarok.Collection.query(sql);
}
continue;
}
for(var j = 0; j < d.length; ++j)
{
//Amarok.alert(d[j].path);
Amarok.Playlist.addMedia(QUrl.fromLocalFile(d[j].path));
}
}
}
if(Amarok.Window.addToolsMenu("script_usb_export", "USB Export..."))
{
var sync_button = Amarok.Window.ToolsMenu.script_usb_export;
sync_button['triggered()'].connect(USBExportCallback);
}
else
Amarok.debug("USB Export menu already exists");
if(Amarok.Window.addToolsMenu("script_usb_export_fixratings", "Fix Ratings..."))
{
var sync_button = Amarok.Window.ToolsMenu.script_usb_export_fixratings;
sync_button['triggered()'].connect(FixRatingsCallback);
}
else
Amarok.debug("USB Export menu already exists");
//USBExportCallback();