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