From 913663eef098950ade976cc108f47599199acf52 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sat, 5 May 2012 16:40:09 +0200 Subject: [PATCH] * Added similar tracks method to liblastfm. --- thirdparty/liblastfm2/src/types/Track.cpp | 40 +++++++++++++++++++++++ thirdparty/liblastfm2/src/types/Track.h | 8 +++++ 2 files changed, 48 insertions(+) diff --git a/thirdparty/liblastfm2/src/types/Track.cpp b/thirdparty/liblastfm2/src/types/Track.cpp index 0111b8a9d..e8cfc9fa3 100644 --- a/thirdparty/liblastfm2/src/types/Track.cpp +++ b/thirdparty/liblastfm2/src/types/Track.cpp @@ -454,6 +454,46 @@ lastfm::Track::params( const QString& method, bool use_mbid ) const } +QNetworkReply* +lastfm::Track::getSimilar( int limit ) const +{ + QMap map = params("getSimilar"); + if ( limit != -1 ) map["limit"] = QString::number( limit ); + map["autocorrect"] = "1"; + return ws::get( map ); +} + + +QMap > /* static */ +lastfm::Track::getSimilar( QNetworkReply* r ) +{ + QMap > tracks; + try + { + QByteArray b = r->readAll(); + XmlQuery lfm = b; + foreach (XmlQuery e, lfm.children( "track" )) + { + QPair< QString, QString > track; + track.first = e["name"].text(); + + XmlQuery artist = e.children( "artist" ).first(); + track.second = artist["name"].text(); + + // convert floating percentage to int in range 0 to 10,000 + int const match = e["match"].text().toFloat() * 100; + tracks.insertMulti( match, track ); + } + } + catch (ws::ParseError& e) + { + qWarning() << e.what(); + } + + return tracks; +} + + QNetworkReply* lastfm::Track::getTopTags() const { diff --git a/thirdparty/liblastfm2/src/types/Track.h b/thirdparty/liblastfm2/src/types/Track.h index 56e97b272..4fa0c7043 100644 --- a/thirdparty/liblastfm2/src/types/Track.h +++ b/thirdparty/liblastfm2/src/types/Track.h @@ -244,6 +244,14 @@ public: /** See last.fm/api Track section */ QNetworkReply* share( const QStringList& recipients, const QString& message = "", bool isPublic = true ) const; + QNetworkReply* getSimilar( int limit = -1 ) const; + /** The match percentage is returned from last.fm as a 4 significant + * figure floating point value. So we multply it by 100 to make an + * integer in the range of 0 to 10,000. This is possible confusing + * for you, but I felt it best not to lose any precision, and floats + * aren't much fun. */ + static QMap > getSimilar( QNetworkReply* ); + /** you can get any QNetworkReply TagList using Tag::list( QNetworkReply* ) */ QNetworkReply* getTags() const; // for the logged in user QNetworkReply* getTopTags() const;