File indexing completed on 2024-05-19 04:50:11
0001 /*************************************************************************************** 0002 * Copyright (c) 2009 Dan Meltzer <parallelgrapefruit@gmail.com> * 0003 * * 0004 * This program is free software; you can redistribute it and/or modify it under * 0005 * the terms of the GNU General Public License as published by the Free Software * 0006 * Foundation; either version 2 of the License, or (at your option) any later * 0007 * version. * 0008 * * 0009 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0010 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0011 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0012 * * 0013 * You should have received a copy of the GNU General Public License along with * 0014 * this program. If not, see <http://www.gnu.org/licenses/>. * 0015 ****************************************************************************************/ 0016 0017 0018 #include "LastfmInfoParser.h" 0019 0020 #include "core/meta/Meta.h" 0021 #include "core/support/Amarok.h" 0022 #include "core/support/Debug.h" 0023 0024 #include <KLocalizedString> 0025 0026 #include <QNetworkReply> 0027 0028 #include <XmlQuery.h> 0029 0030 void LastfmInfoParser::getInfo(const Meta::TrackPtr &track) 0031 { 0032 DEBUG_BLOCK 0033 QMap<QString, QString> query; 0034 query[ "method" ] = "track.getInfo"; 0035 query[ "track" ] = track->name(); 0036 query[ "album" ] = track->album() ? track->album()->name() : QString(); 0037 query[ "artist" ] = track->artist() ? track->artist()->name() : QString(); 0038 query[ "apikey" ] = Amarok::lastfmApiKey(); 0039 0040 m_jobs[ "getTrackInfo" ] = lastfm::ws::post( query ); 0041 0042 connect( m_jobs[ "getTrackInfo" ], &QNetworkReply::finished, this, &LastfmInfoParser::onGetTrackInfo ); 0043 } 0044 0045 void LastfmInfoParser::onGetTrackInfo() 0046 { 0047 DEBUG_BLOCK 0048 if( !m_jobs[ "getTrackInfo" ] ) 0049 { 0050 debug() << "WARNING: GOT RESULT but no object"; 0051 return; 0052 } 0053 0054 switch ( m_jobs[ "getTrackInfo" ]->error() ) 0055 { 0056 case QNetworkReply::NoError: 0057 { 0058 lastfm::XmlQuery lfm; 0059 lfm.parse( m_jobs[ "getTrackInfo" ]->readAll() ); 0060 lastfm::XmlQuery wiki = lfm["track"]["wiki"]; 0061 const QString contentText = wiki["content"].text(); 0062 const QString publishedDate = wiki["published"].text(); 0063 0064 QString html; 0065 if( !contentText.isEmpty() ) 0066 html = QStringLiteral("<p><font size=3><i>%2<i></font></p> <p align='right'><font size=1>%1</font></p>").arg( i18n("Updated: %1", publishedDate), contentText ); 0067 else 0068 html = i18n( "<p>No information found for this track.</p>" ); 0069 emit info( html ); 0070 break; 0071 } 0072 default: 0073 break; 0074 } 0075 m_jobs["getTrackInfo"]->deleteLater(); 0076 m_jobs["getTrackInfo"] = nullptr; 0077 } 0078 0079 void LastfmInfoParser::getInfo(const Meta::AlbumPtr &album) 0080 { 0081 DEBUG_BLOCK 0082 QMap<QString, QString> query; 0083 query[ "method" ] = "album.getInfo"; 0084 query[ "album" ] = album->name(); 0085 query[ "artist" ] = album->albumArtist() ? album->albumArtist()->name() : QString(); 0086 query[ "apikey" ] = Amarok::lastfmApiKey(); 0087 0088 m_jobs[ "getAlbumInfo" ] = lastfm::ws::post( query ); 0089 0090 connect( m_jobs[ "getAlbumInfo" ], &QNetworkReply::finished, this, &LastfmInfoParser::onGetAlbumInfo ); 0091 } 0092 0093 0094 void LastfmInfoParser::onGetAlbumInfo() 0095 { 0096 DEBUG_BLOCK 0097 if( !m_jobs[ "getAlbumInfo" ] ) 0098 { 0099 debug() << "WARNING: GOT RESULT but no object"; 0100 return; 0101 } 0102 0103 switch ( m_jobs[ "getAlbumInfo" ]->error() ) 0104 { 0105 case QNetworkReply::NoError: 0106 { 0107 lastfm::XmlQuery lfm; 0108 lfm.parse( m_jobs[ "getAlbumInfo" ]->readAll() ); 0109 lastfm::XmlQuery wiki = lfm["album"]["wiki"]; 0110 const QString summaryText = wiki["summary"].text(); 0111 const QString contentText = wiki["content"].text(); 0112 const QString publishedDate = wiki["published"].text(); 0113 0114 const QString albumUrl = lfm["image size=large"].text(); 0115 0116 QString html; 0117 if( !contentText.isEmpty() ) 0118 html = QString("<div align='center'><img src=%1></div><div align='center'><p><font size=3><i>%2<i></font></p> <p align='right'><font size=1>Updated: %3</font></p></div>").arg( albumUrl, contentText, publishedDate ); 0119 else 0120 html = i18n( "<p>No information found for this album.</p>" ); 0121 emit info( html ); 0122 break; 0123 } 0124 default: 0125 break; 0126 } 0127 m_jobs["getAlbumInfo"]->deleteLater(); 0128 m_jobs["getAlbumInfo"] = nullptr; 0129 } 0130 0131 0132 void LastfmInfoParser::getInfo(const Meta::ArtistPtr &artist) 0133 { 0134 QMap<QString, QString> query; 0135 query[ "method" ] = "artist.getInfo"; 0136 query[ "artist" ] = artist->name(); 0137 debug() << "api key is: " << Amarok::lastfmApiKey(); 0138 query[ "apikey" ] = Amarok::lastfmApiKey(); 0139 0140 m_jobs[ "getArtistInfo" ] = lastfm::ws::post( query ); 0141 0142 connect( m_jobs[ "getArtistInfo" ], &QNetworkReply::finished, this, &LastfmInfoParser::onGetArtistInfo ); 0143 0144 } 0145 0146 0147 void LastfmInfoParser::onGetArtistInfo() 0148 { 0149 DEBUG_BLOCK 0150 if( !m_jobs[ "getArtistInfo" ] ) 0151 { 0152 debug() << "WARNING: GOT RESULT but no object"; 0153 return; 0154 } 0155 0156 switch ( m_jobs[ "getArtistInfo" ]->error() ) 0157 { 0158 case QNetworkReply::NoError: 0159 { 0160 lastfm::XmlQuery lfm; 0161 lfm.parse( m_jobs[ "getArtistInfo" ]->readAll() ); 0162 debug() << lfm.text(); 0163 lastfm::XmlQuery bio = lfm["artist"]["bio"]; 0164 const QString summaryText = bio["summary"].text(); 0165 const QString contentText = bio["content"].text(); 0166 const QString publishedDate = bio["published"].text(); 0167 0168 const QString imageUrl = lfm["image size=large"].text(); 0169 0170 QString html; 0171 if( !contentText.isEmpty() ) 0172 html = QString("<div align='left'><img src=%1></div><div align='center'><p><font size=3><i>%2<i></font></p> <p align='right'><font size=1>Updated: %3</font></p></div>").arg( imageUrl, contentText, publishedDate ); 0173 else 0174 html = i18n( "<p>No information found for this artist.</p>" ); 0175 emit info( html ); 0176 0177 break; 0178 } 0179 default: 0180 break; 0181 } 0182 m_jobs["getArtistInfo"]->deleteLater(); 0183 m_jobs["getArtistInfo"] = nullptr; 0184 } 0185