From 7164404b2e0d428b4f0083255d0ed140740b7146 Mon Sep 17 00:00:00 2001 From: trialuser02 Date: Wed, 23 Jan 2013 18:03:45 +0000 Subject: scrobbler: refactoring again git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@3187 90c681e8-e032-0410-971d-27865f9a5e38 --- src/plugins/General/scrobbler/CMakeLists.txt | 15 +- src/plugins/General/scrobbler/lastfmscrobbler.cpp | 464 ++++++++++++++++++ src/plugins/General/scrobbler/lastfmscrobbler.h | 85 ++++ src/plugins/General/scrobbler/librefmscrobbler.cpp | 357 ++++++++++++++ src/plugins/General/scrobbler/librefmscrobbler.h | 81 +++ src/plugins/General/scrobbler/scrobbler.cpp | 512 ------------------- src/plugins/General/scrobbler/scrobbler.h | 118 ----- src/plugins/General/scrobbler/scrobbler.pro | 22 +- src/plugins/General/scrobbler/scrobbler2.cpp | 541 --------------------- src/plugins/General/scrobbler/scrobbler2.h | 86 ---- src/plugins/General/scrobbler/scrobblercache.cpp | 187 +++++++ src/plugins/General/scrobbler/scrobblercache.h | 78 +++ src/plugins/General/scrobbler/scrobblerhandler.cpp | 13 +- 13 files changed, 1285 insertions(+), 1274 deletions(-) create mode 100644 src/plugins/General/scrobbler/lastfmscrobbler.cpp create mode 100644 src/plugins/General/scrobbler/lastfmscrobbler.h create mode 100644 src/plugins/General/scrobbler/librefmscrobbler.cpp create mode 100644 src/plugins/General/scrobbler/librefmscrobbler.h delete mode 100644 src/plugins/General/scrobbler/scrobbler.cpp delete mode 100644 src/plugins/General/scrobbler/scrobbler.h delete mode 100644 src/plugins/General/scrobbler/scrobbler2.cpp delete mode 100644 src/plugins/General/scrobbler/scrobbler2.h create mode 100644 src/plugins/General/scrobbler/scrobblercache.cpp create mode 100644 src/plugins/General/scrobbler/scrobblercache.h (limited to 'src/plugins/General') diff --git a/src/plugins/General/scrobbler/CMakeLists.txt b/src/plugins/General/scrobbler/CMakeLists.txt index f548508cd..f0b007741 100644 --- a/src/plugins/General/scrobbler/CMakeLists.txt +++ b/src/plugins/General/scrobbler/CMakeLists.txt @@ -30,18 +30,23 @@ link_directories(${CMAKE_CURRENT_BINARY_DIR}/../../../qmmp) SET(libscrobbler_SRCS settingsdialog.cpp - scrobbler.cpp scrobblerhandler.cpp scrobblerfactory.cpp - scrobbler2.cpp + scrobblercache.cpp + lastfmscrobbler.cpp + librefmscrobbler.cpp ) SET(libscrobbler_MOC_HDRS settingsdialog.h scrobblerfactory.h scrobblerhandler.h - scrobbler.h - scrobbler2.h + lastfmscrobbler.h + librefmscrobbler.h +) + +SET(libscrobbler_HDRS + scrobblercache.h ) SET(libscrobbler_RCCS translations/translations.qrc) @@ -63,7 +68,7 @@ QT4_WRAP_UI(libscrobbler_UIS_H ${libscrobbler_UIS}) include_directories(${CMAKE_CURRENT_BINARY_DIR}) ADD_LIBRARY(scrobbler MODULE ${libscrobbler_SRCS} ${libscrobbler_MOC_SRCS} ${libscrobbler_UIS_H} - ${libscrobbler_RCC_SRCS}) + ${libscrobbler_RCC_SRCS} ${libscrobbler_HDRS}) add_dependencies(scrobbler qmmpui) target_link_libraries(scrobbler ${QT_LIBRARIES} -lqmmpui -lqmmp) install(TARGETS scrobbler DESTINATION ${LIB_DIR}/qmmp/General) diff --git a/src/plugins/General/scrobbler/lastfmscrobbler.cpp b/src/plugins/General/scrobbler/lastfmscrobbler.cpp new file mode 100644 index 000000000..be1086a8d --- /dev/null +++ b/src/plugins/General/scrobbler/lastfmscrobbler.cpp @@ -0,0 +1,464 @@ +/*************************************************************************** + * Copyright (C) 2010-2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "lastfmscrobbler.h" + +#define SCROBBLER_LASTFM_URL "http://ws.audioscrobbler.com/2.0/" +#define API_KEY "d71c6f01b2ea562d7042bd5f5970041f" +#define SECRET "32d47bc0010473d40e1d38bdcff20968" + + +LastfmScrobbler::LastfmScrobbler(QObject *parent) : QObject(parent) +{ + m_getTokenReply = 0; + m_getSessionReply = 0; + m_notificationReply = 0; + m_submitedSongs = 0; + m_submitReply = 0; + m_state = Qmmp::Stopped; + m_time = new QTime(); + m_cache = new ScrobblerCache(QDir::homePath() +"/.qmmp/scrobbler_lastfm.cache"); + m_ua = QString("qmmp-plugins/%1").arg(Qmmp::strVersion().toLower()).toAscii(); + m_http = new QNetworkAccessManager(this); + m_core = SoundCore::instance(); + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + m_session = settings.value("Scrobbler/lastfm_session").toString(); + + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); + connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); + connect(m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); + connect(m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); + + setupProxy(); + m_cachedSongs = m_cache->load(); + + if(m_session.isEmpty()) + getToken(); + else + { + submit(); + if(m_core->state() == Qmmp::Playing) + { + setState(Qmmp::Playing); + updateMetaData(); + } + } +} + +LastfmScrobbler::~LastfmScrobbler() +{ + m_cache->save(m_cachedSongs); + delete m_time; + delete m_cache; + +} + +void LastfmScrobbler::setState(Qmmp::State state) +{ + m_state = state; + switch ((uint) state) + { + case Qmmp::Playing: + m_start_ts = QDateTime::currentDateTime().toTime_t(); + m_time->restart(); + break; + case Qmmp::Stopped: + if (!m_song.metaData().isEmpty() + && ((m_time->elapsed ()/1000 > 240) || (m_time->elapsed ()/1000 > int(m_song.length()/2))) + && (m_song.length() > MIN_SONG_LENGTH)) + { + m_song.setTimeStamp(m_start_ts); + m_cachedSongs << m_song; + m_cache->save(m_cachedSongs); + } + + m_song.clear(); + if (m_cachedSongs.isEmpty()) + break; + + if (!m_session.isEmpty() && !m_submitReply) + submit(); + break; + default: + ; + } +} + +void LastfmScrobbler::updateMetaData() +{ + QMap metadata = m_core->metaData(); + if(m_state != Qmmp::Playing || m_core->totalTime() <= 0) //skip stream + return; + if(metadata.value(Qmmp::TITLE).isEmpty() || metadata.value(Qmmp::ARTIST).isEmpty()) //skip empty tags + return; + if(m_notificationReply && m_submitReply) //used + return; + + m_song = SongInfo(metadata, m_core->totalTime()/1000); + sendNotification(m_song); +} + +void LastfmScrobbler::processResponse(QNetworkReply *reply) +{ + if (reply->error() != QNetworkReply::NoError) + { + qWarning("LastfmScrobbler: http error: %s", qPrintable(reply->errorString())); + } + + LastfmResponse response; + QStringList tags; + QXmlStreamReader reader(reply); + while(!reader.atEnd()) + { + reader.readNext(); + if(reader.isStartElement()) + { + tags << reader.name().toString(); + if(tags.last() == "lfm") + response.status = reader.attributes().value("status").toString(); + else if(tags.last() == "error") + response.code = reader.attributes().value("code").toString(); + } + else if(reader.isCharacters() && !reader.isWhitespace()) + { + if(tags.last() == "token") + response.token = reader.text().toString(); + else if(tags.last() == "error") + response.error = reader.text().toString(); + if(tags.count() >= 2 && tags.at(tags.count() - 2) == "session") + { + if(tags.last() == "key") + response.key = reader.text().toString(); + else if(tags.last() == "name") + response.name = reader.text().toString(); + else if(tags.last() == "subscriber") + response.subscriber = reader.text().toString(); + } + } + else if(reader.isEndElement()) + { + tags.takeLast(); + } + } + + QString error_code; + if(response.status != "ok" && !response.status.isEmpty()) + { + if(!response.error.isEmpty()) + { + qWarning("LastfmScrobbler: status=%s, %s-%s", qPrintable(response.status), + qPrintable(response.code), qPrintable(response.error)); + error_code = response.code; + } + else + qWarning("LastfmScrobbler: invalid content"); + } + + if (reply == m_getTokenReply) + { + m_getTokenReply = 0; + if(response.status == "ok") + { + m_token = response.token; + qDebug("LastfmScrobbler: token: %s", qPrintable(m_token)); + QDesktopServices::openUrl("http://www.last.fm/api/auth/?api_key="API_KEY"&token="+m_token); + QTimer::singleShot(120000, this, SLOT(getSession())); //2 minutes + } + else if(error_code == "8" || error_code == "7" || error_code == "11" || error_code.isEmpty()) + { + m_token.clear(); + QTimer::singleShot(120000, this, SLOT(getToken())); // wait 2 minutes and try again + } + else + { + m_token.clear(); + qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); + } + } + else if(reply == m_getSessionReply) + { + m_getSessionReply = 0; + m_session.clear(); + if(response.status == "ok") + { + m_session = response.key; + qDebug("LastfmScrobbler: name: %s", qPrintable(response.name)); + qDebug("LastfmScrobbler: key: %s", qPrintable(m_session)); + qDebug("LastfmScrobbler: subscriber: %s", qPrintable(response.subscriber)); + QSettings settings(Qmmp::configFile(), QSettings::IniFormat); + settings.setValue("Scrobbler/lastfm_session", m_session); + submit(); + } + else if(error_code == "4" || error_code == "15") //invalid token + { + m_token.clear(); + getToken(); + } + else if(error_code == "11") //service offline + { + QTimer::singleShot(120000, this, SLOT(getSession())); + } + else if(error_code == "14") // unauthorized token + { + QDesktopServices::openUrl("http://www.last.fm/api/auth/?api_key="API_KEY"&token="+m_token); + QTimer::singleShot(120000, this, SLOT(getSession())); //2 minutes + } + else if (error_code.isEmpty()) //network error + { + QTimer::singleShot(120000, this, SLOT(getSession())); + } + else + { + m_token.clear(); + qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); + } + } + else if (reply == m_submitReply) + { + m_submitReply = 0; + if (response.status == "ok") + { + qDebug("LastfmScrobbler: submited %d song(s)", m_submitedSongs); + while (m_submitedSongs) + { + m_submitedSongs--; + m_cachedSongs.removeFirst (); + } + if (!m_cachedSongs.isEmpty()) //submit remaining songs + { + submit(); + } + else + { + m_cache->save(m_cachedSongs); // update the cache file to reflect the empty cache + updateMetaData(); + } + } + else if(error_code == "9") //invalid session key + { + m_session.clear(); + getToken(); + } + else if(error_code == "11" || error_code == "16" || error_code.isEmpty()) //unavailable + { + QTimer::singleShot(120000, this, SLOT(submit())); + } + else + { + m_session.clear(); + qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); + } + } + else if (reply == m_notificationReply) + { + m_notificationReply = 0; + if(response.status == "ok") + { + qDebug("LastfmScrobbler: Now-Playing notification done"); + } + else if(error_code == "9") //invalid session key + { + m_session.clear(); + getToken(); + } + } + reply->deleteLater(); +} + +void LastfmScrobbler::setupProxy() +{ + QmmpSettings *gs = QmmpSettings::instance(); + if (gs->isProxyEnabled()) + { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); + if(gs->useProxyAuth()) + { + proxy.setUser(gs->proxy().userName()); + proxy.setPassword(gs->proxy().password()); + } + m_http->setProxy(proxy); + } + else + m_http->setProxy(QNetworkProxy::NoProxy); +} + +void LastfmScrobbler::getToken() +{ + qDebug("LastfmScrobbler: new token request"); + m_session.clear(); + QUrl url(QString(SCROBBLER_LASTFM_URL) + "?"); + url.setPort(80); + url.addQueryItem("method", "auth.getToken"); + url.addQueryItem("api_key", API_KEY); + + QByteArray data; + data.append("api_key"API_KEY); + data.append("methodauth.getToken"); + data.append(SECRET); + url.addQueryItem("api_sig", QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex()); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host",url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + m_getTokenReply = m_http->get(request); +} + +void LastfmScrobbler::getSession() +{ + qDebug("LastfmScrobbler: new session request"); + QUrl url(QString(SCROBBLER_LASTFM_URL) + "?"); + url.setPort(80); + url.addQueryItem("api_key", API_KEY); + url.addQueryItem("method", "auth.getSession"); + url.addQueryItem("token", m_token); + + QByteArray data; + data.append("api_key"API_KEY); + data.append("methodauth.getSession"); + data.append("token" + m_token.toUtf8()); + data.append(SECRET); + url.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host",url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + m_getSessionReply = m_http->get(request); +} + +void LastfmScrobbler::submit() +{ + if (m_cachedSongs.isEmpty() || m_session.isEmpty() || m_submitReply) + return; + + qDebug("LastfmScrobbler: submit request"); + m_submitedSongs = qMin(m_cachedSongs.size(),25); + + QMap params; + for (int i = 0; i < m_submitedSongs; ++i) + { + SongInfo info = m_cachedSongs[i]; + params.insert(QString("track[%1]").arg(i),info.metaData(Qmmp::TITLE)); + params.insert(QString("timestamp[%1]").arg(i),QString("%1").arg(info.timeStamp())); + params.insert(QString("artist[%1]").arg(i),info.metaData(Qmmp::ARTIST)); + params.insert(QString("album[%1]").arg(i),info.metaData(Qmmp::ALBUM)); + params.insert(QString("trackNumber[%1]").arg(i),info.metaData(Qmmp::TRACK)); + params.insert(QString("duration[%1]").arg(i),QString("%1").arg(info.length())); + } + params.insert("api_key", API_KEY); + params.insert("method", "track.scrobble"); + params.insert("sk", m_session); + + QStringList keys = params.keys(); + foreach (QString key, keys) //removes empty keys + { + if(params.value(key).isEmpty() || params.value(key) == "0") + params.remove(key); + } + + QUrl url(SCROBBLER_LASTFM_URL); + url.setPort(80); + + QUrl body(""); + QByteArray data; + foreach (QString key, params.keys()) + { + body.addQueryItem(key, params.value(key)); + data.append(key.toUtf8() + params.value(key).toUtf8()); + } + data.append(SECRET); + body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + QByteArray bodyData = body.toEncoded().remove(0,1); + bodyData.replace("+", QUrl::toPercentEncoding("+")); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host", url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); + m_submitReply = m_http->post(request, bodyData); +} + +void LastfmScrobbler::sendNotification(const SongInfo &info) +{ + if(m_session.isEmpty()) + return; + qDebug("LastfmScrobbler: sending notification"); + + QMap params; + params.insert("track", info.metaData(Qmmp::TITLE)); + params.insert("artist", info.metaData(Qmmp::ARTIST)); + if(!info.metaData(Qmmp::ALBUM).isEmpty()) + params.insert("album", info.metaData(Qmmp::ALBUM)); + if(!info.metaData(Qmmp::TRACK).isEmpty()) + params.insert("trackNumber", info.metaData(Qmmp::TRACK)); + params.insert("duration", QString("%1").arg(info.length())); + params.insert("api_key", API_KEY); + params.insert("method", "track.updateNowPlaying"); + params.insert("sk", m_session); + + foreach (QString key, params) //removes empty keys + { + if(params.value(key).isEmpty()) + params.remove(key); + } + + QUrl url(SCROBBLER_LASTFM_URL); + url.setPort(80); + + QUrl body(""); + QByteArray data; + foreach (QString key, params.keys()) + { + body.addQueryItem(key, params.value(key)); + data.append(key.toUtf8() + params.value(key).toUtf8()); + } + data.append(SECRET); + body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); + QByteArray bodyData = body.toEncoded().remove(0,1); + bodyData.replace("+", QUrl::toPercentEncoding("+")); + + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host", url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); + m_notificationReply = m_http->post(request, bodyData); +} diff --git a/src/plugins/General/scrobbler/lastfmscrobbler.h b/src/plugins/General/scrobbler/lastfmscrobbler.h new file mode 100644 index 000000000..313661e8b --- /dev/null +++ b/src/plugins/General/scrobbler/lastfmscrobbler.h @@ -0,0 +1,85 @@ +/*************************************************************************** + * Copyright (C) 2010-2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef LASTFMSCROBBLER_H +#define LASTFMSCROBBLER_H + +#include +#include +#include +#include "scrobblercache.h" + +class QNetworkAccessManager; +class QNetworkReply; +class QTime; +class SoundCore; + +/** + @author Ilya Kotov +*/ +struct LastfmResponse +{ + QString status; + QString token; + QString code; + QString error; + QString key; + QString name; + QString subscriber; +}; + +/** + @author Ilya Kotov +*/ +class LastfmScrobbler : public QObject +{ + Q_OBJECT +public: + LastfmScrobbler(QObject *parent = 0); + ~LastfmScrobbler(); + +private slots: + void setState(Qmmp::State state); + void updateMetaData(); + void processResponse(QNetworkReply *reply); + void setupProxy(); + void getToken(); + void getSession(); + void submit(); + +private: + enum { MIN_SONG_LENGTH = 30 }; + + void sendNotification(const SongInfo &info); + uint m_start_ts; + SongInfo m_song; + Qmmp::State m_state; + QList m_cachedSongs; + QByteArray m_ua; + int m_submitedSongs; + QString m_token, m_session; + QNetworkAccessManager *m_http; + SoundCore *m_core; + QNetworkReply *m_getTokenReply, *m_getSessionReply; + QNetworkReply *m_submitReply, *m_notificationReply; + QTime *m_time; + ScrobblerCache *m_cache; +}; + +#endif diff --git a/src/plugins/General/scrobbler/librefmscrobbler.cpp b/src/plugins/General/scrobbler/librefmscrobbler.cpp new file mode 100644 index 000000000..c4e624697 --- /dev/null +++ b/src/plugins/General/scrobbler/librefmscrobbler.cpp @@ -0,0 +1,357 @@ +/*************************************************************************** + * Copyright (C) 2008-2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "librefmscrobbler.h" + +#define PROTOCOL_VER "1.2.1" +#define CLIENT_ID "qmm" +#define CLIENT_VER "0.7" +#define SCROBBLER_LIBREFM_URL "http://turtle.libre.fm/" + + +LibrefmScrobbler::LibrefmScrobbler(const QString &login, const QString &passw, QObject *parent) + : QObject(parent) +{ + m_failure_count = 0; + m_handshake_count = 0; + m_submitedSongs = 0; + m_handshakeReply = 0; + m_submitReply = 0; + m_notificationReply = 0; + m_ua = QString("iScrobbler/1.5.1qmmp-plugins/%1").arg(Qmmp::strVersion()).toAscii(); + m_login = login; + m_passw = passw; + m_passw = QString(QCryptographicHash::hash(m_passw.toAscii(), QCryptographicHash::Md5).toHex()); + m_disabled = login.isEmpty() || passw.isEmpty(); + m_core = SoundCore::instance(); + m_cache = new ScrobblerCache(QDir::homePath() +"/.qmmp/scrobbler_librefm.cache"); + m_http = new QNetworkAccessManager(this); + m_time = new QTime(); + + connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); + connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); + connect (m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); + connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); + + setupProxy(); + m_cachedSongs = m_cache->load(); + + m_start_ts = QDateTime::currentDateTime().toTime_t(); + handshake(); +} + + +LibrefmScrobbler::~LibrefmScrobbler() +{ + m_cache->save(m_cachedSongs); + delete m_time; + delete m_cache; +} + +void LibrefmScrobbler::setState(Qmmp::State state) +{ + switch ((uint) state) + { + case Qmmp::Playing: + m_start_ts = QDateTime::currentDateTime().toTime_t(); + m_time->restart(); + if (!isReady() && !m_handshakeReply) + handshake(); + break; + case Qmmp::Stopped: + if (!m_song.metaData().isEmpty() + && ((m_time->elapsed ()/1000 > 240) || (m_time->elapsed ()/1000 > int(m_song.length()/2))) + && (m_song.length() > MIN_SONG_LENGTH)) + { + m_song.setTimeStamp(m_start_ts); + m_cachedSongs << m_song; + m_cache->save(m_cachedSongs); + } + m_song.clear(); + if (m_cachedSongs.isEmpty()) + break; + + if (isReady() && !m_submitReply) + submit(); + break; + default: + ; + } +} + +void LibrefmScrobbler::updateMetaData() +{ + QMap metadata = m_core->metaData(); + if (m_core->state() == Qmmp::Playing + && !metadata.value(Qmmp::TITLE).isEmpty() //skip empty tags + && !metadata.value(Qmmp::ARTIST).isEmpty() + && m_core->totalTime() >= 0) //skip stream + { + metadata[Qmmp::ARTIST].replace("%", QUrl::toPercentEncoding("%")); //replace special symbols + metadata[Qmmp::ALBUM].replace("%", QUrl::toPercentEncoding("%")); + metadata[Qmmp::TITLE].replace("%", QUrl::toPercentEncoding("%")); + metadata[Qmmp::ARTIST].replace("&", QUrl::toPercentEncoding("&")); + metadata[Qmmp::ALBUM].replace("&", QUrl::toPercentEncoding("&")); + metadata[Qmmp::TITLE].replace("&", QUrl::toPercentEncoding("&")); + metadata[Qmmp::ARTIST].replace("=", QUrl::toPercentEncoding("=")); + metadata[Qmmp::ALBUM].replace("=", QUrl::toPercentEncoding("=")); + metadata[Qmmp::TITLE].replace("=", QUrl::toPercentEncoding("=")); + m_song = SongInfo(metadata, m_core->totalTime()/1000); + if (isReady() && !m_notificationReply && !m_submitReply) + sendNotification(m_song); + } +} + +void LibrefmScrobbler::processResponse(QNetworkReply *reply) +{ + QString data; + if (reply->error() == QNetworkReply::NoError) + data = reply->readAll(); + else + data = reply->errorString (); + + data = data.trimmed(); + if (data.startsWith("OK")) + { + m_failure_count = 0; + m_handshake_count = 0; + } + + if (reply == m_handshakeReply) + { + m_handshakeReply = 0; + m_submitUrl.clear(); + m_session.clear(); + m_nowPlayingUrl.clear(); + m_failure_count = 0; + QStringList strlist = data.split("\n"); + if (!strlist[0].contains("OK") || strlist.size() < 4) + { + qWarning("LibrefmScrobbler: handshake phase error"); + m_disabled = true; + if(strlist[0].contains("BANNED")) + qWarning("LibrefmScrobbler: client has been banned"); + else if(strlist[0].contains("BADAUTH")) + qWarning("LibrefmScrobbler: incorrect user/password"); + else if(strlist[0].contains("BADTIME")) + qWarning("LibrefmScrobbler: incorrect system time"); + else + { + qWarning("LibrefmScrobbler: service error: %s", qPrintable(strlist[0])); + m_disabled = false; + m_handshake_count++; + QTimer::singleShot (60000 * qMin(m_handshake_count^2, 120) , this, SLOT(handshake())); + qWarning("LibrefmScrobbler: waiting %d minutes...", qMin(m_handshake_count^2, 120)); + } + } + else if (strlist.size() > 3) //process handshake response + { + qDebug("LibrefmScrobbler: reading handshake response"); + qDebug("LibrefmScrobbler: Session ID: %s", qPrintable(strlist[1])); + qDebug("LibrefmScrobbler: Now-Playing URL: %s", qPrintable(strlist[2])); + qDebug("LibrefmScrobbler: Submission URL: %s", qPrintable(strlist[3])); + m_submitUrl = strlist[3]; + m_nowPlayingUrl = strlist[2]; + m_session = strlist[1]; + updateMetaData(); //send now-playing notification for already playing song + if (!m_cachedSongs.isEmpty()) //submit recent songs + submit(); + } + } + else if (reply == m_submitReply) + { + m_submitReply = 0; + if (!data.startsWith("OK")) + { + qWarning("LibrefmScrobbler: submit error"); + if(data.contains("BADSESSION")) + { + qWarning("LibrefmScrobbler: invalid session ID"); + qWarning("LibrefmScrobbler: performing re-handshake"); + handshake(); + } + else + { + qWarning("LibrefmScrobbler: %s", qPrintable(data)); + m_failure_count ++; + } + } + else + { + qDebug("LibrefmScrobbler: submited %d song(s)", m_submitedSongs); + while (m_submitedSongs) + { + m_submitedSongs--; + m_cachedSongs.removeFirst (); + } + if (!m_cachedSongs.isEmpty()) //submit remaining songs + submit(); + else + updateMetaData(); + } + } + else if (reply == m_notificationReply) + { + m_notificationReply = 0; + if (!data.startsWith("OK")) + { + qWarning("LibrefmScrobbler: notification error"); + if(data.contains("BADSESSION")) + { + qWarning("LibrefmScrobbler: invalid session ID"); + qWarning("LibrefmScrobbler: performing re-handshake"); + handshake(); + } + else + { + qWarning("LibrefmScrobbler: %s",qPrintable(data)); + m_failure_count ++; + } + } + else + qDebug("LibrefmScrobbler: Now-Playing notification done"); + } + if(m_failure_count >= 3) + { + qWarning("LibrefmScrobbler: performing re-handshake"); + handshake(); + } + reply->deleteLater(); +} + +void LibrefmScrobbler::setupProxy() +{ + QmmpSettings *gs = QmmpSettings::instance(); + if (gs->isProxyEnabled()) + { + QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); + if(gs->useProxyAuth()) + { + proxy.setUser(gs->proxy().userName()); + proxy.setPassword(gs->proxy().password()); + } + m_http->setProxy(proxy); + } + else + m_http->setProxy(QNetworkProxy::NoProxy); +} + +void LibrefmScrobbler::handshake() +{ + if (m_disabled) + return; + qDebug("LibrefmScrobbler: handshake request"); + uint ts = QDateTime::currentDateTime().toTime_t(); + qDebug("LibrefmScrobbler: current time stamp %d",ts); + QString auth_tmp = QString("%1%2").arg(m_passw).arg(ts); + QByteArray auth = QCryptographicHash::hash(auth_tmp.toAscii (), QCryptographicHash::Md5); + auth = auth.toHex(); + QUrl url(QString(SCROBBLER_LIBREFM_URL) + "?"); + url.addQueryItem("hs", "true"); + url.addQueryItem("p", PROTOCOL_VER); + url.addQueryItem("c", CLIENT_ID); + url.addQueryItem("v", CLIENT_VER); + url.addQueryItem("u", m_login); + url.addQueryItem("t", QString::number(ts)); + url.addQueryItem("a", QString(auth)); + url.setPort(80); + qDebug("LibrefmScrobbler: request url: %s",qPrintable(url.toString())); + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host",url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + m_handshakeReply = m_http->get(request); +} + +void LibrefmScrobbler::submit() +{ + qDebug("LibrefmScrobbler: submit request"); + if (m_cachedSongs.isEmpty()) + return; + m_submitedSongs = qMin(m_cachedSongs.size(),25); + QString body = QString("s=%1").arg(m_session); + for (int i = 0; i < m_submitedSongs; ++i) + { + SongInfo info = m_cachedSongs[i]; + body += QString("&a[%9]=%1&t[%9]=%2&i[%9]=%3&o[%9]=%4&r[%9]=%5&l[%9]=%6&b[%9]=%7&n[%9]=%8&m[%9]=") + .arg(info.metaData(Qmmp::ARTIST)) + .arg(info.metaData(Qmmp::TITLE)) + .arg(info.timeStamp()) + .arg("P") + .arg("") + .arg(info.length()) + .arg(info.metaData(Qmmp::ALBUM)) + .arg(info.metaData(Qmmp::TRACK)) + .arg(i); + } + //qDebug("%s",qPrintable(body)); + QUrl url(m_submitUrl); + url.setPort(80); + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host",url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, + QUrl::toPercentEncoding(body,":/[]&=%").size()); + m_submitReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); +} + +void LibrefmScrobbler::sendNotification(const SongInfo &info) +{ + qDebug("LibrefmScrobbler: sending notification"); + QString body = QString("s=%1").arg(m_session); + body += QString("&a=%1&t=%2&b=%3&l=%4&n=%5&m=") + .arg(info.metaData(Qmmp::ARTIST)) + .arg(info.metaData(Qmmp::TITLE)) + .arg(info.metaData(Qmmp::ALBUM)) + .arg(info.length()) + .arg(info.metaData(Qmmp::TRACK)); + QUrl url(m_nowPlayingUrl); + url.setPort(80); + QNetworkRequest request(url); + request.setRawHeader("User-Agent", m_ua); + request.setRawHeader("Host", url.host().toAscii()); + request.setRawHeader("Accept", "*/*"); + request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); + request.setHeader(QNetworkRequest::ContentLengthHeader, + QUrl::toPercentEncoding(body,":/[]&=%").size()); + m_notificationReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); +} + +bool LibrefmScrobbler::isReady() +{ + return !m_submitUrl.isEmpty() && !m_session.isEmpty(); +} diff --git a/src/plugins/General/scrobbler/librefmscrobbler.h b/src/plugins/General/scrobbler/librefmscrobbler.h new file mode 100644 index 000000000..ece5ecec1 --- /dev/null +++ b/src/plugins/General/scrobbler/librefmscrobbler.h @@ -0,0 +1,81 @@ +/*************************************************************************** + * Copyright (C) 2008-2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ +#ifndef LIBREFMSCROBBLER_H +#define LIBREFMSCROBBLER_H + +#include +#include +#include +#include "scrobblercache.h" + +class QNetworkAccessManager; +class QNetworkReply; +class QTime; +class SoundCore; + +/** + @author Ilya Kotov +*/ +class LibrefmScrobbler : public QObject +{ + Q_OBJECT +public: + LibrefmScrobbler(const QString &login, const QString &passw, QObject *parent = 0); + + ~LibrefmScrobbler(); + +private slots: + void setState(Qmmp::State state); + void updateMetaData(); + void processResponse(QNetworkReply *reply); + void setupProxy(); + void handshake(); + +private: + enum { MIN_SONG_LENGTH = 30 }; + + void submit(); + void sendNotification(const SongInfo &info); + bool isReady(); + + uint m_start_ts; + SongInfo m_song; + QString m_login; + QString m_passw; + QString m_submitUrl; + QString m_nowPlayingUrl; + QString m_session; + QList m_cachedSongs; + QByteArray m_ua; + int m_submitedSongs; + int m_failure_count; + int m_handshake_count; + bool m_disabled; + QNetworkAccessManager *m_http; + SoundCore *m_core; + QNetworkReply *m_handshakeReply; + QNetworkReply *m_submitReply; + QNetworkReply *m_notificationReply; + QTime* m_time; + ScrobblerCache *m_cache; + +}; + +#endif diff --git a/src/plugins/General/scrobbler/scrobbler.cpp b/src/plugins/General/scrobbler/scrobbler.cpp deleted file mode 100644 index d57b38783..000000000 --- a/src/plugins/General/scrobbler/scrobbler.cpp +++ /dev/null @@ -1,512 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * 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, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "scrobbler.h" - -#define PROTOCOL_VER "1.2.1" -#define CLIENT_ID "qmm" -#define CLIENT_VER "0.7" - - -Scrobbler::Scrobbler(const QString &url, - const QString &login, - const QString &passw, - const QString &name, - QObject *parent) : QObject(parent) -{ - m_failure_count = 0; - m_handshake_count = 0; - m_submitedSongs = 0; - m_handshakeReply = 0; - m_submitReply = 0; - m_notificationReply = 0; - m_server = url; - m_name = name; - m_ua = QString("iScrobbler/1.5.1qmmp-plugins/%1").arg(Qmmp::strVersion()).toAscii(); - m_login = login; - m_passw = passw; - m_passw = QString(QCryptographicHash::hash(m_passw.toAscii(), QCryptographicHash::Md5).toHex()); - m_disabled = login.isEmpty() || passw.isEmpty(); - m_core = SoundCore::instance(); - m_http = new QNetworkAccessManager(this); - m_time = new QTime(); - - connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); - connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); - connect (m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); - connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); - - setupProxy(); - readCache(); - - m_start_ts = QDateTime::currentDateTime().toTime_t(); - handshake(); -} - - -Scrobbler::~Scrobbler() -{ - delete m_time; - writeCache(); -} - -void Scrobbler::setState(Qmmp::State state) -{ - switch ((uint) state) - { - case Qmmp::Playing: - m_start_ts = QDateTime::currentDateTime().toTime_t(); - m_time->restart(); - if (!isReady() && !m_handshakeReply) - handshake(); - break; - case Qmmp::Stopped: - if (!m_song.metaData().isEmpty() - && ((m_time->elapsed ()/1000 > 240) || (m_time->elapsed ()/1000 > int(m_song.length()/2))) - && (m_song.length() > MIN_SONG_LENGTH)) - { - m_song.setTimeStamp(m_start_ts); - m_songCache << m_song; - writeCache(); - } - m_song.clear(); - if (m_songCache.isEmpty()) - break; - - if (isReady() && !m_submitReply) - submit(); - break; - default: - ; - } -} - -void Scrobbler::updateMetaData() -{ - QMap metadata = m_core->metaData(); - if (m_core->state() == Qmmp::Playing - && !metadata.value(Qmmp::TITLE).isEmpty() //skip empty tags - && !metadata.value(Qmmp::ARTIST).isEmpty() - && m_core->totalTime() //skip stream - && !metadata.value(Qmmp::ARTIST).contains("=") //skip tags with special symbols - && !metadata.value(Qmmp::TITLE).contains("=") - && !metadata.value(Qmmp::ALBUM).contains("=")) - { - metadata[Qmmp::ARTIST].replace("%", QUrl::toPercentEncoding("%")); //replace special symbols - metadata[Qmmp::ALBUM].replace("%", QUrl::toPercentEncoding("%")); - metadata[Qmmp::TITLE].replace("%", QUrl::toPercentEncoding("%")); - metadata[Qmmp::ARTIST].replace("&", QUrl::toPercentEncoding("&")); - metadata[Qmmp::ALBUM].replace("&", QUrl::toPercentEncoding("&")); - metadata[Qmmp::TITLE].replace("&", QUrl::toPercentEncoding("&")); - m_song = SongInfo(metadata, m_core->totalTime()/1000); - if (isReady() && !m_notificationReply && !m_submitReply) - sendNotification(m_song); - } -} - -void Scrobbler::processResponse(QNetworkReply *reply) -{ - QString data; - if (reply->error() == QNetworkReply::NoError) - data = reply->readAll(); - else - data = reply->errorString (); - - data = data.trimmed(); - if (data.startsWith("OK")) - { - m_failure_count = 0; - m_handshake_count = 0; - } - - if (reply == m_handshakeReply) - { - m_handshakeReply = 0; - m_submitUrl.clear(); - m_session.clear(); - m_nowPlayingUrl.clear(); - m_failure_count = 0; - QStringList strlist = data.split("\n"); - if (!strlist[0].contains("OK") || strlist.size() < 4) - { - qWarning("Scrobbler[%s]: handshake phase error", qPrintable(m_name)); - m_disabled = true; - if(strlist[0].contains("BANNED")) - qWarning("Scrobbler[%s]: client has been banned", qPrintable(m_name)); - else if(strlist[0].contains("BADAUTH")) - qWarning("Scrobbler[%s]: incorrect user/password", qPrintable(m_name)); - else if(strlist[0].contains("BADTIME")) - qWarning("Scrobbler[%s]: incorrect system time", qPrintable(m_name)); - else - { - qWarning("Scrobbler[%s]: service error: %s", qPrintable(m_name), qPrintable(strlist[0])); - m_disabled = false; - m_handshake_count++; - QTimer::singleShot (60000 * qMin(m_handshake_count^2, 120) , this, SLOT(handshake())); - qWarning("Scrobbler[%s]: waiting %d minutes...", qPrintable(m_name), - qMin(m_handshake_count^2, 120)); - } - } - else if (strlist.size() > 3) //process handshake response - { - qDebug("Scrobbler[%s]: reading handshake response",qPrintable(m_name)); - qDebug("Scrobbler[%s]: Session ID: %s",qPrintable(m_name),qPrintable(strlist[1])); - qDebug("Scrobbler[%s]: Now-Playing URL: %s",qPrintable(m_name),qPrintable(strlist[2])); - qDebug("Scrobbler[%s]: Submission URL: %s",qPrintable(m_name),qPrintable(strlist[3])); - m_submitUrl = strlist[3]; - m_nowPlayingUrl = strlist[2]; - m_session = strlist[1]; - updateMetaData(); //send now-playing notification for already playing song - if (!m_songCache.isEmpty()) //submit recent songs - submit(); - } - } - else if (reply == m_submitReply) - { - m_submitReply = 0; - if (!data.startsWith("OK")) - { - qWarning("Scrobbler[%s]: submit error",qPrintable(m_name)); - if(data.contains("BADSESSION")) - { - qWarning("Scrobbler[%s]: invalid session ID",qPrintable(m_name)); - qWarning("Scrobbler[%s]: performing re-handshake",qPrintable(m_name)); - handshake(); - } - else - { - qWarning("Scrobbler[%s]: %s",qPrintable(m_name),qPrintable(data)); - m_failure_count ++; - } - } - else - { - qDebug("Scrobbler[%s]: submited %d song(s)",qPrintable(m_name), m_submitedSongs); - while (m_submitedSongs) - { - m_submitedSongs--; - m_songCache.removeFirst (); - } - if (!m_songCache.isEmpty()) //submit remaining songs - submit(); - else - updateMetaData(); - } - } - else if (reply == m_notificationReply) - { - m_notificationReply = 0; - if (!data.startsWith("OK")) - { - qWarning("Scrobbler[%s]: notification error",qPrintable(m_name)); - if(data.contains("BADSESSION")) - { - qWarning("Scrobbler[%s]: invalid session ID",qPrintable(m_name)); - qWarning("Scrobbler[%s]: performing re-handshake",qPrintable(m_name)); - handshake(); - } - else - { - qWarning("Scrobbler[%s]: %s",qPrintable(m_name),qPrintable(data)); - m_failure_count ++; - } - } - else - qDebug("Scrobbler[%s]: Now-Playing notification done", qPrintable(m_name)); - } - if(m_failure_count >= 3) - { - qWarning("Scrobbler[%s]: performing re-handshake",qPrintable(m_name)); - handshake(); - } - reply->deleteLater(); -} - -void Scrobbler::setupProxy() -{ - QmmpSettings *gs = QmmpSettings::instance(); - if (gs->isProxyEnabled()) - { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); - if(gs->useProxyAuth()) - { - proxy.setUser(gs->proxy().userName()); - proxy.setPassword(gs->proxy().password()); - } - m_http->setProxy(proxy); - } - else - m_http->setProxy(QNetworkProxy::NoProxy); -} - -void Scrobbler::handshake() -{ - if (m_disabled) - return; - qDebug("Scrobbler[%s] handshake request",qPrintable(m_name)); - uint ts = QDateTime::currentDateTime().toTime_t(); - qDebug("Scrobbler[%s]: current time stamp %d",qPrintable(m_name),ts); - QString auth_tmp = QString("%1%2").arg(m_passw).arg(ts); - QByteArray auth = QCryptographicHash::hash(auth_tmp.toAscii (), QCryptographicHash::Md5); - auth = auth.toHex(); - QUrl url(QString("http://") + m_server + "/?"); - url.addQueryItem("hs", "true"); - url.addQueryItem("p", PROTOCOL_VER); - url.addQueryItem("c", CLIENT_ID); - url.addQueryItem("v", CLIENT_VER); - url.addQueryItem("u", m_login); - url.addQueryItem("t", QString::number(ts)); - url.addQueryItem("a", QString(auth)); - url.setPort(80); - qDebug("Scrobbler[%s]: request url: %s",qPrintable(m_name),qPrintable(url.toString())); - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - m_handshakeReply = m_http->get(request); -} - -void Scrobbler::submit() -{ - qDebug("Scrobbler[%s]: submit request", qPrintable(m_name)); - if (m_songCache.isEmpty()) - return; - m_submitedSongs = qMin(m_songCache.size(),25); - QString body = QString("s=%1").arg(m_session); - for (int i = 0; i < m_submitedSongs; ++i) - { - SongInfo info = m_songCache[i]; - body += QString("&a[%9]=%1&t[%9]=%2&i[%9]=%3&o[%9]=%4&r[%9]=%5&l[%9]=%6&b[%9]=%7&n[%9]=%8&m[%9]=") - .arg(info.metaData(Qmmp::ARTIST)) - .arg(info.metaData(Qmmp::TITLE)) - .arg(info.timeStamp()) - .arg("P") - .arg("") - .arg(info.length()) - .arg(info.metaData(Qmmp::ALBUM)) - .arg(info.metaData(Qmmp::TRACK)) - .arg(i); - } - //qDebug("%s",qPrintable(body)); - QUrl url(m_submitUrl); - url.setPort(80); - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, - QUrl::toPercentEncoding(body,":/[]&=%").size()); - m_submitReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); -} - -void Scrobbler::sendNotification(const SongInfo &info) -{ - qDebug("Scrobbler[%s]: sending notification", qPrintable(m_name)); - QString body = QString("s=%1").arg(m_session); - body += QString("&a=%1&t=%2&b=%3&l=%4&n=%5&m=") - .arg(info.metaData(Qmmp::ARTIST)) - .arg(info.metaData(Qmmp::TITLE)) - .arg(info.metaData(Qmmp::ALBUM)) - .arg(info.length()) - .arg(info.metaData(Qmmp::TRACK)); - QUrl url(m_nowPlayingUrl); - url.setPort(80); - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, - QUrl::toPercentEncoding(body,":/[]&=%").size()); - m_notificationReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); -} - -bool Scrobbler::isReady() -{ - return !m_submitUrl.isEmpty() && !m_session.isEmpty(); -} - -void Scrobbler::writeCache() -{ - QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); - if (m_songCache.isEmpty()) - { - file.remove(); - return; - } - file.open(QIODevice::WriteOnly); - foreach(SongInfo m, m_songCache) - { - file.write(QString("title=%1").arg(m.metaData(Qmmp::TITLE)).toUtf8() +"\n"); - file.write(QString("artist=%1").arg(m.metaData(Qmmp::ARTIST)).toUtf8() +"\n"); - file.write(QString("album=%1").arg(m.metaData(Qmmp::ALBUM)).toUtf8() +"\n"); - file.write(QString("comment=%1").arg(m.metaData(Qmmp::COMMENT)).toUtf8() +"\n"); - file.write(QString("genre=%1").arg(m.metaData(Qmmp::GENRE)).toUtf8() +"\n"); - file.write(QString("year=%1").arg(m.metaData(Qmmp::YEAR)).toUtf8() +"\n"); - file.write(QString("track=%1").arg(m.metaData(Qmmp::TRACK)).toUtf8() +"\n"); - file.write(QString("length=%1").arg(m.length()).toUtf8() +"\n"); - file.write(QString("time=%1").arg(m.timeStamp()).toUtf8() +"\n"); - } - file.close(); -} - -void Scrobbler::readCache() -{ - int s = 0; - QString line, param, value; - QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); - if(m_disabled || !file.open(QIODevice::ReadOnly)) - return; - - while (!file.atEnd()) - { - line = QString::fromUtf8(file.readLine()).trimmed(); - if ((s = line.indexOf("=")) < 0) - continue; - - param = line.left(s); - value = line.right(line.size() - s - 1); - - if (param == "title") - { - m_songCache << SongInfo(); - m_songCache.last().setMetaData(Qmmp::TITLE, value); - } - else if (m_songCache.isEmpty()) - continue; - else if (param == "artist") - m_songCache.last().setMetaData(Qmmp::ARTIST, value); - else if (param == "album") - m_songCache.last().setMetaData(Qmmp::ALBUM, value); - else if (param == "comment") - m_songCache.last().setMetaData(Qmmp::COMMENT, value); - else if (param == "genre") - m_songCache.last().setMetaData(Qmmp::GENRE, value); - else if (param == "year") - m_songCache.last().setMetaData(Qmmp::YEAR, value); - else if (param == "track") - m_songCache.last().setMetaData(Qmmp::TRACK, value); - else if (param == "length") - m_songCache.last().setLength(value.toInt()); - else if (param == "time") - m_songCache.last().setTimeStamp(value.toUInt()); - } - file.close(); -} - -SongInfo::SongInfo() -{ - m_length = 0; -} - -SongInfo::SongInfo(const QMap metadata, qint64 length) -{ - m_metadata = metadata; - m_length = length; -} - -SongInfo::SongInfo(const SongInfo &other) -{ - m_metadata = other.metaData(); - m_length = other.length(); - m_start_ts = other.timeStamp(); -} - -SongInfo::~SongInfo() -{} - -void SongInfo::operator=(const SongInfo &info) -{ - m_metadata = info.metaData(); - m_length = info.length(); - m_start_ts = info.timeStamp(); -} - -bool SongInfo::operator==(const SongInfo &info) -{ - return (m_metadata == info.metaData()) && (m_length == info.length()) && (m_start_ts == info.timeStamp()); -} - -bool SongInfo::operator!=(const SongInfo &info) -{ - return !operator==(info); -} - -void SongInfo::setMetaData(const QMap metadata) -{ - m_metadata = metadata; -} - -void SongInfo::setMetaData(Qmmp::MetaData key, const QString &value) -{ - m_metadata.insert(key, value); -} - -void SongInfo::setLength(qint64 l) -{ - m_length = l; -} - -const QMap SongInfo::metaData() const -{ - return m_metadata; -} - -const QString SongInfo::metaData(Qmmp::MetaData key) const -{ - return m_metadata.value(key); -} - -qint64 SongInfo::length () const -{ - return m_length; -} - -void SongInfo::clear() -{ - m_metadata.clear(); - m_length = 0; -} - -void SongInfo::setTimeStamp(uint ts) -{ - m_start_ts = ts; -} - -uint SongInfo::timeStamp() const -{ - return m_start_ts; -} diff --git a/src/plugins/General/scrobbler/scrobbler.h b/src/plugins/General/scrobbler/scrobbler.h deleted file mode 100644 index 34024c3c2..000000000 --- a/src/plugins/General/scrobbler/scrobbler.h +++ /dev/null @@ -1,118 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2008-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * 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, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ -#ifndef SCROBBLER_H -#define SCROBBLER_H - -#include -#include -#include - -class QNetworkAccessManager; -class QNetworkReply; -class QTime; -class SoundCore; - -/** - @author Ilya Kotov -*/ -class SongInfo -{ -public: - SongInfo(); - SongInfo(const QMap metadata, qint64 length = 0); - SongInfo(const SongInfo &other); - - ~SongInfo(); - - void operator=(const SongInfo &info); - bool operator==(const SongInfo &info); - bool operator!=(const SongInfo &info); - - void setMetaData(const QMap metadata); - void setMetaData(Qmmp::MetaData key, const QString &value); - void setLength(qint64 l); - const QMap metaData() const; - const QString metaData(Qmmp::MetaData) const; - qint64 length () const; - void clear(); - void setTimeStamp(uint ts); - uint timeStamp() const; - -private: - QMap m_metadata; - qint64 m_length; - uint m_start_ts; - -}; - -/** - @author Ilya Kotov -*/ -class Scrobbler : public QObject -{ - Q_OBJECT -public: - Scrobbler(const QString &url, - const QString &login, - const QString &passw, - const QString &name, - QObject *parent = 0); - - ~Scrobbler(); - -private slots: - void setState(Qmmp::State state); - void updateMetaData(); - void processResponse(QNetworkReply *reply); - void setupProxy(); - void handshake(); - -private: - enum { MIN_SONG_LENGTH = 30 }; - - void submit(); - void sendNotification(const SongInfo &info); - bool isReady(); - void writeCache(); - void readCache(); - uint m_start_ts; - SongInfo m_song; - QNetworkAccessManager *m_http; - SoundCore *m_core; - QNetworkReply *m_handshakeReply; - QNetworkReply *m_submitReply; - QNetworkReply *m_notificationReply; - QTime* m_time; - QString m_login; - QString m_passw; - QString m_submitUrl; - QString m_nowPlayingUrl; - QString m_session; - QString m_server, m_name; - QList m_songCache; - QByteArray m_ua; - int m_submitedSongs; - int m_failure_count; - int m_handshake_count; - bool m_disabled; - -}; - -#endif diff --git a/src/plugins/General/scrobbler/scrobbler.pro b/src/plugins/General/scrobbler/scrobbler.pro index 6b8eba577..a779ad5fe 100644 --- a/src/plugins/General/scrobbler/scrobbler.pro +++ b/src/plugins/General/scrobbler/scrobbler.pro @@ -28,16 +28,18 @@ unix { INSTALLS += target } HEADERS += scrobblerfactory.h \ - scrobbler.h \ settingsdialog.h \ scrobblerhandler.h \ - scrobbler2.h + lastfmscrobbler.h \ + librefmscrobbler.h \ + scrobblercache.h win32:HEADERS += ../../../../src/qmmpui/general.h SOURCES += scrobblerfactory.cpp \ - scrobbler.cpp \ settingsdialog.cpp \ scrobblerhandler.cpp \ - scrobbler2.cpp + lastfmscrobbler.cpp \ + librefmscrobbler.cpp \ + scrobblercache.cpp QT += network INCLUDEPATH += ../../../ unix:LIBS += -lqmmpui \ @@ -45,3 +47,15 @@ unix:LIBS += -lqmmpui \ win32:LIBS += -lqmmpui0 \ -lqmmp0 FORMS += settingsdialog.ui + + + + + + + + + + + + diff --git a/src/plugins/General/scrobbler/scrobbler2.cpp b/src/plugins/General/scrobbler/scrobbler2.cpp deleted file mode 100644 index c240f3790..000000000 --- a/src/plugins/General/scrobbler/scrobbler2.cpp +++ /dev/null @@ -1,541 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2010-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * 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, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "scrobbler2.h" - -#define API_KEY "d71c6f01b2ea562d7042bd5f5970041f" -#define SECRET "32d47bc0010473d40e1d38bdcff20968" - -Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) : QObject(parent) -{ - m_getTokenReply = 0; - m_getSessionReply = 0; - m_notificationReply = 0; - m_submitedSongs = 0; - m_submitReply = 0; - m_state = Qmmp::Stopped; - m_server = url; - m_name = name; - m_time = new QTime(); - m_ua = QString("qmmp-plugins/%1").arg(Qmmp::strVersion().toLower()).toAscii(); - m_http = new QNetworkAccessManager(this); - m_core = SoundCore::instance(); - QSettings settings(Qmmp::configFile(), QSettings::IniFormat); - m_session = settings.value("Scrobbler/lastfm_session").toString(); - - connect(m_http, SIGNAL(finished (QNetworkReply *)), SLOT(processResponse(QNetworkReply *))); - connect(QmmpSettings::instance(), SIGNAL(networkSettingsChanged()), SLOT(setupProxy())); - connect (m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); - connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); - - setupProxy(); - readCache(); - - if(m_session.isEmpty()) - getToken(); - else - { - submit(); - if(m_core->state() == Qmmp::Playing) - { - setState(Qmmp::Playing); - updateMetaData(); - } - } -} - - -Scrobbler2::~Scrobbler2() -{ - delete m_time; - writeCache(); -} - -void Scrobbler2::setState(Qmmp::State state) -{ - m_state = state; - switch ((uint) state) - { - case Qmmp::Playing: - m_start_ts = QDateTime::currentDateTime().toTime_t(); - m_time->restart(); - break; - case Qmmp::Stopped: - if (!m_song.metaData().isEmpty() - && ((m_time->elapsed ()/1000 > 240) || (m_time->elapsed ()/1000 > int(m_song.length()/2))) - && (m_song.length() > MIN_SONG_LENGTH)) - { - m_song.setTimeStamp(m_start_ts); - m_songCache << m_song; - writeCache(); - } - - m_song.clear(); - if (m_songCache.isEmpty()) - break; - - if (!m_session.isEmpty() && !m_submitReply) - submit(); - break; - default: - ; - } -} - -void Scrobbler2::updateMetaData() -{ - QMap metadata = m_core->metaData(); - if(m_state != Qmmp::Playing || m_core->totalTime() <= 0) //skip stream - return; - if(metadata.value(Qmmp::TITLE).isEmpty() || metadata.value(Qmmp::ARTIST).isEmpty()) //skip empty tags - return; - if(m_notificationReply && m_submitReply) //used - return; - - m_song = SongInfo(metadata, m_core->totalTime()/1000); - sendNotification(m_song); -} - -void Scrobbler2::processResponse(QNetworkReply *reply) -{ - if (reply->error() != QNetworkReply::NoError) - { - qWarning("Scrobbler2[%s]: http error: %s", qPrintable(m_name), qPrintable(reply->errorString())); - } - - Scrobbler2Response response; - QStringList tags; - QXmlStreamReader reader(reply); - while(!reader.atEnd()) - { - reader.readNext(); - if(reader.isStartElement()) - { - tags << reader.name().toString(); - if(tags.last() == "lfm") - response.status = reader.attributes().value("status").toString(); - else if(tags.last() == "error") - response.code = reader.attributes().value("code").toString(); - } - else if(reader.isCharacters() && !reader.isWhitespace()) - { - if(tags.last() == "token") - response.token = reader.text().toString(); - else if(tags.last() == "error") - response.error = reader.text().toString(); - if(tags.count() >= 2 && tags.at(tags.count() - 2) == "session") - { - if(tags.last() == "key") - response.key = reader.text().toString(); - else if(tags.last() == "name") - response.name = reader.text().toString(); - else if(tags.last() == "subscriber") - response.subscriber = reader.text().toString(); - } - } - else if(reader.isEndElement()) - { - tags.takeLast(); - } - } - - QString error_code; - if(response.status != "ok" && !response.status.isEmpty()) - { - if(!response.error.isEmpty()) - { - qWarning("Scrobbler2[%s]: status=%s, %s-%s", - qPrintable(m_name), - qPrintable(response.status), - qPrintable(response.code), qPrintable(response.error)); - error_code = response.code; - } - else - qWarning("Scrobbler2[%s]: invalid content", qPrintable(m_name)); - } - - if (reply == m_getTokenReply) - { - m_getTokenReply = 0; - if(response.status == "ok") - { - m_token = response.token; - qDebug("Scrobbler2[%s]: token: %s", qPrintable(m_name), qPrintable(m_token)); - QDesktopServices::openUrl("http://www.last.fm/api/auth/?api_key="API_KEY"&token="+m_token); - QTimer::singleShot(120000, this, SLOT(getSession())); //2 minutes - } - else if(error_code == "8" || error_code == "7" || error_code == "11" || error_code.isEmpty()) - { - m_token.clear(); - QTimer::singleShot(120000, this, SLOT(getToken())); // wait 2 minutes and try again - } - else - { - m_token.clear(); - qWarning("Scrobbler2[%s]: service returned unrecoverable error, scrobbling disabled", - qPrintable(m_name)); - } - } - else if(reply == m_getSessionReply) - { - m_getSessionReply = 0; - m_session.clear(); - if(response.status == "ok") - { - m_session = response.key; - qDebug("Scrobbler2[%s]: name: %s", qPrintable(m_name),qPrintable(response.name)); - qDebug("Scrobbler2[%s]: key: %s", qPrintable(m_name), qPrintable(m_session)); - qDebug("Scrobbler2[%s]: subscriber: %s",qPrintable(m_name), qPrintable(response.subscriber)); - QSettings settings(Qmmp::configFile(), QSettings::IniFormat); - settings.setValue("Scrobbler/lastfm_session", m_session); - submit(); - } - else if(error_code == "4" || error_code == "15") //invalid token - { - m_token.clear(); - getToken(); - } - else if(error_code == "11") //service offline - { - QTimer::singleShot(120000, this, SLOT(getSession())); - } - else if(error_code == "14") // unauthorized token - { - QDesktopServices::openUrl("http://www.last.fm/api/auth/?api_key="API_KEY"&token="+m_token); - QTimer::singleShot(120000, this, SLOT(getSession())); //2 minutes - } - else if (error_code.isEmpty()) //network error - { - QTimer::singleShot(120000, this, SLOT(getSession())); - } - else - { - m_token.clear(); - qWarning("Scrobbler2[%s]: service returned unrecoverable error, scrobbling disabled", - qPrintable(m_name)); - } - } - else if (reply == m_submitReply) - { - m_submitReply = 0; - if (response.status == "ok") - { - qDebug("Scrobbler2[%s]: submited %d song(s)",qPrintable(m_name), m_submitedSongs); - while (m_submitedSongs) - { - m_submitedSongs--; - m_songCache.removeFirst (); - } - if (!m_songCache.isEmpty()) //submit remaining songs - { - submit(); - } - else - { - writeCache(); // update the cache file to reflect the empty cache - updateMetaData(); - } - } - else if(error_code == "9") //invalid session key - { - m_session.clear(); - getToken(); - } - else if(error_code == "11" || error_code == "16" || error_code.isEmpty()) //unavailable - { - QTimer::singleShot(120000, this, SLOT(submit())); - } - else - { - m_session.clear(); - qWarning("Scrobbler2[%s]: service returned unrecoverable error, scrobbling disabled", - qPrintable(m_name)); - } - } - else if (reply == m_notificationReply) - { - m_notificationReply = 0; - if(response.status == "ok") - { - qDebug("Scrobbler2[%s]: Now-Playing notification done", qPrintable(m_name)); - } - else if(error_code == "9") //invalid session key - { - m_session.clear(); - getToken(); - } - } - reply->deleteLater(); -} - -void Scrobbler2::setupProxy() -{ - QmmpSettings *gs = QmmpSettings::instance(); - if (gs->isProxyEnabled()) - { - QNetworkProxy proxy(QNetworkProxy::HttpProxy, gs->proxy().host(), gs->proxy().port()); - if(gs->useProxyAuth()) - { - proxy.setUser(gs->proxy().userName()); - proxy.setPassword(gs->proxy().password()); - } - m_http->setProxy(proxy); - } - else - m_http->setProxy(QNetworkProxy::NoProxy); -} - -void Scrobbler2::getToken() -{ - qDebug("Scrobbler2[%s]: new token request", qPrintable(m_name)); - m_session.clear(); - QUrl url(QString("http://") + m_server + "/?"); - url.setPort(80); - url.addQueryItem("method", "auth.getToken"); - url.addQueryItem("api_key", API_KEY); - - QByteArray data; - data.append("api_key"API_KEY); - data.append("methodauth.getToken"); - data.append(SECRET); - url.addQueryItem("api_sig", QCryptographicHash::hash(data,QCryptographicHash::Md5).toHex()); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - m_getTokenReply = m_http->get(request); -} - -void Scrobbler2::getSession() -{ - qDebug("Scrobbler2[%s]: new session request", qPrintable(m_name)); - QUrl url(QString("http://") + m_server + "/?"); - url.setPort(80); - url.addQueryItem("api_key", API_KEY); - url.addQueryItem("method", "auth.getSession"); - url.addQueryItem("token", m_token); - - QByteArray data; - data.append("api_key"API_KEY); - data.append("methodauth.getSession"); - data.append("token" + m_token.toUtf8()); - data.append(SECRET); - url.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host",url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - m_getSessionReply = m_http->get(request); -} - -void Scrobbler2::submit() -{ - if (m_songCache.isEmpty() || m_session.isEmpty() || m_submitReply) - return; - - qDebug("Scrobbler2[%s]: submit request", qPrintable(m_name)); - m_submitedSongs = qMin(m_songCache.size(),25); - - QMap params; - for (int i = 0; i < m_submitedSongs; ++i) - { - SongInfo info = m_songCache[i]; - params.insert(QString("track[%1]").arg(i),info.metaData(Qmmp::TITLE)); - params.insert(QString("timestamp[%1]").arg(i),QString("%1").arg(info.timeStamp())); - params.insert(QString("artist[%1]").arg(i),info.metaData(Qmmp::ARTIST)); - params.insert(QString("album[%1]").arg(i),info.metaData(Qmmp::ALBUM)); - params.insert(QString("trackNumber[%1]").arg(i),info.metaData(Qmmp::TRACK)); - params.insert(QString("duration[%1]").arg(i),QString("%1").arg(info.length())); - } - params.insert("api_key", API_KEY); - params.insert("method", "track.scrobble"); - params.insert("sk", m_session); - - QStringList keys = params.keys(); - foreach (QString key, keys) //removes empty keys - { - if(params.value(key).isEmpty() || params.value(key) == "0") - params.remove(key); - } - - QUrl url(QString("http://") + m_server + "/"); - url.setPort(80); - - QUrl body(""); - QByteArray data; - foreach (QString key, params.keys()) - { - body.addQueryItem(key, params.value(key)); - data.append(key.toUtf8() + params.value(key).toUtf8()); - } - data.append(SECRET); - body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - QByteArray bodyData = body.toEncoded().remove(0,1); - bodyData.replace("+", QUrl::toPercentEncoding("+")); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); - m_submitReply = m_http->post(request, bodyData); -} - -void Scrobbler2::sendNotification(const SongInfo &info) -{ - if(m_session.isEmpty()) - return; - qDebug("Scrobbler2[%s]: sending notification", qPrintable(m_name)); - - QMap params; - params.insert("track", info.metaData(Qmmp::TITLE)); - params.insert("artist", info.metaData(Qmmp::ARTIST)); - if(!info.metaData(Qmmp::ALBUM).isEmpty()) - params.insert("album", info.metaData(Qmmp::ALBUM)); - if(!info.metaData(Qmmp::TRACK).isEmpty()) - params.insert("trackNumber", info.metaData(Qmmp::TRACK)); - params.insert("duration", QString("%1").arg(info.length())); - params.insert("api_key", API_KEY); - params.insert("method", "track.updateNowPlaying"); - params.insert("sk", m_session); - - foreach (QString key, params) //removes empty keys - { - if(params.value(key).isEmpty()) - params.remove(key); - } - - QUrl url(QString("http://") + m_server + "/"); - url.setPort(80); - - QUrl body(""); - QByteArray data; - foreach (QString key, params.keys()) - { - body.addQueryItem(key, params.value(key)); - data.append(key.toUtf8() + params.value(key).toUtf8()); - } - data.append(SECRET); - body.addQueryItem("api_sig", QCryptographicHash::hash(data, QCryptographicHash::Md5).toHex()); - QByteArray bodyData = body.toEncoded().remove(0,1); - bodyData.replace("+", QUrl::toPercentEncoding("+")); - - QNetworkRequest request(url); - request.setRawHeader("User-Agent", m_ua); - request.setRawHeader("Host", url.host().toAscii()); - request.setRawHeader("Accept", "*/*"); - request.setRawHeader("Content-Type", "application/x-www-form-urlencoded"); - request.setHeader(QNetworkRequest::ContentLengthHeader, bodyData.size()); - m_notificationReply = m_http->post(request, bodyData); -} - -void Scrobbler2::writeCache() -{ - QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); - if (m_songCache.isEmpty()) - { - file.remove(); - return; - } - if(!file.open(QIODevice::WriteOnly)) - { - qWarning("Scrobbler2[%s]: error %d: %s", qPrintable(m_name), - file.error(), qPrintable(file.errorString())); - return; - } - foreach(SongInfo m, m_songCache) - { - file.write(QString("title=%1").arg(m.metaData(Qmmp::TITLE)).toUtf8() +"\n"); - file.write(QString("artist=%1").arg(m.metaData(Qmmp::ARTIST)).toUtf8() +"\n"); - file.write(QString("album=%1").arg(m.metaData(Qmmp::ALBUM)).toUtf8() +"\n"); - file.write(QString("comment=%1").arg(m.metaData(Qmmp::COMMENT)).toUtf8() +"\n"); - file.write(QString("genre=%1").arg(m.metaData(Qmmp::GENRE)).toUtf8() +"\n"); - file.write(QString("year=%1").arg(m.metaData(Qmmp::YEAR)).toUtf8() +"\n"); - file.write(QString("track=%1").arg(m.metaData(Qmmp::TRACK)).toUtf8() +"\n"); - file.write(QString("length=%1").arg(m.length()).toUtf8() +"\n"); - file.write(QString("time=%1").arg(m.timeStamp()).toUtf8() +"\n"); - } - file.close(); -} - -void Scrobbler2::readCache() -{ - int s = 0; - QString line, param, value; - QFile file(QDir::homePath() +"/.qmmp/scrobbler_" + m_name + ".cache"); - - if(!file.open(QIODevice::ReadOnly)) - return; - - while (!file.atEnd()) - { - line = QString::fromUtf8(file.readLine()).trimmed(); - if ((s = line.indexOf("=")) < 0) - continue; - - param = line.left(s); - value = line.right(line.size() - s - 1); - - if (param == "title") - { - m_songCache << SongInfo(); - m_songCache.last().setMetaData(Qmmp::TITLE, value); - } - else if (m_songCache.isEmpty()) - continue; - else if (param == "artist") - m_songCache.last().setMetaData(Qmmp::ARTIST, value); - else if (param == "album") - m_songCache.last().setMetaData(Qmmp::ALBUM, value); - else if (param == "comment") - m_songCache.last().setMetaData(Qmmp::COMMENT, value); - else if (param == "genre") - m_songCache.last().setMetaData(Qmmp::GENRE, value); - else if (param == "year") - m_songCache.last().setMetaData(Qmmp::YEAR, value); - else if (param == "track") - m_songCache.last().setMetaData(Qmmp::TRACK, value); - else if (param == "length") - m_songCache.last().setLength(value.toInt()); - else if (param == "time") - m_songCache.last().setTimeStamp(value.toUInt()); - } - file.close(); -} diff --git a/src/plugins/General/scrobbler/scrobbler2.h b/src/plugins/General/scrobbler/scrobbler2.h deleted file mode 100644 index 9bd0aeef4..000000000 --- a/src/plugins/General/scrobbler/scrobbler2.h +++ /dev/null @@ -1,86 +0,0 @@ -/*************************************************************************** - * Copyright (C) 2010-2013 by Ilya Kotov * - * forkotov02@hotmail.ru * - * * - * 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, write to the * - * Free Software Foundation, Inc., * - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * - ***************************************************************************/ -#ifndef SCROBBLER2_H -#define SCROBBLER2_H - -#include -#include -#include "scrobbler.h" - -class QNetworkAccessManager; -class QNetworkReply; -class QTime; -class SoundCore; - -/** - @author Ilya Kotov -*/ -struct Scrobbler2Response -{ - QString status; - QString token; - QString code; - QString error; - QString key; - QString name; - QString subscriber; -}; - -/** - @author Ilya Kotov -*/ -class Scrobbler2 : public QObject -{ - Q_OBJECT -public: - Scrobbler2(const QString &url, const QString &name, QObject *parent = 0); - ~Scrobbler2(); - -private slots: - void setState(Qmmp::State state); - void updateMetaData(); - void processResponse(QNetworkReply *reply); - void setupProxy(); - void getToken(); - void getSession(); - void submit(); - -private: - enum { MIN_SONG_LENGTH = 30 }; - - void sendNotification(const SongInfo &info); - void writeCache(); - void readCache(); - uint m_start_ts; - SongInfo m_song; - QNetworkAccessManager *m_http; - Qmmp::State m_state; - SoundCore *m_core; - QList m_songCache; - QByteArray m_ua; - QTime* m_time; - int m_submitedSongs; - QNetworkReply *m_getTokenReply, *m_getSessionReply; - QNetworkReply *m_submitReply, *m_notificationReply; - QString m_server, m_name; - QString m_token, m_session; -}; - -#endif diff --git a/src/plugins/General/scrobbler/scrobblercache.cpp b/src/plugins/General/scrobbler/scrobblercache.cpp new file mode 100644 index 000000000..054b6ba75 --- /dev/null +++ b/src/plugins/General/scrobbler/scrobblercache.cpp @@ -0,0 +1,187 @@ +/*************************************************************************** + * Copyright (C) 2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include +#include "scrobblercache.h" + +SongInfo::SongInfo() +{ + m_length = 0; +} + +SongInfo::SongInfo(const QMap metadata, qint64 length) +{ + m_metadata = metadata; + m_length = length; +} + +SongInfo::SongInfo(const SongInfo &other) +{ + m_metadata = other.metaData(); + m_length = other.length(); + m_start_ts = other.timeStamp(); +} + +SongInfo::~SongInfo() +{} + +void SongInfo::operator=(const SongInfo &info) +{ + m_metadata = info.metaData(); + m_length = info.length(); + m_start_ts = info.timeStamp(); +} + +bool SongInfo::operator==(const SongInfo &info) +{ + return (m_metadata == info.metaData()) && (m_length == info.length()) && (m_start_ts == info.timeStamp()); +} + +bool SongInfo::operator!=(const SongInfo &info) +{ + return !operator==(info); +} + +void SongInfo::setMetaData(const QMap metadata) +{ + m_metadata = metadata; +} + +void SongInfo::setMetaData(Qmmp::MetaData key, const QString &value) +{ + m_metadata.insert(key, value); +} + +void SongInfo::setLength(qint64 l) +{ + m_length = l; +} + +const QMap SongInfo::metaData() const +{ + return m_metadata; +} + +const QString SongInfo::metaData(Qmmp::MetaData key) const +{ + return m_metadata.value(key); +} + +qint64 SongInfo::length () const +{ + return m_length; +} + +void SongInfo::clear() +{ + m_metadata.clear(); + m_length = 0; +} + +void SongInfo::setTimeStamp(uint ts) +{ + m_start_ts = ts; +} + +uint SongInfo::timeStamp() const +{ + return m_start_ts; +} + +ScrobblerCache::ScrobblerCache(const QString &filePath) +{ + m_filePath = filePath; +} + +QList ScrobblerCache::load() +{ + QList songs; + int s = 0; + QString line, param, value; + QFile file(m_filePath); + + if(!file.open(QIODevice::ReadOnly)) + return QList(); + + while (!file.atEnd()) + { + line = QString::fromUtf8(file.readLine()).trimmed(); + if ((s = line.indexOf("=")) < 0) + continue; + + param = line.left(s); + value = line.right(line.size() - s - 1); + + if (param == "title") + { + songs << SongInfo(); + songs.last().setMetaData(Qmmp::TITLE, value); + } + else if (songs.isEmpty()) + continue; + else if (param == "artist") + songs.last().setMetaData(Qmmp::ARTIST, value); + else if (param == "album") + songs.last().setMetaData(Qmmp::ALBUM, value); + else if (param == "comment") + songs.last().setMetaData(Qmmp::COMMENT, value); + else if (param == "genre") + songs.last().setMetaData(Qmmp::GENRE, value); + else if (param == "year") + songs.last().setMetaData(Qmmp::YEAR, value); + else if (param == "track") + songs.last().setMetaData(Qmmp::TRACK, value); + else if (param == "length") + songs.last().setLength(value.toInt()); + else if (param == "time") + songs.last().setTimeStamp(value.toUInt()); + } + file.close(); + return songs; +} + +void ScrobblerCache::save(const QList &songs) +{ + QFile file(m_filePath); + if (songs.isEmpty()) + { + file.remove(); + return; + } + if(!file.open(QIODevice::WriteOnly)) + { + qWarning("ScrobblerCach: unable to save file %s", qPrintable(m_filePath)); + qWarning("ScrobblerCach: error %d: %s", file.error(), qPrintable(file.errorString())); + return; + } + foreach(SongInfo m, songs) + { + file.write(QString("title=%1").arg(m.metaData(Qmmp::TITLE)).toUtf8() +"\n"); + file.write(QString("artist=%1").arg(m.metaData(Qmmp::ARTIST)).toUtf8() +"\n"); + file.write(QString("album=%1").arg(m.metaData(Qmmp::ALBUM)).toUtf8() +"\n"); + file.write(QString("comment=%1").arg(m.metaData(Qmmp::COMMENT)).toUtf8() +"\n"); + file.write(QString("genre=%1").arg(m.metaData(Qmmp::GENRE)).toUtf8() +"\n"); + file.write(QString("year=%1").arg(m.metaData(Qmmp::YEAR)).toUtf8() +"\n"); + file.write(QString("track=%1").arg(m.metaData(Qmmp::TRACK)).toUtf8() +"\n"); + file.write(QString("length=%1").arg(m.length()).toUtf8() +"\n"); + file.write(QString("time=%1").arg(m.timeStamp()).toUtf8() +"\n"); + } + file.close(); +} diff --git a/src/plugins/General/scrobbler/scrobblercache.h b/src/plugins/General/scrobbler/scrobblercache.h new file mode 100644 index 000000000..40ac5fff6 --- /dev/null +++ b/src/plugins/General/scrobbler/scrobblercache.h @@ -0,0 +1,78 @@ +/*************************************************************************** + * Copyright (C) 2013 by Ilya Kotov * + * forkotov02@hotmail.ru * + * * + * 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, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef SCROBBLERCACHE_H +#define SCROBBLERCACHE_H + +#include +#include +#include + + +/** + @author Ilya Kotov +*/ +class SongInfo +{ +public: + SongInfo(); + SongInfo(const QMap metadata, qint64 length = 0); + SongInfo(const SongInfo &other); + + ~SongInfo(); + + void operator=(const SongInfo &info); + bool operator==(const SongInfo &info); + bool operator!=(const SongInfo &info); + + void setMetaData(const QMap metadata); + void setMetaData(Qmmp::MetaData key, const QString &value); + void setLength(qint64 l); + const QMap metaData() const; + const QString metaData(Qmmp::MetaData) const; + qint64 length () const; + void clear(); + void setTimeStamp(uint ts); + uint timeStamp() const; + +private: + QMap m_metadata; + qint64 m_length; + uint m_start_ts; + +}; + +/** + @author Ilya Kotov +*/ +class ScrobblerCache +{ +public: + explicit ScrobblerCache(const QString &filePath); + + QList load(); + void save(const QList &songs); + +private: + QString m_filePath; + +}; + +#endif // SCROBBLERCACHE_H diff --git a/src/plugins/General/scrobbler/scrobblerhandler.cpp b/src/plugins/General/scrobbler/scrobblerhandler.cpp index ed343f23b..17f375f01 100644 --- a/src/plugins/General/scrobbler/scrobblerhandler.cpp +++ b/src/plugins/General/scrobbler/scrobblerhandler.cpp @@ -18,12 +18,9 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#define SCROBBLER_LIBREFM_URL "turtle.libre.fm" -#define SCROBBLER_LASTFM_URL "ws.audioscrobbler.com/2.0" - #include -#include "scrobbler.h" -#include "scrobbler2.h" +#include "lastfmscrobbler.h" +#include "librefmscrobbler.h" #include "scrobblerhandler.h" ScrobblerHandler::ScrobblerHandler(QObject *parent) : QObject(parent) @@ -32,12 +29,12 @@ ScrobblerHandler::ScrobblerHandler(QObject *parent) : QObject(parent) settings.beginGroup("Scrobbler"); if(settings.value("use_lastfm", false).toBool()) { - new Scrobbler2(SCROBBLER_LASTFM_URL, "lastfm", this); + new LastfmScrobbler(this); } if(settings.value("use_librefm", false).toBool()) { - new Scrobbler(SCROBBLER_LIBREFM_URL, settings.value("librefm_login").toString(), - settings.value("librefm_password").toString(), "librefm", this); + new LibrefmScrobbler(settings.value("librefm_login").toString(), + settings.value("librefm_password").toString(), this); } settings.endGroup(); -- cgit v1.2.3-13-gbd6f