PageRenderTime 49ms CodeModel.GetById 10ms app.highlight 35ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/liblastfm2/src/types/Artist.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 230 lines | 177 code | 33 blank | 20 comment | 9 complexity | b632229efad69e353586d95bb89cdb2f MD5 | raw file
  1/*
  2   Copyright 2009 Last.fm Ltd. 
  3      - Primarily authored by Max Howell, Jono Cole and Doug Mansell
  4
  5   This file is part of liblastfm.
  6
  7   liblastfm is free software: you can redistribute it and/or modify
  8   it under the terms of the GNU General Public License as published by
  9   the Free Software Foundation, either version 3 of the License, or
 10   (at your option) any later version.
 11
 12   liblastfm is distributed in the hope that it will be useful,
 13   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15   GNU General Public License for more details.
 16
 17   You should have received a copy of the GNU General Public License
 18   along with liblastfm.  If not, see <http://www.gnu.org/licenses/>.
 19*/
 20#include "Artist.h"
 21#include "User.h"
 22#include "../core/UrlBuilder.h"
 23#include "../core/XmlQuery.h"
 24#include "../ws/ws.h"
 25#include <QRegExp>
 26#include <QStringList>
 27
 28using lastfm::Artist;
 29using lastfm::User;
 30using lastfm::ImageSize;
 31using lastfm::XmlQuery;
 32
 33QUrl
 34Artist::imageUrl( ImageSize size, bool square ) const
 35{
 36    if( !square ) return m_images.value( size ); 
 37
 38    QUrl url = m_images.value( size );
 39    QRegExp re( "/serve/(\\d*)s?/" );
 40    return QUrl( url.toString().replace( re, "/serve/\\1s/" ));
 41}
 42
 43static inline QList<QUrl> images( const lastfm::XmlQuery& e )
 44{
 45    QList<QUrl> images;
 46    images += e["image size=small"].text();
 47    images += e["image size=medium"].text();
 48    images += e["image size=large"].text();
 49    return images;
 50}
 51
 52
 53Artist::Artist( const XmlQuery& xml )
 54    :AbstractType()
 55{
 56    m_name = xml["name"].text();
 57    m_images = images( xml );
 58}
 59
 60
 61QMap<QString, QString> //private
 62Artist::params( const QString& method ) const
 63{
 64    QMap<QString, QString> map;
 65    map["method"] = "artist."+method;
 66    map["artist"] = m_name;
 67    return map;
 68}
 69
 70
 71QNetworkReply*
 72Artist::share( const QStringList& recipients, const QString& message, bool isPublic ) const
 73{
 74    QMap<QString, QString> map = params("share");
 75    map["recipient"] = recipients.join(",");
 76    map["public"] = isPublic ? "1" : "0";
 77    if (message.size()) map["message"] = message;
 78    return lastfm::ws::post(map);
 79}
 80
 81
 82QUrl 
 83Artist::www() const
 84{
 85    return UrlBuilder( "music" ).slash( Artist::name() ).url();
 86}
 87
 88QNetworkReply*
 89Artist::getEvents(int limit) const
 90{
 91    QMap<QString, QString> map = params("getEvents");
 92    if (limit) map["limit"] = QString::number(limit);
 93    return ws::get( map );
 94}
 95
 96QNetworkReply* 
 97Artist::getInfo() const
 98{
 99    QMap<QString, QString> map = params("getInfo");
100    if (!lastfm::ws::Username.isEmpty()) map["username"] = lastfm::ws::Username;
101    if (!lastfm::ws::SessionKey.isEmpty()) map["sk"] = lastfm::ws::SessionKey;
102    return ws::get( map );
103}
104
105
106QNetworkReply* 
107Artist::getTags() const
108{
109    return ws::get( params("getTags") );
110}
111
112QNetworkReply* 
113Artist::getTopTags() const
114{
115    return ws::get( params("getTopTags") );
116}
117
118
119QNetworkReply* 
120Artist::getTopTracks() const
121{
122    return ws::get( params("getTopTracks") );
123}
124
125
126QNetworkReply* 
127Artist::getSimilar( int limit ) const
128{
129    QMap<QString, QString> map = params("getSimilar");
130    if ( limit != -1 ) map["limit"] = QString::number( limit );
131    return ws::get( map );
132}
133
134
135QNetworkReply* 
136Artist::search( int limit ) const
137{
138    QMap<QString, QString> map = params("search");
139    if (limit > 0) map["limit"] = QString::number(limit);
140    return ws::get(map);
141}
142
143
144QMap<int, QString> /* static */
145Artist::getSimilar( QNetworkReply* r )
146{
147    QMap<int, QString> artists;
148    try
149    {
150        XmlQuery lfm = r->readAll();
151        foreach (XmlQuery e, lfm.children( "artist" ))
152        {
153            // convert floating percentage to int in range 0 to 10,000
154            int const match = e["match"].text().toFloat() * 100;
155            artists.insertMulti( match, e["name"].text() );
156        }
157    }
158    catch (ws::ParseError& e)
159    {
160        qWarning() << e.what();
161    }
162    return artists;
163}
164
165
166QStringList /* static */
167Artist::getTopTracks( QNetworkReply* r )
168{
169    QStringList tracks;
170    try
171    {
172        XmlQuery lfm = r->readAll();
173        foreach (XmlQuery e, lfm.children( "track" ))
174        {
175            tracks << e["name"].text();
176        }
177    }
178    catch (ws::ParseError& e)
179    {
180        qWarning() << e.what();
181    }
182    return tracks;
183}
184
185
186QList<Artist> /* static */
187Artist::list( QNetworkReply* r )
188{
189    QList<Artist> artists;
190    try {
191        XmlQuery lfm = r->readAll();
192        foreach (XmlQuery xq, lfm.children( "artist" )) {
193            Artist artist( xq );
194            artists += artist;
195        }
196    }
197    catch (ws::ParseError& e)
198    {
199        qWarning() << e.what();
200    }
201    return artists;
202}
203
204
205Artist
206Artist::getInfo( QNetworkReply* r )
207{
208    try {
209        XmlQuery lfm = r->readAll();
210        Artist artist = lfm["artist"]["name"].text();
211        artist.m_images = images( lfm["artist"] );
212        return artist;
213    }
214    catch (ws::ParseError& e)
215    {
216        qWarning() << e.what();
217        return Artist();
218    }
219}
220
221
222QNetworkReply*
223Artist::addTags( const QStringList& tags ) const
224{
225    if (tags.isEmpty())
226        return 0;
227    QMap<QString, QString> map = params("addTags");
228    map["tags"] = tags.join( QChar(',') );
229    return ws::post(map);
230}