-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
180 lines (147 loc) · 5.7 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "iostream"
#include <QDir>
#include <QTimer>
#include <QWebElement>
#include <QWebFrame>
#include <QVariant>
#include <QStandardPaths>
#include <QProcess>
#include <QNetworkProxy>
#include "util/Configuration.hh"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
currentSong(nullptr)
{
//Make sure spotify will run properly
QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalStorageEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::LocalStorageDatabaseEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::PluginsEnabled, true);
QWebSettings::globalSettings()->setAttribute(QWebSettings::AcceleratedCompositingEnabled, false);
//All Traffic send over privoxy to get rid of ads
QNetworkProxy proxy;
proxy.setType(QNetworkProxy::HttpProxy);
proxy.setHostName("localhost");
proxy.setPort(8118);
QNetworkProxy::setApplicationProxy(proxy);
//Go to Spotify
ui->setupUi(this);
ui->webView->settings()->enablePersistentStorage(QDir::homePath());
ui->webView->setUrl(QUrl("https://play.spotify.com"));
//Setup Timer looking for songs to grab
checkInterval = new QTimer();
checkInterval->setInterval(100);
connect(checkInterval, SIGNAL(timeout()), this, SLOT(checkSong()));
//Setup Timer to fetch Album and Cover (both aren't updated immediately, so we need some delay)
albumDelay = new QTimer();
albumDelay->setInterval(2500);
albumDelay->setSingleShot(true);
connect(albumDelay, SIGNAL(timeout()), this, SLOT(readAlbum()));
setWindowTitle("Grabify");
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::stopRecording()
{
recording = false;
if (currentSong)
{
currentSong->stop();
delete currentSong;
currentSong = nullptr;
}
setWindowTitle("Grabify");
}
void MainWindow::startRecording(const QString &songTitle, const QString& songArtist)
{
recording = true;
currentSong = new Song();
currentSong->setTitle(songTitle);
currentSong->setArtist(songArtist);
currentSong->record(Configuration::the().get(Configuration::OUTPUT_PATTERN));
setWindowTitle(QString("Grabify - Recording ") + songTitle + " by " + songArtist);
}
QWebFrame *MainWindow::findPlayerFrame(QWebFrame *root)
{
for (QWebFrame *temp : root->childFrames())
{
//qDebug() << temp->requestedUrl();
if (temp->requestedUrl().toString().startsWith("https://play.spotify.com/apps/player"))
return temp;
}
return nullptr;
}
void MainWindow::readAlbum()
{
if (!currentSong) return;
QWebFrame *frame = ui->webView->page()->mainFrame();
QWebFrame *playerFrame = findPlayerFrame(frame);
QString albumName = frame->documentElement().evaluateJavaScript("window.frames[document.querySelector('#section-queue iframe').id].document.querySelector('.current .list-row-albums').innerText").toString();
if (albumName != "")
{
currentSong->setAlbum(albumName);
if (currentSong->needsCover())
{
QWebElement el = playerFrame->documentElement().findFirst(".sp-image-img");
QImage image(el.geometry().width(), el.geometry().height(), QImage::Format_RGB32);
QPainter painter(&image);
el.render(&painter);
painter.end();
currentSong->saveCover(image);
}
}
}
void MainWindow::checkSong()
{
checkInterval->stop();
QWebFrame *playerFrame = findPlayerFrame(ui->webView->page()->mainFrame());
// Only look for songs if the player is active
if (playerFrame)
{
//qDebug() << "Player frame found.";
QVariant songTitle = playerFrame->documentElement().evaluateJavaScript("document.getElementById('track-name').innerText");
//Panic! We missed the ending of a song
if (currentSong && (songTitle.toString() != currentSong->getTitle()))
{
stopRecording();
if (!automatic) return;
}
//Check, if a song is currently playing
if (songTitle.toString() != "") {
QVariant length = playerFrame->documentElement().evaluateJavaScript("document.getElementById('track-length').innerText");
QVariant progress = playerFrame->documentElement().evaluateJavaScript("document.getElementById('track-current').innerText");
//Song just started => begin recording
if (progress.toString() == "0:00" && !recording)
{
QVariant songArtist = playerFrame->documentElement().evaluateJavaScript("document.getElementById('track-artist').innerText");
QVariant songAlbum = playerFrame->documentElement().evaluateJavaScript("document.querySelector('#cover-art a').getAttribute('data-tooltip').replace(/ by .*?$/, '')");
startRecording(songTitle.toString(), songArtist.toString());
albumDelay->start();
//Song is ending => finish recording
} else if (progress.toString() == length.toString() && recording) {
stopRecording();
}
}
}
else
{
//qDebug() << "No Player frame found.";
}
//Keep timer running, if still recording or looking for songs
if (recording || automatic) checkInterval->start();
}
void MainWindow::on_actionRecord_automatically_triggered(bool checked)
{
automatic = checked;
if (checked) checkInterval->start(); else if (!recording) checkInterval->stop();
}
void MainWindow::on_actionConfiguration_triggered()
{
config.prepare();
config.show();
}