diff options
| author | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2013-01-23 18:03:45 +0000 |
|---|---|---|
| committer | trialuser02 <trialuser02@90c681e8-e032-0410-971d-27865f9a5e38> | 2013-01-23 18:03:45 +0000 |
| commit | 7164404b2e0d428b4f0083255d0ed140740b7146 (patch) | |
| tree | 47e58d0907096538d26aaa11ccb9a510adc34136 /src | |
| parent | 1988a82eb1f120af57a9d2d77ca448a7bc459615 (diff) | |
| download | qmmp-7164404b2e0d428b4f0083255d0ed140740b7146.tar.gz qmmp-7164404b2e0d428b4f0083255d0ed140740b7146.tar.bz2 qmmp-7164404b2e0d428b4f0083255d0ed140740b7146.zip | |
scrobbler: refactoring again
git-svn-id: http://svn.code.sf.net/p/qmmp-dev/code/trunk/qmmp@3187 90c681e8-e032-0410-971d-27865f9a5e38
Diffstat (limited to 'src')
| -rw-r--r-- | src/plugins/General/scrobbler/CMakeLists.txt | 15 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/lastfmscrobbler.cpp (renamed from src/plugins/General/scrobbler/scrobbler2.cpp) | 177 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/lastfmscrobbler.h (renamed from src/plugins/General/scrobbler/scrobbler2.h) | 29 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/librefmscrobbler.cpp (renamed from src/plugins/General/scrobbler/scrobbler.cpp) | 269 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/librefmscrobbler.h (renamed from src/plugins/General/scrobbler/scrobbler.h) | 67 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobbler.pro | 22 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobblercache.cpp | 187 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobblercache.h | 78 | ||||
| -rw-r--r-- | src/plugins/General/scrobbler/scrobblerhandler.cpp | 13 |
9 files changed, 434 insertions, 423 deletions
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/scrobbler2.cpp b/src/plugins/General/scrobbler/lastfmscrobbler.cpp index c240f3790..be1086a8d 100644 --- a/src/plugins/General/scrobbler/scrobbler2.cpp +++ b/src/plugins/General/scrobbler/lastfmscrobbler.cpp @@ -35,12 +35,14 @@ #include <qmmp/soundcore.h> #include <qmmp/qmmpsettings.h> #include <qmmp/qmmp.h> -#include "scrobbler2.h" +#include "lastfmscrobbler.h" +#define SCROBBLER_LASTFM_URL "http://ws.audioscrobbler.com/2.0/" #define API_KEY "d71c6f01b2ea562d7042bd5f5970041f" #define SECRET "32d47bc0010473d40e1d38bdcff20968" -Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) : QObject(parent) + +LastfmScrobbler::LastfmScrobbler(QObject *parent) : QObject(parent) { m_getTokenReply = 0; m_getSessionReply = 0; @@ -48,9 +50,8 @@ Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) m_submitedSongs = 0; m_submitReply = 0; m_state = Qmmp::Stopped; - m_server = url; - m_name = name; 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(); @@ -59,11 +60,11 @@ Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) 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))); + connect(m_core, SIGNAL(metaDataChanged()), SLOT(updateMetaData())); + connect(m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); setupProxy(); - readCache(); + m_cachedSongs = m_cache->load(); if(m_session.isEmpty()) getToken(); @@ -78,14 +79,15 @@ Scrobbler2::Scrobbler2(const QString &url, const QString &name, QObject *parent) } } - -Scrobbler2::~Scrobbler2() +LastfmScrobbler::~LastfmScrobbler() { + m_cache->save(m_cachedSongs); delete m_time; - writeCache(); + delete m_cache; + } -void Scrobbler2::setState(Qmmp::State state) +void LastfmScrobbler::setState(Qmmp::State state) { m_state = state; switch ((uint) state) @@ -100,12 +102,12 @@ void Scrobbler2::setState(Qmmp::State state) && (m_song.length() > MIN_SONG_LENGTH)) { m_song.setTimeStamp(m_start_ts); - m_songCache << m_song; - writeCache(); + m_cachedSongs << m_song; + m_cache->save(m_cachedSongs); } m_song.clear(); - if (m_songCache.isEmpty()) + if (m_cachedSongs.isEmpty()) break; if (!m_session.isEmpty() && !m_submitReply) @@ -116,7 +118,7 @@ void Scrobbler2::setState(Qmmp::State state) } } -void Scrobbler2::updateMetaData() +void LastfmScrobbler::updateMetaData() { QMap <Qmmp::MetaData, QString> metadata = m_core->metaData(); if(m_state != Qmmp::Playing || m_core->totalTime() <= 0) //skip stream @@ -130,14 +132,14 @@ void Scrobbler2::updateMetaData() sendNotification(m_song); } -void Scrobbler2::processResponse(QNetworkReply *reply) +void LastfmScrobbler::processResponse(QNetworkReply *reply) { if (reply->error() != QNetworkReply::NoError) { - qWarning("Scrobbler2[%s]: http error: %s", qPrintable(m_name), qPrintable(reply->errorString())); + qWarning("LastfmScrobbler: http error: %s", qPrintable(reply->errorString())); } - Scrobbler2Response response; + LastfmResponse response; QStringList tags; QXmlStreamReader reader(reply); while(!reader.atEnd()) @@ -178,14 +180,12 @@ void Scrobbler2::processResponse(QNetworkReply *reply) { if(!response.error.isEmpty()) { - qWarning("Scrobbler2[%s]: status=%s, %s-%s", - qPrintable(m_name), - qPrintable(response.status), + qWarning("LastfmScrobbler: status=%s, %s-%s", qPrintable(response.status), qPrintable(response.code), qPrintable(response.error)); error_code = response.code; } else - qWarning("Scrobbler2[%s]: invalid content", qPrintable(m_name)); + qWarning("LastfmScrobbler: invalid content"); } if (reply == m_getTokenReply) @@ -194,7 +194,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) if(response.status == "ok") { m_token = response.token; - qDebug("Scrobbler2[%s]: token: %s", qPrintable(m_name), qPrintable(m_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 } @@ -206,8 +206,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) else { m_token.clear(); - qWarning("Scrobbler2[%s]: service returned unrecoverable error, scrobbling disabled", - qPrintable(m_name)); + qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); } } else if(reply == m_getSessionReply) @@ -217,9 +216,9 @@ void Scrobbler2::processResponse(QNetworkReply *reply) 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)); + 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(); @@ -245,8 +244,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) else { m_token.clear(); - qWarning("Scrobbler2[%s]: service returned unrecoverable error, scrobbling disabled", - qPrintable(m_name)); + qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); } } else if (reply == m_submitReply) @@ -254,19 +252,19 @@ void Scrobbler2::processResponse(QNetworkReply *reply) m_submitReply = 0; if (response.status == "ok") { - qDebug("Scrobbler2[%s]: submited %d song(s)",qPrintable(m_name), m_submitedSongs); + qDebug("LastfmScrobbler: submited %d song(s)", m_submitedSongs); while (m_submitedSongs) { m_submitedSongs--; - m_songCache.removeFirst (); + m_cachedSongs.removeFirst (); } - if (!m_songCache.isEmpty()) //submit remaining songs + if (!m_cachedSongs.isEmpty()) //submit remaining songs { submit(); } else { - writeCache(); // update the cache file to reflect the empty cache + m_cache->save(m_cachedSongs); // update the cache file to reflect the empty cache updateMetaData(); } } @@ -282,8 +280,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) else { m_session.clear(); - qWarning("Scrobbler2[%s]: service returned unrecoverable error, scrobbling disabled", - qPrintable(m_name)); + qWarning("LastfmScrobbler: service returned unrecoverable error, scrobbling disabled"); } } else if (reply == m_notificationReply) @@ -291,7 +288,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) m_notificationReply = 0; if(response.status == "ok") { - qDebug("Scrobbler2[%s]: Now-Playing notification done", qPrintable(m_name)); + qDebug("LastfmScrobbler: Now-Playing notification done"); } else if(error_code == "9") //invalid session key { @@ -302,7 +299,7 @@ void Scrobbler2::processResponse(QNetworkReply *reply) reply->deleteLater(); } -void Scrobbler2::setupProxy() +void LastfmScrobbler::setupProxy() { QmmpSettings *gs = QmmpSettings::instance(); if (gs->isProxyEnabled()) @@ -319,11 +316,11 @@ void Scrobbler2::setupProxy() m_http->setProxy(QNetworkProxy::NoProxy); } -void Scrobbler2::getToken() +void LastfmScrobbler::getToken() { - qDebug("Scrobbler2[%s]: new token request", qPrintable(m_name)); + qDebug("LastfmScrobbler: new token request"); m_session.clear(); - QUrl url(QString("http://") + m_server + "/?"); + QUrl url(QString(SCROBBLER_LASTFM_URL) + "?"); url.setPort(80); url.addQueryItem("method", "auth.getToken"); url.addQueryItem("api_key", API_KEY); @@ -341,10 +338,10 @@ void Scrobbler2::getToken() m_getTokenReply = m_http->get(request); } -void Scrobbler2::getSession() +void LastfmScrobbler::getSession() { - qDebug("Scrobbler2[%s]: new session request", qPrintable(m_name)); - QUrl url(QString("http://") + m_server + "/?"); + 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"); @@ -364,18 +361,18 @@ void Scrobbler2::getSession() m_getSessionReply = m_http->get(request); } -void Scrobbler2::submit() +void LastfmScrobbler::submit() { - if (m_songCache.isEmpty() || m_session.isEmpty() || m_submitReply) + if (m_cachedSongs.isEmpty() || m_session.isEmpty() || m_submitReply) return; - qDebug("Scrobbler2[%s]: submit request", qPrintable(m_name)); - m_submitedSongs = qMin(m_songCache.size(),25); + qDebug("LastfmScrobbler: submit request"); + m_submitedSongs = qMin(m_cachedSongs.size(),25); QMap <QString, QString> params; for (int i = 0; i < m_submitedSongs; ++i) { - SongInfo info = m_songCache[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)); @@ -394,7 +391,7 @@ void Scrobbler2::submit() params.remove(key); } - QUrl url(QString("http://") + m_server + "/"); + QUrl url(SCROBBLER_LASTFM_URL); url.setPort(80); QUrl body(""); @@ -418,11 +415,11 @@ void Scrobbler2::submit() m_submitReply = m_http->post(request, bodyData); } -void Scrobbler2::sendNotification(const SongInfo &info) +void LastfmScrobbler::sendNotification(const SongInfo &info) { if(m_session.isEmpty()) return; - qDebug("Scrobbler2[%s]: sending notification", qPrintable(m_name)); + qDebug("LastfmScrobbler: sending notification"); QMap <QString, QString> params; params.insert("track", info.metaData(Qmmp::TITLE)); @@ -442,7 +439,7 @@ void Scrobbler2::sendNotification(const SongInfo &info) params.remove(key); } - QUrl url(QString("http://") + m_server + "/"); + QUrl url(SCROBBLER_LASTFM_URL); url.setPort(80); QUrl body(""); @@ -465,77 +462,3 @@ void Scrobbler2::sendNotification(const SongInfo &info) 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/lastfmscrobbler.h index 9bd0aeef4..313661e8b 100644 --- a/src/plugins/General/scrobbler/scrobbler2.h +++ b/src/plugins/General/scrobbler/lastfmscrobbler.h @@ -17,12 +17,13 @@ * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef SCROBBLER2_H -#define SCROBBLER2_H +#ifndef LASTFMSCROBBLER_H +#define LASTFMSCROBBLER_H #include <QMap> +#include <QObject> #include <qmmp/qmmp.h> -#include "scrobbler.h" +#include "scrobblercache.h" class QNetworkAccessManager; class QNetworkReply; @@ -32,7 +33,7 @@ class SoundCore; /** @author Ilya Kotov <forkotov02@hotmail.ru> */ -struct Scrobbler2Response +struct LastfmResponse { QString status; QString token; @@ -46,12 +47,12 @@ struct Scrobbler2Response /** @author Ilya Kotov <forkotov02@hotmail.ru> */ -class Scrobbler2 : public QObject +class LastfmScrobbler : public QObject { Q_OBJECT public: - Scrobbler2(const QString &url, const QString &name, QObject *parent = 0); - ~Scrobbler2(); + LastfmScrobbler(QObject *parent = 0); + ~LastfmScrobbler(); private slots: void setState(Qmmp::State state); @@ -66,21 +67,19 @@ 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 <SongInfo> m_songCache; + QList <SongInfo> m_cachedSongs; QByteArray m_ua; - QTime* m_time; int m_submitedSongs; + QString m_token, m_session; + QNetworkAccessManager *m_http; + SoundCore *m_core; QNetworkReply *m_getTokenReply, *m_getSessionReply; QNetworkReply *m_submitReply, *m_notificationReply; - QString m_server, m_name; - QString m_token, m_session; + QTime *m_time; + ScrobblerCache *m_cache; }; #endif diff --git a/src/plugins/General/scrobbler/scrobbler.cpp b/src/plugins/General/scrobbler/librefmscrobbler.cpp index d57b38783..c4e624697 100644 --- a/src/plugins/General/scrobbler/scrobbler.cpp +++ b/src/plugins/General/scrobbler/librefmscrobbler.cpp @@ -33,18 +33,16 @@ #include <qmmp/soundcore.h> #include <qmmp/qmmpsettings.h> #include <qmmp/qmmp.h> -#include "scrobbler.h" +#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/" -Scrobbler::Scrobbler(const QString &url, - const QString &login, - const QString &passw, - const QString &name, - QObject *parent) : QObject(parent) +LibrefmScrobbler::LibrefmScrobbler(const QString &login, const QString &passw, QObject *parent) + : QObject(parent) { m_failure_count = 0; m_handshake_count = 0; @@ -52,14 +50,13 @@ Scrobbler::Scrobbler(const QString &url, 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_cache = new ScrobblerCache(QDir::homePath() +"/.qmmp/scrobbler_librefm.cache"); m_http = new QNetworkAccessManager(this); m_time = new QTime(); @@ -69,20 +66,21 @@ Scrobbler::Scrobbler(const QString &url, connect (m_core, SIGNAL(stateChanged (Qmmp::State)), SLOT(setState(Qmmp::State))); setupProxy(); - readCache(); + m_cachedSongs = m_cache->load(); m_start_ts = QDateTime::currentDateTime().toTime_t(); handshake(); } -Scrobbler::~Scrobbler() +LibrefmScrobbler::~LibrefmScrobbler() { + m_cache->save(m_cachedSongs); delete m_time; - writeCache(); + delete m_cache; } -void Scrobbler::setState(Qmmp::State state) +void LibrefmScrobbler::setState(Qmmp::State state) { switch ((uint) state) { @@ -98,11 +96,11 @@ void Scrobbler::setState(Qmmp::State state) && (m_song.length() > MIN_SONG_LENGTH)) { m_song.setTimeStamp(m_start_ts); - m_songCache << m_song; - writeCache(); + m_cachedSongs << m_song; + m_cache->save(m_cachedSongs); } m_song.clear(); - if (m_songCache.isEmpty()) + if (m_cachedSongs.isEmpty()) break; if (isReady() && !m_submitReply) @@ -113,16 +111,13 @@ void Scrobbler::setState(Qmmp::State state) } } -void Scrobbler::updateMetaData() +void LibrefmScrobbler::updateMetaData() { QMap <Qmmp::MetaData, QString> 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("=")) + && m_core->totalTime() >= 0) //skip stream { metadata[Qmmp::ARTIST].replace("%", QUrl::toPercentEncoding("%")); //replace special symbols metadata[Qmmp::ALBUM].replace("%", QUrl::toPercentEncoding("%")); @@ -130,13 +125,16 @@ void Scrobbler::updateMetaData() 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 Scrobbler::processResponse(QNetworkReply *reply) +void LibrefmScrobbler::processResponse(QNetworkReply *reply) { QString data; if (reply->error() == QNetworkReply::NoError) @@ -161,35 +159,34 @@ void Scrobbler::processResponse(QNetworkReply *reply) QStringList strlist = data.split("\n"); if (!strlist[0].contains("OK") || strlist.size() < 4) { - qWarning("Scrobbler[%s]: handshake phase error", qPrintable(m_name)); + qWarning("LibrefmScrobbler: handshake phase error"); m_disabled = true; if(strlist[0].contains("BANNED")) - qWarning("Scrobbler[%s]: client has been banned", qPrintable(m_name)); + qWarning("LibrefmScrobbler: client has been banned"); else if(strlist[0].contains("BADAUTH")) - qWarning("Scrobbler[%s]: incorrect user/password", qPrintable(m_name)); + qWarning("LibrefmScrobbler: incorrect user/password"); else if(strlist[0].contains("BADTIME")) - qWarning("Scrobbler[%s]: incorrect system time", qPrintable(m_name)); + qWarning("LibrefmScrobbler: incorrect system time"); else { - qWarning("Scrobbler[%s]: service error: %s", qPrintable(m_name), qPrintable(strlist[0])); + 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("Scrobbler[%s]: waiting %d minutes...", qPrintable(m_name), - qMin(m_handshake_count^2, 120)); + qWarning("LibrefmScrobbler: waiting %d minutes...", 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])); + 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_songCache.isEmpty()) //submit recent songs + if (!m_cachedSongs.isEmpty()) //submit recent songs submit(); } } @@ -198,28 +195,28 @@ void Scrobbler::processResponse(QNetworkReply *reply) m_submitReply = 0; if (!data.startsWith("OK")) { - qWarning("Scrobbler[%s]: submit error",qPrintable(m_name)); + qWarning("LibrefmScrobbler: submit error"); if(data.contains("BADSESSION")) { - qWarning("Scrobbler[%s]: invalid session ID",qPrintable(m_name)); - qWarning("Scrobbler[%s]: performing re-handshake",qPrintable(m_name)); + qWarning("LibrefmScrobbler: invalid session ID"); + qWarning("LibrefmScrobbler: performing re-handshake"); handshake(); } else { - qWarning("Scrobbler[%s]: %s",qPrintable(m_name),qPrintable(data)); + qWarning("LibrefmScrobbler: %s", qPrintable(data)); m_failure_count ++; } } else { - qDebug("Scrobbler[%s]: submited %d song(s)",qPrintable(m_name), m_submitedSongs); + qDebug("LibrefmScrobbler: submited %d song(s)", m_submitedSongs); while (m_submitedSongs) { m_submitedSongs--; - m_songCache.removeFirst (); + m_cachedSongs.removeFirst (); } - if (!m_songCache.isEmpty()) //submit remaining songs + if (!m_cachedSongs.isEmpty()) //submit remaining songs submit(); else updateMetaData(); @@ -230,31 +227,31 @@ void Scrobbler::processResponse(QNetworkReply *reply) m_notificationReply = 0; if (!data.startsWith("OK")) { - qWarning("Scrobbler[%s]: notification error",qPrintable(m_name)); + qWarning("LibrefmScrobbler: notification error"); if(data.contains("BADSESSION")) { - qWarning("Scrobbler[%s]: invalid session ID",qPrintable(m_name)); - qWarning("Scrobbler[%s]: performing re-handshake",qPrintable(m_name)); + qWarning("LibrefmScrobbler: invalid session ID"); + qWarning("LibrefmScrobbler: performing re-handshake"); handshake(); } else { - qWarning("Scrobbler[%s]: %s",qPrintable(m_name),qPrintable(data)); + qWarning("LibrefmScrobbler: %s",qPrintable(data)); m_failure_count ++; } } else - qDebug("Scrobbler[%s]: Now-Playing notification done", qPrintable(m_name)); + qDebug("LibrefmScrobbler: Now-Playing notification done"); } if(m_failure_count >= 3) { - qWarning("Scrobbler[%s]: performing re-handshake",qPrintable(m_name)); + qWarning("LibrefmScrobbler: performing re-handshake"); handshake(); } reply->deleteLater(); } -void Scrobbler::setupProxy() +void LibrefmScrobbler::setupProxy() { QmmpSettings *gs = QmmpSettings::instance(); if (gs->isProxyEnabled()) @@ -271,17 +268,17 @@ void Scrobbler::setupProxy() m_http->setProxy(QNetworkProxy::NoProxy); } -void Scrobbler::handshake() +void LibrefmScrobbler::handshake() { if (m_disabled) return; - qDebug("Scrobbler[%s] handshake request",qPrintable(m_name)); + qDebug("LibrefmScrobbler: handshake request"); uint ts = QDateTime::currentDateTime().toTime_t(); - qDebug("Scrobbler[%s]: current time stamp %d",qPrintable(m_name),ts); + 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("http://") + m_server + "/?"); + QUrl url(QString(SCROBBLER_LIBREFM_URL) + "?"); url.addQueryItem("hs", "true"); url.addQueryItem("p", PROTOCOL_VER); url.addQueryItem("c", CLIENT_ID); @@ -290,7 +287,7 @@ void Scrobbler::handshake() 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())); + qDebug("LibrefmScrobbler: request url: %s",qPrintable(url.toString())); QNetworkRequest request(url); request.setRawHeader("User-Agent", m_ua); request.setRawHeader("Host",url.host().toAscii()); @@ -298,16 +295,16 @@ void Scrobbler::handshake() m_handshakeReply = m_http->get(request); } -void Scrobbler::submit() +void LibrefmScrobbler::submit() { - qDebug("Scrobbler[%s]: submit request", qPrintable(m_name)); - if (m_songCache.isEmpty()) + qDebug("LibrefmScrobbler: submit request"); + if (m_cachedSongs.isEmpty()) return; - m_submitedSongs = qMin(m_songCache.size(),25); + 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_songCache[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)) @@ -332,9 +329,9 @@ void Scrobbler::submit() m_submitReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); } -void Scrobbler::sendNotification(const SongInfo &info) +void LibrefmScrobbler::sendNotification(const SongInfo &info) { - qDebug("Scrobbler[%s]: sending notification", qPrintable(m_name)); + 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)) @@ -354,159 +351,7 @@ void Scrobbler::sendNotification(const SongInfo &info) m_notificationReply = m_http->post(request, QUrl::toPercentEncoding(body,":/[]&=%")); } -bool Scrobbler::isReady() +bool LibrefmScrobbler::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 <Qmmp::MetaData, QString> 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 <Qmmp::MetaData, QString> 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 <Qmmp::MetaData, QString> 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/librefmscrobbler.h index 34024c3c2..ece5ecec1 100644 --- a/src/plugins/General/scrobbler/scrobbler.h +++ b/src/plugins/General/scrobbler/librefmscrobbler.h @@ -17,12 +17,13 @@ * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ -#ifndef SCROBBLER_H -#define SCROBBLER_H +#ifndef LIBREFMSCROBBLER_H +#define LIBREFMSCROBBLER_H #include <QMap> #include <QObject> #include <qmmp/qmmp.h> +#include "scrobblercache.h" class QNetworkAccessManager; class QNetworkReply; @@ -32,50 +33,13 @@ class SoundCore; /** @author Ilya Kotov <forkotov02@hotmail.ru> */ -class SongInfo -{ -public: - SongInfo(); - SongInfo(const QMap <Qmmp::MetaData, QString> 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 <Qmmp::MetaData, QString> metadata); - void setMetaData(Qmmp::MetaData key, const QString &value); - void setLength(qint64 l); - const QMap <Qmmp::MetaData, QString> metaData() const; - const QString metaData(Qmmp::MetaData) const; - qint64 length () const; - void clear(); - void setTimeStamp(uint ts); - uint timeStamp() const; - -private: - QMap <Qmmp::MetaData, QString> m_metadata; - qint64 m_length; - uint m_start_ts; - -}; - -/** - @author Ilya Kotov <forkotov02@hotmail.ru> -*/ -class Scrobbler : public QObject +class LibrefmScrobbler : public QObject { Q_OBJECT public: - Scrobbler(const QString &url, - const QString &login, - const QString &passw, - const QString &name, - QObject *parent = 0); + LibrefmScrobbler(const QString &login, const QString &passw, QObject *parent = 0); - ~Scrobbler(); + ~LibrefmScrobbler(); private slots: void setState(Qmmp::State state); @@ -90,28 +54,27 @@ private: 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 <SongInfo> m_songCache; + QList <SongInfo> 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; }; 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/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 <QFile> +#include "scrobblercache.h" + +SongInfo::SongInfo() +{ + m_length = 0; +} + +SongInfo::SongInfo(const QMap <Qmmp::MetaData, QString> 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 <Qmmp::MetaData, QString> 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 <Qmmp::MetaData, QString> 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<SongInfo> ScrobblerCache::load() +{ + QList<SongInfo> songs; + int s = 0; + QString line, param, value; + QFile file(m_filePath); + + if(!file.open(QIODevice::ReadOnly)) + return QList<SongInfo>(); + + 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<SongInfo> &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 <QMap> +#include <QList> +#include <qmmp/qmmp.h> + + +/** + @author Ilya Kotov <forkotov02@hotmail.ru> +*/ +class SongInfo +{ +public: + SongInfo(); + SongInfo(const QMap <Qmmp::MetaData, QString> 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 <Qmmp::MetaData, QString> metadata); + void setMetaData(Qmmp::MetaData key, const QString &value); + void setLength(qint64 l); + const QMap <Qmmp::MetaData, QString> metaData() const; + const QString metaData(Qmmp::MetaData) const; + qint64 length () const; + void clear(); + void setTimeStamp(uint ts); + uint timeStamp() const; + +private: + QMap <Qmmp::MetaData, QString> m_metadata; + qint64 m_length; + uint m_start_ts; + +}; + +/** + @author Ilya Kotov <forkotov02@hotmail.ru> +*/ +class ScrobblerCache +{ +public: + explicit ScrobblerCache(const QString &filePath); + + QList<SongInfo> load(); + void save(const QList<SongInfo> &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 <QSettings> -#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(); |
