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

Implement fetching and displaying of the Top Tracks Chart.

This commit is contained in:
Casey Link 2011-08-21 18:04:44 -05:00
parent a21992a99a
commit fc235ccf1b
5 changed files with 131 additions and 7 deletions

View File

@ -47,7 +47,7 @@ LastFmPlugin::LastFmPlugin()
: InfoPlugin()
, m_scrobbler( 0 )
{
m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoArtistSimilars << InfoArtistSongs << InfoChartArtists;
m_supportedGetTypes << InfoAlbumCoverArt << InfoArtistImages << InfoArtistSimilars << InfoArtistSongs << InfoChartArtists << InfoChartTracks;
m_supportedPushTypes << InfoSubmitScrobble << InfoSubmitNowPlaying << InfoLove << InfoUnLove;
/*
@ -151,6 +151,10 @@ LastFmPlugin::getInfo( uint requestId, Tomahawk::InfoSystem::InfoRequestData req
fetchChartArtists( requestId, requestData );
break;
case InfoChartTracks:
fetchChartTracks( requestId, requestData );
break;
default:
dataError( requestId, requestData );
}
@ -321,6 +325,25 @@ LastFmPlugin::fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoReque
}
void
LastFmPlugin::fetchChartTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData )
{
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoCriteriaHash >() )
{
dataError( requestId, requestData );
return;
}
InfoCriteriaHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash >();
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
if ( hash.contains( "country" ) )
{
criteria["country"] = hash["country"];
}
emit getCachedInfo( requestId, criteria, 2419200000, requestData );
}
void
LastFmPlugin::fetchCoverArt( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData )
{
@ -397,6 +420,25 @@ LastFmPlugin::notInCacheSlot( uint requestId, QHash<QString, QString> criteria,
return;
}
case InfoChartTracks:
{
tDebug() << "LastfmPlugin: InfoChartTracks not in cache, fetching";
QMap<QString, QString> args;
if( criteria.contains( "country" ) ) {
args["method"] = "geo.getTopTracks";
args["country"] = criteria["country"];
} else {
args["method"] = "chart.getTopTracks";
}
args["limit"] = "100";
QNetworkReply* reply = lastfm::ws::get(args);
reply->setProperty( "requestId", requestId );
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
connect( reply, SIGNAL( finished() ), SLOT( chartTopTracksReturned() ) );
return;
}
case InfoArtistSimilars:
{
lastfm::Artist a( criteria["artist"] );
@ -519,6 +561,42 @@ LastFmPlugin::chartTopArtistsReturned()
emit updateCache( criteria, 2419200000, requestData.type, returnedData );
}
void
LastFmPlugin::chartTopTracksReturned()
{
tDebug() << "LastfmPlugin: InfoChartTracks data returned!";
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
QList<lastfm::Track> tracks = parseTrackList( reply );
QList<ArtistTrackPair> top_tracks;
foreach( const lastfm::Track &t, tracks ) {
ArtistTrackPair pair;
pair.artist = t.artist().toString();
pair.track = t.title();
top_tracks << pair;
}
tDebug() << "\tgot " << top_tracks.size() << " tracks";
QVariantMap returnedData;
returnedData["tracks"] = QVariant::fromValue( top_tracks );
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
emit info(
reply->property( "requestId" ).toUInt(),
requestData,
returnedData
);
Tomahawk::InfoSystem::InfoCriteriaHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash>();
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
if( origData.contains("country") )
criteria["country"] = origData["country"];
emit updateCache( criteria, 0, requestData.type, returnedData );
}
void
LastFmPlugin::topTracksReturned()
{
@ -539,7 +617,7 @@ LastFmPlugin::topTracksReturned()
Tomahawk::InfoSystem::InfoCriteriaHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoCriteriaHash>();
Tomahawk::InfoSystem::InfoCriteriaHash criteria;
criteria["artist"] = origData["artist"];
emit updateCache( criteria, 2419200000, requestData.type, returnedData );
emit updateCache( criteria, 0, requestData.type, returnedData );
}
@ -746,3 +824,21 @@ LastFmPlugin::createScrobbler()
}
}
QList<lastfm::Track>
LastFmPlugin::parseTrackList( QNetworkReply * reply )
{
QList<lastfm::Track> tracks;
try {
lastfm::XmlQuery lfm = lastfm::ws::parse(reply);
foreach (lastfm::XmlQuery xq, lfm.children( "track" )) {
tracks.append( lastfm::Track( xq ) );
}
}
catch (lastfm::ws::ParseError& e)
{
qWarning() << e.what();
}
return tracks;
}

View File

@ -53,6 +53,7 @@ public slots:
void similarArtistsReturned();
void topTracksReturned();
void chartTopArtistsReturned();
void chartTopTracksReturned();
void namChangedSlot( QNetworkAccessManager *nam );
@ -68,6 +69,7 @@ private:
void fetchSimilarArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchTopTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChartArtists( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchChartTracks( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
void createScrobbler();
void nowPlaying( const QVariant &input );
@ -76,6 +78,8 @@ private:
void dataError( uint requestId, Tomahawk::InfoSystem::InfoRequestData requestData );
QList<lastfm::Track> parseTrackList( QNetworkReply * reply );
lastfm::MutableTrack m_track;
lastfm::Audioscrobbler* m_scrobbler;
QString m_pw;

View File

@ -122,6 +122,11 @@ struct InfoRequestData {
QVariantMap customData;
};
struct ArtistTrackPair {
QString artist;
QString track;
};
typedef QMap< InfoType, QVariant > InfoTypeMap;
typedef QMap< InfoType, uint > InfoTimeoutMap;
typedef QMap< QString, QMap< QString, QString > > InfoGenericMap;
@ -258,5 +263,7 @@ Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoRequestData );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoGenericMap );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::InfoCriteriaHash );
Q_DECLARE_METATYPE( QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > );
Q_DECLARE_METATYPE( Tomahawk::InfoSystem::ArtistTrackPair );
Q_DECLARE_METATYPE( QList<Tomahawk::InfoSystem::ArtistTrackPair> );
#endif // TOMAHAWK_INFOSYSTEM_H

View File

@ -56,9 +56,10 @@ WhatsHotWidget::WhatsHotWidget( QWidget* parent )
m_tracksModel = new PlaylistModel( ui->tracksView );
m_tracksModel->setStyle( TrackModel::ShortWithAvatars );
m_tracksModel->setStyle( TrackModel::Short );
ui->tracksView->overlay()->setEnabled( false );
ui->tracksView->setPlaylistModel( m_tracksModel );
ui->tracksView->setTrackModel( m_tracksModel );
ui->tracksView->setHeaderHidden( true );
ui->tracksView->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
@ -99,7 +100,11 @@ WhatsHotWidget::WhatsHotWidget( QWidget* parent )
requestData.type = Tomahawk::InfoSystem::InfoChartArtists;
Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
tDebug() << "WhatsHot: requested InfoChartArtists";
requestData.type = Tomahawk::InfoSystem::InfoChartTracks;
Tomahawk::InfoSystem::InfoSystem::instance()->getInfo( requestData );
tDebug() << "WhatsHot: requested InfoChartArtists+Tracks";
}
@ -113,7 +118,7 @@ void
WhatsHotWidget::checkQueries()
{
m_timer->stop();
// m_tracksModel->ensureResolved();
m_tracksModel->ensureResolved();
}
@ -141,7 +146,17 @@ WhatsHotWidget::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestDat
}
break;
}
case InfoSystem::InfoChartTracks:
{
const QList<Tomahawk::InfoSystem::ArtistTrackPair> tracks = returnedData["tracks"].value<QList<Tomahawk::InfoSystem::ArtistTrackPair> >();
tDebug() << "WhatsHot: got tracks! " << tracks.size();
foreach ( const Tomahawk::InfoSystem::ArtistTrackPair& track, tracks )
{
query_ptr query = Query::get( track.artist, track.track, QString(), uuid() );
m_tracksModel->append( query );
}
break;
}
default:
return;
}

View File

@ -389,6 +389,8 @@ TomahawkApp::registerMetaTypes()
qRegisterMetaType< QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache > >( "QWeakPointer< Tomahawk::InfoSystem::InfoSystemCache >" );
qRegisterMetaType< DirLister::Mode >("DirLister::Mode");
qRegisterMetaType< Tomahawk::InfoSystem::ArtistTrackPair >("Tomahawk::InfoSystem::ArtistTrackPair");
qRegisterMetaType< QList<Tomahawk::InfoSystem::ArtistTrackPair> >("QList<Tomahawk::InfoSystem::ArtistTrackPair>");
}