1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-02-11 11:21:22 +01:00

* Added similar tracks method to liblastfm.

This commit is contained in:
Christian Muehlhaeuser 2012-05-05 16:40:09 +02:00
parent f59bbc111f
commit 913663eef0
2 changed files with 48 additions and 0 deletions

View File

@ -454,6 +454,46 @@ lastfm::Track::params( const QString& method, bool use_mbid ) const
}
QNetworkReply*
lastfm::Track::getSimilar( int limit ) const
{
QMap<QString, QString> map = params("getSimilar");
if ( limit != -1 ) map["limit"] = QString::number( limit );
map["autocorrect"] = "1";
return ws::get( map );
}
QMap<int, QPair< QString, QString > > /* static */
lastfm::Track::getSimilar( QNetworkReply* r )
{
QMap<int, QPair< QString, QString > > 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
{

View File

@ -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<int, QPair< QString, QString > > getSimilar( QNetworkReply* );
/** you can get any QNetworkReply TagList using Tag::list( QNetworkReply* ) */
QNetworkReply* getTags() const; // for the logged in user
QNetworkReply* getTopTags() const;