scrobbler.cpp 9.13 KB
Newer Older
1 2
/**
 * Copyright (C) 2012 Martin Sandsmark <martin.sandsmark@kde.org>
3
 * Copyright (C) 2014 Arnold Dumas <contact@arnolddumas.fr>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program.  If not, see <http://www.gnu.org/licenses/>.
 */
17

18
#include "scrobbler.h"
19

20
#include <QCryptographicHash>
21
#include <QDir>
22 23 24
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QDomDocument>
25
#include <QByteArray>
Michael Pyne's avatar
Michael Pyne committed
26 27
#include <QUrl>
#include <QUrlQuery>
28 29 30 31

#include <kconfiggroup.h>
#include <KSharedConfig>

32 33
#include <memory>

34
#include "tag.h"
35
#include "juk.h"
36
#include "juk_debug.h"
37

38 39
Scrobbler::Scrobbler(QObject* parent)
    : QObject(parent)
40
    , m_networkAccessManager(new QNetworkAccessManager(this))
41
    , m_wallet(Scrobbler::openKWallet())
42
{
43
    QByteArray sessionKey;
44

45 46 47
    if (m_wallet) {
        m_wallet->readEntry("SessionKey", sessionKey);
    } else {
48
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
49 50
        sessionKey.append(config.readEntry("SessionKey", ""));
    }
51 52

    if(sessionKey.isEmpty())
53 54 55
        getAuthToken();
}

56
bool Scrobbler::isScrobblingEnabled() // static
57
{
58 59
    QString username, password;

60 61
    // checks without prompting to open the wallet
    if (Wallet::folderDoesNotExist(Wallet::LocalWallet(), "JuK")) {
62
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
63 64 65 66

        username = config.readEntry("Username", "");
        password = config.readEntry("Password", "");
    } else {
67
        auto wallet = Scrobbler::openKWallet();
68 69 70
        if (wallet) {
            QMap<QString, QString> scrobblingCredentials;
            wallet->readMap("Scrobbling", scrobblingCredentials);
71

72 73 74 75 76 77
            if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) {
                username = scrobblingCredentials["Username"];
                password = scrobblingCredentials["Password"];
            }
        }
    }
78 79 80 81

    return (!username.isEmpty() && !password.isEmpty());
}

82
std::unique_ptr<KWallet::Wallet> Scrobbler::openKWallet() // static
83
{
84
    using KWallet::Wallet;
85

86 87
    const QString walletFolderName(QStringLiteral("JuK"));
    const auto walletName = Wallet::LocalWallet();
88

89 90 91 92 93 94 95 96 97 98 99 100 101 102
    // checks without prompting to open the wallet
    if (Wallet::folderDoesNotExist(walletName, walletFolderName)) {
        return nullptr;
    }

    std::unique_ptr<Wallet> wallet(
        Wallet::openWallet(walletName, JuK::JuKInstance()->winId()));

    if(!wallet ||
       (!wallet->hasFolder(walletFolderName) &&
           !wallet->createFolder(walletFolderName)) ||
       !wallet->setFolder(walletFolderName))
    {
        return nullptr;
103 104 105 106 107
    }

    return wallet;
}

108 109
QByteArray Scrobbler::md5(QByteArray data)
{
110 111
    return QCryptographicHash::hash(data, QCryptographicHash::Md5)
        .toHex().rightJustified(32, '0').toLower();
112 113 114 115
}

void Scrobbler::sign(QMap< QString, QString >& params)
{
116 117
    params["api_key"] = "3e6ecbd7284883089e8f2b5b53b0aecd";

118
    QString s;
119 120 121
    QMapIterator<QString, QString> i(params);

    while(i.hasNext()) {
122 123 124
        i.next();
        s += i.key() + i.value();
    }
125

126
    s += "2cab3957b1f70d485e9815ac1ac94096"; //shared secret
127

128 129 130
    params["api_sig"] = md5(s.toUtf8());
}

131 132
void Scrobbler::getAuthToken(QString username, QString password)
{
Michael Pyne's avatar
Michael Pyne committed
133
    qCDebug(JUK_LOG) << "Getting new auth token for user:" << username;
134

135
    QByteArray authToken = md5((username + md5(password.toUtf8())).toUtf8());
136

137 138
    QMap<QString, QString> params;
    params["method"]    = "auth.getMobileSession";
139
    params["authToken"] = authToken;
140 141
    params["username"]  = username;

142
    QUrl url("http://ws.audioscrobbler.com/2.0/?");
143

144
    sign(params);
145

Michael Pyne's avatar
Michael Pyne committed
146
    QUrlQuery urlQuery;
147
    foreach(QString key, params.keys()) {
Michael Pyne's avatar
Michael Pyne committed
148
        urlQuery.addQueryItem(key, params[key]);
149 150
    }

Michael Pyne's avatar
Michael Pyne committed
151 152
    url.setQuery(urlQuery);

153
    QNetworkReply *reply = m_networkAccessManager->get(QNetworkRequest(url));
154 155 156
    connect(reply, SIGNAL(finished()), this, SLOT(handleAuthenticationReply()));
}

157
void Scrobbler::getAuthToken()
158
{
159 160 161 162 163 164 165 166
    QString username, password;

    if (m_wallet) {

        QMap<QString, QString> scrobblingCredentials;
        m_wallet->readMap("Scrobbling", scrobblingCredentials);

        if (scrobblingCredentials.contains("Username") && scrobblingCredentials.contains("Password")) {
167

168 169 170 171 172 173
            username = scrobblingCredentials["Username"];
            password = scrobblingCredentials["Password"];
        }

    } else {

174
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
175 176 177
        username = config.readEntry("Username", "");
        password = config.readEntry("Password", "");
    }
178 179

    if(username.isEmpty() || password.isEmpty())
180
        return;
181

182 183 184
    getAuthToken(username, password);
}

185 186 187
void Scrobbler::handleAuthenticationReply()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
188

Michael Pyne's avatar
Michael Pyne committed
189
    qCDebug(JUK_LOG) << "got authentication reply";
190
    if(reply->error() != QNetworkReply::NoError) {
191
        emit invalidAuth();
Michael Pyne's avatar
Michael Pyne committed
192
        qCWarning(JUK_LOG) << "Error while getting authentication reply" << reply->errorString();
193 194
        return;
    }
195

196 197 198
    QDomDocument doc;
    QByteArray data = reply->readAll();
    doc.setContent(data);
199 200 201 202 203 204

    QString sessionKey = doc.documentElement()
        .firstChildElement("session")
            .firstChildElement("key").text();

    if(sessionKey.isEmpty()) {
205
        emit invalidAuth();
Michael Pyne's avatar
Michael Pyne committed
206
        qCWarning(JUK_LOG) << "Unable to get session key" << data;
207 208
        return;
    }
209

210 211 212 213 214 215
    if (m_wallet) {

        m_wallet->writeEntry("SessionKey", sessionKey.toUtf8());

    } else {

216
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
217 218 219
        config.writeEntry("SessionKey", sessionKey);
    }

220
    emit validAuth();
221 222 223 224
}

void Scrobbler::nowPlaying(const FileHandle& file)
{
225
    QString sessionKey;
226

227 228 229 230 231 232 233 234
    if (m_wallet) {

        QByteArray sessionKeyByteArray;
        m_wallet->readEntry("SessionKey", sessionKeyByteArray);
        sessionKey = QString::fromLatin1(sessionKeyByteArray);

    } else {

235
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
236
        sessionKey = config.readEntry("SessionKey", "");
237
    }
238

239 240 241 242
    if (!m_file.isNull()) {
        scrobble(); // Update time-played info for last track
    }

243 244
    QMap<QString, QString> params;
    params["method"] = "track.updateNowPlaying";
245 246
    params["sk"]     = sessionKey;
    params["track"]  = file.tag()->title();
247
    params["artist"] = file.tag()->artist();
248
    params["album"]  = file.tag()->album();
249
    params["trackNumber"] = QString::number(file.tag()->track());
250 251
    params["duration"]    = QString::number(file.tag()->seconds());

252 253
    sign(params);
    post(params);
254

255
    m_file = file; // May be empty FileHandle
256
    m_playbackTimer = QDateTime::currentDateTime();
257 258 259 260
}

void Scrobbler::scrobble()
{
261 262 263
    QString sessionKey;

    if (m_wallet) {
264

265 266 267 268 269 270
        QByteArray sessionKeyByteArray;
        m_wallet->readEntry("SessionKey", sessionKeyByteArray);
        sessionKey = QString::fromLatin1(sessionKeyByteArray);

    } else {

271
        KConfigGroup config(KSharedConfig::openConfig(), "Scrobbling");
272 273
        sessionKey = config.readEntry("SessionKey", "");
    }
274 275

    if(sessionKey.isEmpty()) {
276 277 278
        getAuthToken();
        return;
    }
279

280 281 282 283 284 285 286
    int halfDuration = m_file.tag()->seconds() / 2;
    int timeElapsed = m_playbackTimer.secsTo(QDateTime::currentDateTime());

    if (timeElapsed < 30 || timeElapsed < halfDuration) {
        return; // API says not to scrobble if the user didn't play long enough
    }

Michael Pyne's avatar
Michael Pyne committed
287
    qCDebug(JUK_LOG) << "Scrobbling" << m_file.tag()->title();
288

289 290
    QMap<QString, QString> params;
    params["method"] = "track.scrobble";
291 292
    params["sk"]     = sessionKey;
    params["track"]  = m_file.tag()->title();
293
    params["artist"] = m_file.tag()->artist();
294
    params["album"]  = m_file.tag()->album();
295
    params["timestamp"]   = QString::number(m_playbackTimer.toTime_t());
296
    params["trackNumber"] = QString::number(m_file.tag()->track());
297 298
    params["duration"]    = QString::number(m_file.tag()->seconds());

299 300 301 302 303 304 305
    sign(params);
    post(params);
}

void Scrobbler::post(QMap<QString, QString> &params)
{
    QUrl url("http://ws.audioscrobbler.com/2.0/");
306

307 308
    QByteArray data;
    foreach(QString key, params.keys()) {
Shubham Chaudhary's avatar
Shubham Chaudhary committed
309
        data += QUrl::toPercentEncoding(key) + '=' + QUrl::toPercentEncoding(params[key]) + '&';
310 311 312 313
    }

    QNetworkRequest req(url);
    req.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
314
    QNetworkReply *reply = m_networkAccessManager->post(req, data);
315 316 317 318 319 320 321
    connect(reply, SIGNAL(finished()), this, SLOT(handleResults()));
}

void Scrobbler::handleResults()
{
    QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
    QByteArray data = reply->readAll();
322
    if(data.contains("code=\"9\"")) // We need a new token
323 324
        getAuthToken();
}