1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-09 15:47:38 +02: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_account( account )
, m_scrobbler( 0 ) , 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; m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying << InfoLove << InfoUnLove;
} }
@@ -107,10 +107,6 @@ LastFmInfoPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
{ {
switch ( requestData.type ) switch ( requestData.type )
{ {
case InfoArtistImages:
fetchArtistImages( requestData );
break;
case InfoAlbumCoverArt: case InfoAlbumCoverArt:
fetchCoverArt( requestData ); fetchCoverArt( requestData );
break; break;
@@ -123,6 +119,11 @@ LastFmInfoPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
fetchTopTracks( requestData ); fetchTopTracks( requestData );
break; break;
case InfoArtistImages:
case InfoArtistBiography:
fetchArtistInfo( requestData );
break;
case InfoChart: case InfoChart:
fetchChart( requestData ); fetchChart( requestData );
break; 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 void
LastFmInfoPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ) 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 void
LastFmInfoPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) LastFmInfoPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSystem::InfoRequestData requestData )
{ {
@@ -515,6 +516,17 @@ LastFmInfoPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::In
return; 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: case InfoAlbumCoverArt:
{ {
QString artistName = criteria["artist"]; QString artistName = criteria["artist"];
@@ -536,25 +548,6 @@ LastFmInfoPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::In
return; 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: default:
{ {
tLog() << "Couldn't figure out what to do with this type of request after cache miss"; 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 void
LastFmInfoPlugin::coverArtReturned() LastFmInfoPlugin::coverArtReturned()
{ {
@@ -762,12 +815,6 @@ LastFmInfoPlugin::coverArtReturned()
} }
else 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 // Follow HTTP redirect
QNetworkRequest req( redir ); QNetworkRequest req( redir );
QNetworkReply* newReply = TomahawkUtils::nam()->get( req ); QNetworkReply* newReply = TomahawkUtils::nam()->get( req );
@@ -798,6 +845,7 @@ LastFmInfoPlugin::artistImagesReturned()
if ( reply->url().toString().startsWith( url.toString() ) ) if ( reply->url().toString().startsWith( url.toString() ) )
ba = QByteArray(); ba = QByteArray();
} }
QVariantMap returnedData; QVariantMap returnedData;
returnedData["imgbytes"] = ba; returnedData["imgbytes"] = ba;
returnedData["url"] = reply->url().toString(); returnedData["url"] = reply->url().toString();

View File

@@ -59,6 +59,7 @@ public slots:
void artistImagesReturned(); void artistImagesReturned();
void similarArtistsReturned(); void similarArtistsReturned();
void topTracksReturned(); void topTracksReturned();
void artistInfoReturned();
void chartReturned(); void chartReturned();
void similarTracksReturned(); void similarTracksReturned();
@@ -71,22 +72,22 @@ protected slots:
private: private:
void fetchCoverArt( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchCoverArt( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchArtistImages( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchSimilarArtists( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchSimilarArtists( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchTopTracks( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchTopTracks( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchArtistInfo( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchSimilarTracks( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchSimilarTracks( Tomahawk::InfoSystem::InfoRequestData requestData );
void createScrobbler(); void createScrobbler();
void nowPlaying( const QVariant &input ); void nowPlaying( const QVariant& input );
void scrobble(); void scrobble();
void sendLoveSong( const InfoType type, QVariant input ); void sendLoveSong( const InfoType type, QVariant input );
void dataError( Tomahawk::InfoSystem::InfoRequestData requestData ); void dataError( Tomahawk::InfoSystem::InfoRequestData requestData );
QWeakPointer< Accounts::LastFmAccount > m_account; QWeakPointer< Accounts::LastFmAccount > m_account;
QList<lastfm::Track> parseTrackList( QNetworkReply * reply ); QList<lastfm::Track> parseTrackList( QNetworkReply* reply );
lastfm::MutableTrack m_track; lastfm::MutableTrack m_track;
lastfm::Audioscrobbler* m_scrobbler; lastfm::Audioscrobbler* m_scrobbler;