From 9266fdb9a4c515eb7734a00ed5b0397ae5f1ce92 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 9 Dec 2012 06:37:46 +0100 Subject: [PATCH] * Support getting biographies from Last.fm. --- .../accounts/lastfm/LastFmInfoPlugin.cpp | 152 ++++++++++++------ .../accounts/lastfm/LastFmInfoPlugin.h | 7 +- 2 files changed, 104 insertions(+), 55 deletions(-) diff --git a/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp b/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp index 9dc4571dc..e0ff6df73 100644 --- a/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp +++ b/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.cpp @@ -47,7 +47,7 @@ LastFmInfoPlugin::LastFmInfoPlugin( LastFmAccount* account ) , m_account( account ) , m_scrobbler( 0 ) { - m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoArtistSimilars << InfoArtistSongs << InfoChart << InfoChartCapabilities << InfoTrackSimilars; + m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoArtistSimilars << InfoArtistSongs << InfoArtistBiography << InfoChart << InfoChartCapabilities << InfoTrackSimilars; m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying << InfoLove << InfoUnLove; } @@ -107,10 +107,6 @@ LastFmInfoPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) { switch ( requestData.type ) { - case InfoArtistImages: - fetchArtistImages( requestData ); - break; - case InfoAlbumCoverArt: fetchCoverArt( requestData ); break; @@ -123,6 +119,11 @@ LastFmInfoPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) fetchTopTracks( requestData ); break; + case InfoArtistImages: + case InfoArtistBiography: + fetchArtistInfo( requestData ); + break; + case InfoChart: fetchChart( requestData ); break; @@ -325,6 +326,28 @@ LastFmInfoPlugin::fetchTopTracks( Tomahawk::InfoSystem::InfoRequestData requestD } +void +LastFmInfoPlugin::fetchArtistInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) +{ + if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() ) + { + dataError( requestData ); + return; + } + InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); + if ( !hash.contains( "artist" ) ) + { + dataError( requestData ); + return; + } + + Tomahawk::InfoSystem::InfoStringHash criteria; + criteria["artist"] = hash["artist"]; + + emit getCachedInfo( criteria, 2419200000, requestData ); +} + + void LastFmInfoPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ) { @@ -385,28 +408,6 @@ LastFmInfoPlugin::fetchCoverArt( Tomahawk::InfoSystem::InfoRequestData requestDa } -void -LastFmInfoPlugin::fetchArtistImages( Tomahawk::InfoSystem::InfoRequestData requestData ) -{ - if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() ) - { - dataError( requestData ); - return; - } - InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); - if ( !hash.contains( "artist" ) ) - { - dataError( requestData ); - return; - } - - Tomahawk::InfoSystem::InfoStringHash criteria; - criteria["artist"] = hash["artist"]; - - emit getCachedInfo( criteria, 2419200000, requestData ); -} - - void LastFmInfoPlugin::notInCacheSlot( QHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) { @@ -515,6 +516,17 @@ LastFmInfoPlugin::notInCacheSlot( QHash criteria, Tomahawk::In return; } + case InfoArtistBiography: + case InfoArtistImages: + { + lastfm::Artist a( criteria["artist"] ); + QNetworkReply* reply = a.getInfo(); + reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); + + connect( reply, SIGNAL( finished() ), SLOT( artistInfoReturned() ) ); + return; + } + case InfoAlbumCoverArt: { QString artistName = criteria["artist"]; @@ -536,25 +548,6 @@ LastFmInfoPlugin::notInCacheSlot( QHash criteria, Tomahawk::In return; } - case InfoArtistImages: - { - QString artistName = criteria["artist"]; - - QUrl imgurl( "http://ws.audioscrobbler.com/2.0/" ); - imgurl.addQueryItem( "method", "artist.imageredirect" ); - imgurl.addEncodedQueryItem( "artist", QUrl::toPercentEncoding( artistName, "", "+" ) ); - imgurl.addQueryItem( "autocorrect", QString::number( 1 ) ); - imgurl.addQueryItem( "size", "largesquare" ); - imgurl.addQueryItem( "api_key", "7a90f6672a04b809ee309af169f34b8b" ); - - QNetworkRequest req( imgurl ); - QNetworkReply* reply = TomahawkUtils::nam()->get( req ); - reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); - - connect( reply, SIGNAL( finished() ), SLOT( artistImagesReturned() ) ); - return; - } - default: { tLog() << "Couldn't figure out what to do with this type of request after cache miss"; @@ -726,6 +719,66 @@ LastFmInfoPlugin::topTracksReturned() } +void +LastFmInfoPlugin::artistInfoReturned() +{ + QNetworkReply* reply = qobject_cast( sender() ); + Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(); + + if ( requestData.type == Tomahawk::InfoSystem::InfoArtistBiography ) + { + QVariantMap returnedData; + lastfm::XmlQuery lfm; + if ( lfm.parse( reply ) ) + { + QRegExp tagRegExp( "" ); + QRegExp artistRegExp( "" ); + QRegExp albumRegExp( "" ); + QRegExp trackRegExp( "" ); + + tagRegExp.setMinimal( true ); + artistRegExp.setMinimal( true ); + albumRegExp.setMinimal( true ); + trackRegExp.setMinimal( true ); + + QString biography = lfm["artist"]["bio"]["content"].text().trimmed().replace( "User-contributed text is available under the Creative Commons By-SA License and may also be available under the GNU FDL.", "" ); + biography = biography.replace( tagRegExp, "" ) + .replace( artistRegExp, "" ) + .replace( albumRegExp, "" ) + .replace( trackRegExp, "" ) + .replace( "&album=_", "" ); + + QVariantHash siteData; + siteData[ "site" ] = "last.fm"; + siteData[ "text" ] = biography.replace( "\r", "\n" ).replace( "\n\n", "\n" ); + siteData[ "summary" ] = lfm["artist"]["bio"]["summary"].text().trimmed().replace( "\r", "\n" ).replace( "\n\n", "\n" ); + returnedData[ "last.fm" ] = siteData; + + Tomahawk::InfoSystem::InfoStringHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>(); + Tomahawk::InfoSystem::InfoStringHash criteria; + criteria["artist"] = origData["artist"]; + emit updateCache( criteria, 0, requestData.type, returnedData ); + } + emit info( requestData, returnedData ); + } + else if ( requestData.type == Tomahawk::InfoSystem::InfoArtistImages ) + { + lastfm::Artist artist = lastfm::Artist::getInfo( reply ); + + QUrl imgurl = artist.imageUrl( lastfm::AbstractType::MegaImage ); + if ( !imgurl.isValid() ) + imgurl = artist.imageUrl( lastfm::AbstractType::ExtraLargeImage ); + if ( !imgurl.isValid() ) + imgurl = artist.imageUrl( lastfm::AbstractType::LargeImage ); + + QNetworkRequest req( imgurl ); + QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); + newReply->setProperty( "requestData", reply->property( "requestData" ) ); + connect( newReply, SIGNAL( finished() ), SLOT( coverArtReturned() ) ); + } +} + + void LastFmInfoPlugin::coverArtReturned() { @@ -762,12 +815,6 @@ LastFmInfoPlugin::coverArtReturned() } else { - if ( !TomahawkUtils::nam() ) - { - tLog() << Q_FUNC_INFO << "Uh oh, nam is null"; - emit info( reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant() ); - return; - } // Follow HTTP redirect QNetworkRequest req( redir ); QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); @@ -798,6 +845,7 @@ LastFmInfoPlugin::artistImagesReturned() if ( reply->url().toString().startsWith( url.toString() ) ) ba = QByteArray(); } + QVariantMap returnedData; returnedData["imgbytes"] = ba; returnedData["url"] = reply->url().toString(); diff --git a/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.h b/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.h index f8bbacd17..bf95ed6a0 100644 --- a/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.h +++ b/src/libtomahawk/accounts/lastfm/LastFmInfoPlugin.h @@ -59,6 +59,7 @@ public slots: void artistImagesReturned(); void similarArtistsReturned(); void topTracksReturned(); + void artistInfoReturned(); void chartReturned(); void similarTracksReturned(); @@ -71,22 +72,22 @@ protected slots: private: void fetchCoverArt( Tomahawk::InfoSystem::InfoRequestData requestData ); - void fetchArtistImages( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchSimilarArtists( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchTopTracks( Tomahawk::InfoSystem::InfoRequestData requestData ); + void fetchArtistInfo( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchSimilarTracks( Tomahawk::InfoSystem::InfoRequestData requestData ); void createScrobbler(); - void nowPlaying( const QVariant &input ); + void nowPlaying( const QVariant& input ); void scrobble(); void sendLoveSong( const InfoType type, QVariant input ); void dataError( Tomahawk::InfoSystem::InfoRequestData requestData ); QWeakPointer< Accounts::LastFmAccount > m_account; - QList parseTrackList( QNetworkReply * reply ); + QList parseTrackList( QNetworkReply* reply ); lastfm::MutableTrack m_track; lastfm::Audioscrobbler* m_scrobbler;