1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 00:09:47 +01:00

* Support getting biographies from Last.fm.

This commit is contained in:
Christian Muehlhaeuser 2012-12-09 06:37:46 +01:00
parent f20b3360e1
commit 9266fdb9a4
2 changed files with 104 additions and 55 deletions

View File

@ -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<QString, QString> criteria, Tomahawk::InfoSystem::InfoRequestData requestData )
{
@ -515,6 +516,17 @@ LastFmInfoPlugin::notInCacheSlot( QHash<QString, QString> 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<QString, QString> 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<QNetworkReply*>( 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( "<a href=\"http://www.last.fm/tag/([^\"]*)\" class=\"bbcode_tag\" rel=\"tag\">" );
QRegExp artistRegExp( "<a href=\"http://www.last.fm/music/([^\"]*)\" class=\"bbcode_artist\">" );
QRegExp albumRegExp( "<a title=\"([^\"]*)\" href=\"http://www.last.fm/music/([^\"]*)/([^\"]*)\" class=\"bbcode_album\">" );
QRegExp trackRegExp( "<a title=\"([^\"]*)\" href=\"http://www.last.fm/music/([^\"]*)/([^\"]*)/([^\"]*)\" class=\"bbcode_track\">" );
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, "<a href=\"tomahawk://view/tag?name=\\1\">" )
.replace( artistRegExp, "<a href=\"tomahawk://view/artist?name=\\1\">" )
.replace( albumRegExp, "<a href=\"tomahawk://view/album?artist=\\2&name=\\3\">" )
.replace( trackRegExp, "<a href=\"tomahawk://view/track?artist=\\2&album=\\3&name=\\4\">" )
.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();

View File

@ -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<lastfm::Track> parseTrackList( QNetworkReply * reply );
QList<lastfm::Track> parseTrackList( QNetworkReply* reply );
lastfm::MutableTrack m_track;
lastfm::Audioscrobbler* m_scrobbler;