-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspotian.cpp
More file actions
581 lines (507 loc) · 15.6 KB
/
Copy pathspotian.cpp
File metadata and controls
581 lines (507 loc) · 15.6 KB
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
/*
* TODO:
* - Playlist besser einbinden
* - Next soll TrackId kennen -> Play Status lässt sich erkennen
* - Check ob lieder vorhanden allein anhand von Playlist
*/
#include "tools.h"
#include "Timeout.h"
#include "Metadata.h"
#include "Recorder.h"
#include "Player.h"
#include "Playlist.h"
#include "SpotifyController.h"
#include <fstream>
void resetCache() {
deleteFolder(Variables::instance().getHomeDir() + "/.cache/spotify");
deleteFolder(Variables::instance().getHomeDir() + "/.config/spotify");
}
int checkError() {
//Moves content of logfile to final Logfile
//Returns errorcode
bool success;
std::string content = readFile(Variables::instance().getTempLogFile(), &success) + "\n";
if (!success) {
return 0;
}
//Only Work if File has content, otherwise no error!
if (content.length() > 1) {
//Move content to our
int lines = strfind(content, "\n");
std::string head = execToString(
"head -n " + numToString(lines) + " " + Variables::instance().getTempLogFile());
std::string tail = execToString(
"tail -n +" + numToString(lines) + " " + Variables::instance().getTempLogFile());
writeToFile(head, Variables::instance().getLogFile(), true);
writeToFile(tail, Variables::instance().getTempLogFile(), false);
//Do the real check
if (std::string::npos
!= content.find("Locale could not be found for en-US"))
return 1; //Kein Plan!
if (std::string::npos != content.find("Connection error: 4"))
return 2;
if (std::string::npos != content.find("Connection error: 117"))
return 2; //Kein Internet! Auch im Betrieb interessant!
if (std::string::npos != content.find("Connection error: 410"))
return 3; //Benutzername oder Passwort Falsch!
if (std::string::npos != content.find("file not found in archive:"))
return 4; //failed loading skin!
//if (std::string::npos
// != content.find(
// "Uncaught unknown: (bridge message: 'user_metadata', args: [\"spotify:user:@\"])"))
// return 5; //failed loading skin!
if (std::string::npos != content.find("Failed to launch child process"))
return 5; //???
if (std::string::npos != content.find("Cannot communicate with zygote"))
return 6; //???
if (std::string::npos
!= content.find("Deadlock detected (Thread: gui)"))
return 7; //???
if (std::string::npos != content.find("Fatal deadlock detected"))
return 8; //
if (std::string::npos
!= content.find(
"cannot open Variables::instance().getDisplay()"))
return 9; //
}
return 0;
}
bool checkLoaded(std::string log) {
std::string line;
std::ifstream myfile(log);
int process = 0;
bool created = false;
if (myfile.is_open()) {
while (std::getline(myfile, line)) {
if (line.find("playlist_be_pl4_context.cpp:71") < line.size()) {
process++;
created = true;
//std::cout << line << '\n';
}
if (line.find("playlist_be_pl4_context.cpp:202") < line.size()) {
process--;
//std::cout << line << '\n';
}
}
myfile.close();
return (created && process == 0);
}
//std::cout << "Unable to open file";
logline("Unable to open file", true);
return false;
}
int login() {
int windowWidth = 904;
int windowHeight = 668;
resetCache();
SpotifyController::instance().startSpotify();
while (execToString(
"wmctrl -lG 2>/dev/null | grep " + numToString(windowWidth)
+ " | grep " + numToString(windowHeight)) == "") {
int err = checkError();
if (err > 0) {
return err;
}
if (!waitLoad(MAXLOADLOADING, 0.5, Variables::instance().getLoginLoggingActive(), WAITLOADINGTIMEOUT, 0,
"Waiting for LoginWindow"))
return 1002;
}
//Move Window
logline("Move Window", Variables::instance().getLoginLoggingActive());
//while (execToString("wmctrl -lG | grep \"0 320 500 Spotian spotify\"") == "") {
while (execToString(
"wmctrl -lG 2>/dev/null | grep \"16 " + numToString(windowWidth)
+ " " + numToString(windowHeight) + "\" | grep Spotify")
== "") {
logline("Check for Error", Variables::instance().getLoginLoggingActive());
int err = checkError();
if (err > 0) {
return err;
}
execToString(
"wmctrl -r spotify -e 0,0,0," + numToString(windowWidth) + ","
+ numToString(windowHeight));
if (!waitLoad(MAXLOADLOADING, 0.5, Variables::instance().getLoginLoggingActive(), WAITLOADINGTIMEOUT, 0))
return 1003;
}
//Enter Userdata, and Login
logline("Enter Userdata, and Login", Variables::instance().getLoginLoggingActive());
float time = 0.5;
execToString("xte 'mousemove 470 290'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1004;
execToString("xte 'mouseclick 1'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1005;
execToString("xte 'str " + Variables::instance().getUser() + "'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1006;
execToString("xte 'mousemove 470 340'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1007;
execToString("xte 'mouseclick 1'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1008;
execToString("xte 'str " + Variables::instance().getPass() + "'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1009;
execToString("xte 'mousemove 470 430'");
if (!waitLoad(MAXLOADLOADING, time, false, WAITLOADINGTIMEOUT, 0))
return 1010;
execToString("xte 'mouseclick 1'");
//Wait till initialisation is done
logline("Check if Playlists are Loaded", Variables::instance().getLoginLoggingActive());
waitLoad(MAXLOADLOADING, 1.0, true, WAITLOADTIMEOUTPAUSED, 0,
"Loaded Window");
int j = 0;
while (!checkLoaded(Variables::instance().getTempLogFile()) && j < 10) {
sleep(1);
j++;
}
execToString("wmctrl -r Spotify -b toggle,maximized_vert,maximized_horz");
int err = checkError();
if (err > 0) {
return err;
}
if (!Playlist::instance().openPlaylist(
Variables::instance().getTestPlaylist()))
return 1011;
if (!Player::instance().pause())
return 1012;
float cpuload;
float maxload = 1.0;
float avgLoadTime = 0.5;
do {
int err = checkError();
if (err > 0) {
logline("error! " + numToString(err), Variables::instance().getLoginLoggingActive());
return err;
}
cpuload = avgCPULoad(avgLoadTime);
logline("CPU-Load: " + numToString(cpuload), Variables::instance().getLoginLoggingActive());
} while (cpuload > maxload);
// //No local library
// execToString("xte 'keydown Control_R' 'key p' 'keyup Control_R'");
// sleep(1);
// execToString("xte 'mousemove 237 624' 'mouseclick 1'");
// sleep(1);
// execToString("xte 'mousemove 34 103' 'mouseclick 1'");
// sleep(2);
//Check data
if (atoi(
execToString(
"cat " + Variables::instance().getHomeDir()
+ "/.config/spotify/prefs | grep autologin | wc -l").c_str())
< 3) {
logline(
execToString(
"cat " + Variables::instance().getHomeDir()
+ "/.config/spotify/prefs | grep autologin | wc -l"),
Variables::instance().getLoginLoggingActive());
return 1001;
}
//Add audio gap between songs
logline("Add audio gap between songs", Variables::instance().getLoginLoggingActive());
writeToFile("audio.crossfade_v2=false",
".config/spotify/lazybean93-user/prefs", false);
logline("Login finished", true);
return 0;
}
void doLogin() {
Variables::instance().prefix.push_back("Login");
int err = 0;
do {
err = login();
if (err > 0)
logline("Login Failed, errcode: " + numToString(err), true);
} while (err > 0);
Variables::instance().prefix.pop_back();
}
int init() {
//Only needed to press play and record if 0 will be returned
//Doing Soundcheck
Variables::instance().prefix.push_back("Soundcheck");
bool err;
do {
err = false;
logline("Soundcheck Failed! Try again", true);
SpotifyController::instance().startSpotify();
logline("Spotify Started", true);
Timeout startTimeout(60);
while (!checkLoaded(Variables::instance().getTempLogFile()) && startTimeout.inTime())
sleep(0.1);
if (!startTimeout.inTime())
return 2014;
execToString(
"wmctrl -r Spotify -b toggle,maximized_vert,maximized_horz");
if (Playlist::instance().playPlaylist(Variables::instance().getTestPlaylist()) != 0)
err = true;
if (!Player::instance().pause())
return 2012;
if (!waitLoad(MAXLOADLOADING, 1.5, true, WAITLOADINGTIMEOUT, 0))
return 2010;
if (!Player::instance().play())
return 2013;
sleep(10);
} while (err || !Recorder::instance().testPlaying());
Variables::instance().prefix.pop_back();
//Set Playlist
Variables::instance().prefix.push_back("Set Playlist");
Metadata m1;
if (Playlist::instance().playPlaylist(Variables::instance().getUri()) != 0) {
return 2001;
}
//Go to beginning of first Track
if (!Player::instance().pauseAtStart()) {
return 2002;
}
//Read Playlist
Playlist::instance().readPlaylist();
//Create Folder
Playlist::instance().createFolder();
Variables::instance().prefix.pop_back();
return 0;
}
void doInit() {
Variables::instance().prefix.push_back("Init\t");
while (init() > 0)
Variables::instance().prefix.pop_back();
Variables::instance().prefix.pop_back();
}
int checkCorrectSong(Metadata m1, int num) {
Playlist& playlist = Playlist::instance();
if (m1 != playlist.at(num - 1)) {
logline("Metadata does not match Playlist", true);
logline("Metadata:", true);
m1.print();
logline("Playlist:", true);
playlist.at(num - 1).print();
logline("Check if song should be next song", true);
if (m1 == playlist.at(num - 2)) {
if (playlist.nextSong() != 0)
return 4010;
playlist.decrementTrack();
m1 = Metadata();
if (m1 != playlist.at(num - 1))
return 4011;
if (!Player::instance().pauseAtStart())
return 4012;
}
m1 = Metadata();
if (m1 != playlist.at(num - 1))
return 5001;
}
return 0;
}
int moveToSong(int num) {
Playlist& playlist = Playlist::instance();
for (int i = 0; i < playlist.size(); i++) {
if (Metadata() == playlist.at(i)) {
logline("Spotify is at Song " + numToString(i), true);
logline("Spotian is at Song " + numToString(num - 1), true);
if (num - i > 1)
playlist.seek(num);
break;
}
}
return 0;
}
std::string findSong(Metadata m1) {
std::vector<std::string> files = split(
execToString(
"find * -type f | fgrep \"" + m1.getFileName(0).substr(7)
+ "\""), "\n");
for (int i = 0; i < (int) files.size(); i++) {
std::vector<std::string> tmp_filename_vector = split(files.at(i), "/");
std::string tmp_filename = "";
for (int j = 0; j < (int) tmp_filename_vector.size() - 1; j++) {
tmp_filename += tmp_filename_vector.at(j) + "/";
}
tmp_filename +=
tmp_filename_vector.at(tmp_filename_vector.size() - 1).substr(0,
7);
std::string cmd = "id3tool \"" + tmp_filename + "\"* | grep "
+ m1.getUrl();
std::string result = execToString(cmd);
//logline(result, true);
if (result != "")
return tmp_filename;
}
return "";
}
int copySong(Metadata m, int num) {
Playlist& playlist = Playlist::instance();
std::string copy = findSong(m);
if (copy != "") {
logline("Copy from old Recordings", true);
std::string cmd = "cp \"" + string_replace(copy, "\"", "\\\"")
+ "\"* \"" + Variables::instance().getDataDir() + "/"
+ m.getUrlMP3() + "\"";
execToString(cmd);
} else
return 1;
return 0;
}
void printCurrentSong(int track, int size, Metadata song) {
std::string num_str = numToString(track);
while (num_str.size() < numToString(size).size())
num_str = "0" + num_str;
logline(num_str + "/" + numToString(size) + " " + song.getFileName(track),
true);
}
void setSymlink(Metadata m, int num) {
execToString(
"cd " + Variables::instance().getDataDir() + "; ln -sf ../data/"
+ m.getUrlMP3() + " \"../" + Playlist::instance().getName()
+ "/" + m.getFileName(num) + "\"");
}
int recordSong(int num) {
//State: Spotify is Started, Playlist is Loaded, track is not Playing
Player& player = Player::instance();
Recorder& recorder = Recorder::instance();
Playlist& playlist = Playlist::instance();
//Echo current Song
printCurrentSong(num, playlist.size(), playlist.at(num - 1));
if (checkCorrectSong(Metadata(), num) > 0)
return 5002;
//Search for Song
if (!is_file_exist(
Variables::instance().getDataDir() + "/"
+ playlist.at(num - 1).getUrlMP3())) {
int copy = copySong(playlist.at(num - 1), num);
if (copy == 1) {
//Compare metadata to Playlist
Metadata m1;
if (!player.pauseAtStart())
return 5010;
//Start Recording
int errRecording = recorder.recordSong(m1, num);
if (errRecording > 999)
return errRecording;
bool ad = false;
if (errRecording == 1)
ad = true;
//Track has been recorded. Invisible ad has been played.
//Next Track, or visible ad is playing
Metadata m2;
if (m2.isAd()) {
logline("Got more ads", true);
Timeout adTimeout(ADTIMEOUT);
while (m2.isAd()) {
if (!adTimeout.inTime())
return 5004;
sleep(2);
m2 = Metadata();
}
logline("Got beyond ad", true);
}
//Next Track is playing, check for metadata
if (m2.getError())
return 5005;
//Next Track is not playing after Advertisement
if (ad && !player.isPlaying()) {
logline("Had advertisement, and now... something went wrong",
true);
if (m2 != playlist.at(0)) {
//What the hell is happening here????
sleep(5);
if (!player.isPlaying()) {
player.play();
sleep(5);
if (player.isPlaying()) {
m2 = Metadata();
m2.print();
int correctSongErr = checkCorrectSong(m2, num + 1);
if (correctSongErr > 0)
return correctSongErr;
} else
return 5006;
}
}
}
//Check Song, eventually short it
if (!recorder.checkOverrun())
return 5007; //check for overrun
logline("Check Song", true);
recorder.resetSongErrors(2);
bool check = recorder.checkSong(
Variables::instance().getDataDir() + "/" + m1.getUrlMP3(),
true);
if (!check) {
logline("Check Failed, record again!", true);
return 5008;
}
logline("Finished Recording", true);
if (player.isPlaying()) {
playlist.incrementTrack();
}
}
} else {
int err = playlist.nextSong();
if (err > 0)
return err;
}
setSymlink(playlist.at(num - 1), num);
return 0;
}
void doRecord() {
Variables::instance().prefix.push_back("Record");
Playlist& playlist = Playlist::instance();
//Track is from init Playlist -> Playlist is not available
if (Metadata().getUrl() != Variables::instance().getTestTrack()) {
int i = 1;
do {
int err = recordSong(i);
if (err > 0) {
deleteFile(
Variables::instance().getDataDir() + "/"
+ playlist.at(i).getUrlMP3());
logline("Recording Failed, errcode: " + numToString(err), true);
doInit();
i = 1;
} else
i++;
} while (!playlist.isFinished());
} else {
logline("Playlist is not available", true);
}
Variables::instance().prefix.pop_back();
}
bool doLoad(int argc, char* argv[]) {
if (argc == 2) {
//Return Playlistname
std::cout << Playlist::instance().playlistNameUri(argv[1], false) << std::endl;
return false;
} else {
//Normal Programm
Variables& var = Variables::instance();
var.prefix.push_back("Load\t");
deleteFile("commands.txt");
logline(
"Compiled on " + std::string(__DATE__) + " at "
+ std::string(__TIME__), true);
//Some Variables
var.setUser(argv[1]);
var.setPass(argv[2]);
var.setUri(argv[3]);
var.prefix.pop_back();
return true;
}
}
int main(int argc, char* argv[]) {
Timeout t0(0);
if (!doLoad(argc, argv))
return false;
doLogin();
std::string timelogin = t0.getFormatTime();
Timeout t1(0);
doInit();
std::string timeinit = t1.getFormatTime();
Timeout t2(0);
doRecord();
std::string timerecord = t2.getFormatTime();
std::string timetotal = t0.getFormatTime();
logline(
"Elapsed time: " + timetotal + " | " + timelogin + " | " + timeinit
+ " | " + timerecord, true);
}