mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-09 15:47:38 +02:00
Update charts info plugin to use the new API
This changes from the /source/billboard/chart/top100 style of uri resource naming to /charts/billboard/top100. The server side changes have been live for several weeks.
This commit is contained in:
@@ -45,10 +45,8 @@ ChartsPlugin::ChartsPlugin()
|
|||||||
: InfoPlugin()
|
: InfoPlugin()
|
||||||
, m_chartsFetchJobs( 0 )
|
, m_chartsFetchJobs( 0 )
|
||||||
{
|
{
|
||||||
/// Add resources here
|
|
||||||
m_chartResources << "billboard" << "itunes" << "rdio" << "wearehunted" << "ex.fm" << "soundcloudwall";
|
|
||||||
/// If you add resource, update version aswell
|
/// If you add resource, update version aswell
|
||||||
m_chartVersion = "2.1";
|
m_chartVersion = "2.2";
|
||||||
m_supportedGetTypes << InfoChart << InfoChartCapabilities;
|
m_supportedGetTypes << InfoChart << InfoChartCapabilities;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -104,11 +102,11 @@ ChartsPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
fetchChart( requestData );
|
fetchChartFromCache( requestData );
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case InfoChartCapabilities:
|
case InfoChartCapabilities:
|
||||||
fetchChartCapabilities( requestData );
|
fetchChartCapabilitiesFromCache( requestData );
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
dataError( requestData );
|
dataError( requestData );
|
||||||
@@ -117,7 +115,7 @@ ChartsPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
ChartsPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData )
|
ChartsPlugin::fetchChartFromCache( Tomahawk::InfoSystem::InfoRequestData requestData )
|
||||||
{
|
{
|
||||||
|
|
||||||
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
|
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
|
||||||
@@ -145,7 +143,7 @@ ChartsPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData )
|
|||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ChartsPlugin::fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData )
|
ChartsPlugin::fetchChartCapabilitiesFromCache( Tomahawk::InfoSystem::InfoRequestData requestData )
|
||||||
{
|
{
|
||||||
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
|
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
|
||||||
{
|
{
|
||||||
@@ -168,53 +166,17 @@ ChartsPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSy
|
|||||||
case InfoChart:
|
case InfoChart:
|
||||||
{
|
{
|
||||||
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChart not in cache! Fetching...";
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChart not in cache! Fetching...";
|
||||||
|
fetchChart( requestData, criteria["chart_source"], criteria["chart_id"] );
|
||||||
/// Fetch the chart, we need source and id
|
|
||||||
QUrl url = QUrl( QString( CHART_URL "source/%1/chart/%2" ).arg( criteria["chart_source"] ).arg( criteria["chart_id"] ) );
|
|
||||||
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Getting chart url" << url;
|
|
||||||
|
|
||||||
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
|
|
||||||
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
|
|
||||||
|
|
||||||
connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) );
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case InfoChartCapabilities:
|
case InfoChartCapabilities:
|
||||||
{
|
{
|
||||||
|
|
||||||
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChartCapabilities not in cache! Fetching...";
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChartCapabilities not in cache! Fetching...";
|
||||||
// we never need to re-fetch
|
fetchChartSourcesList();
|
||||||
//if ( !m_allChartsMap.isEmpty() )
|
|
||||||
// return;
|
|
||||||
|
|
||||||
/// Then get each chart from resource
|
|
||||||
|
|
||||||
if ( !m_chartResources.isEmpty() && m_allChartsMap.isEmpty() )
|
|
||||||
{
|
|
||||||
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChart fetching possible resources";
|
|
||||||
foreach ( QString resource, m_chartResources )
|
|
||||||
{
|
|
||||||
QUrl url = QUrl( QString( CHART_URL "source/%1" ).arg( resource ) );
|
|
||||||
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
|
|
||||||
reply->setProperty( "chart_resource", resource);
|
|
||||||
|
|
||||||
tDebug() << "fetching:" << url;
|
|
||||||
connect( reply, SIGNAL( finished() ), SLOT( chartTypes() ) );
|
|
||||||
|
|
||||||
m_chartsFetchJobs++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( m_chartsFetchJobs > 0 )
|
|
||||||
{
|
|
||||||
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChartCapabilities still fetching!";
|
|
||||||
m_cachedRequests.append( requestData );
|
m_cachedRequests.append( requestData );
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit info( requestData, m_allChartsMap );
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -227,21 +189,89 @@ ChartsPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSy
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
ChartsPlugin::fetchChartSourcesList()
|
||||||
|
{
|
||||||
|
QUrl url = QUrl( QString( CHART_URL "charts" ) );
|
||||||
|
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
|
||||||
|
|
||||||
|
tDebug() << "fetching:" << url;
|
||||||
|
connect( reply, SIGNAL( finished() ), SLOT( chartSourcesList() ) );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
ChartsPlugin::chartTypes()
|
ChartsPlugin::chartSourcesList()
|
||||||
{
|
{
|
||||||
/// Get possible chart type for specificChartsPlugin: InfoChart types returned chart source
|
tDebug( LOGVERBOSE ) << "Got chart sources list";
|
||||||
tDebug( LOGVERBOSE ) << "Got chart type result";
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
|
||||||
|
|
||||||
|
if ( reply->error() == QNetworkReply::NoError )
|
||||||
|
{
|
||||||
|
QJson::Parser p;
|
||||||
|
bool ok;
|
||||||
|
const QVariantMap res = p.parse( reply, &ok ).toMap();
|
||||||
|
const QVariantList sources = res.value( "sources" ).toList();
|
||||||
|
|
||||||
|
if ( !ok )
|
||||||
|
{
|
||||||
|
tLog() << "Failed to parse sources" << p.errorString() << "On line" << p.errorLine();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
m_chartResources.clear();
|
||||||
|
foreach(const QVariant &source, sources) {
|
||||||
|
m_chartResources << source.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchAllChartSources();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChartsPlugin::fetchAllChartSources()
|
||||||
|
{
|
||||||
|
if ( !m_chartResources.isEmpty() && m_allChartsMap.isEmpty() )
|
||||||
|
{
|
||||||
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChart fetching source data";
|
||||||
|
foreach ( QString source, m_chartResources )
|
||||||
|
{
|
||||||
|
QUrl url = QUrl( QString( CHART_URL "charts/%1" ).arg( source ) );
|
||||||
|
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
|
||||||
|
reply->setProperty( "chart_source", source);
|
||||||
|
|
||||||
|
tDebug() << "fetching:" << url;
|
||||||
|
connect( reply, SIGNAL( finished() ), SLOT( chartsList() ) );
|
||||||
|
|
||||||
|
m_chartsFetchJobs++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChartsPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData, const QString& source, const QString& chart_id)
|
||||||
|
{
|
||||||
|
/// Fetch the chart, we need source and id
|
||||||
|
QUrl url = QUrl( QString( CHART_URL "charts/%1/%2" ).arg( source ).arg( chart_id ) );
|
||||||
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "fetching: " << url;
|
||||||
|
|
||||||
|
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
|
||||||
|
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
|
||||||
|
|
||||||
|
connect( reply, SIGNAL( finished() ), SLOT( chartReturned() ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
ChartsPlugin::chartsList()
|
||||||
|
{
|
||||||
|
tDebug( LOGVERBOSE ) << "Got chart list result";
|
||||||
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
|
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
|
||||||
|
|
||||||
|
|
||||||
if ( reply->error() == QNetworkReply::NoError )
|
if ( reply->error() == QNetworkReply::NoError )
|
||||||
{
|
{
|
||||||
QJson::Parser p;
|
QJson::Parser p;
|
||||||
bool ok;
|
bool ok;
|
||||||
const QVariantMap res = p.parse( reply, &ok ).toMap();
|
const QVariantMap res = p.parse( reply, &ok ).toMap();
|
||||||
const QVariantMap chartObjs = res.value( "charts" ).toMap();
|
|
||||||
|
|
||||||
if ( !ok )
|
if ( !ok )
|
||||||
{
|
{
|
||||||
@@ -251,7 +281,7 @@ ChartsPlugin::chartTypes()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Got types, append!
|
/// Got types, append!
|
||||||
const QString source = res.value( "source" ).toString();
|
const QString source = reply->property("chart_source").toString();
|
||||||
|
|
||||||
// We'll populate charts with the data from the server
|
// We'll populate charts with the data from the server
|
||||||
QVariantMap charts;
|
QVariantMap charts;
|
||||||
@@ -267,7 +297,7 @@ ChartsPlugin::chartTypes()
|
|||||||
// WeAreHunted - Type - Artists - Chart Type
|
// WeAreHunted - Type - Artists - Chart Type
|
||||||
// - Tracks - Chart Type
|
// - Tracks - Chart Type
|
||||||
QHash< QString, QVariantMap > extraType;
|
QHash< QString, QVariantMap > extraType;
|
||||||
foreach( const QVariant& chartObj, chartObjs.values() )
|
foreach( const QVariant& chartObj, res.values() )
|
||||||
{
|
{
|
||||||
if( !chartObj.toMap().isEmpty() )
|
if( !chartObj.toMap().isEmpty() )
|
||||||
{
|
{
|
||||||
@@ -352,7 +382,7 @@ ChartsPlugin::chartTypes()
|
|||||||
QList< InfoStringHash > trackCharts;
|
QList< InfoStringHash > trackCharts;
|
||||||
QList< InfoStringHash > artistCharts;
|
QList< InfoStringHash > artistCharts;
|
||||||
|
|
||||||
foreach( const QVariant& chartObj, chartObjs.values() )
|
foreach( const QVariant& chartObj, res.values() )
|
||||||
{
|
{
|
||||||
if( !chartObj.toMap().isEmpty() ){
|
if( !chartObj.toMap().isEmpty() ){
|
||||||
const QVariantMap chart = chartObj.toMap();
|
const QVariantMap chart = chartObj.toMap();
|
||||||
@@ -407,7 +437,7 @@ ChartsPlugin::chartTypes()
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Add the possible charts and its types to breadcrumb
|
/// Add the possible charts and its types to breadcrumb
|
||||||
// tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "ADDING CHART TYPE TO CHARTS:" << chartName;
|
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "ADDING CHART TO CHARTS:" << chartName;
|
||||||
QVariantMap defaultMap = m_allChartsMap.value( "defaults" ).value< QVariantMap >();
|
QVariantMap defaultMap = m_allChartsMap.value( "defaults" ).value< QVariantMap >();
|
||||||
defaultMap[ source ] = defaultChain;
|
defaultMap[ source ] = defaultChain;
|
||||||
m_allChartsMap[ "defaults" ] = defaultMap;
|
m_allChartsMap[ "defaults" ] = defaultMap;
|
||||||
|
@@ -52,10 +52,6 @@ public:
|
|||||||
void setChartType( ChartType type ) { m_chartType = type; }
|
void setChartType( ChartType type ) { m_chartType = type; }
|
||||||
ChartType chartType() const { return m_chartType; }
|
ChartType chartType() const { return m_chartType; }
|
||||||
|
|
||||||
public slots:
|
|
||||||
void chartReturned();
|
|
||||||
void chartTypes();
|
|
||||||
|
|
||||||
protected slots:
|
protected slots:
|
||||||
virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData );
|
virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData );
|
||||||
virtual void notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData );
|
virtual void notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData );
|
||||||
@@ -65,9 +61,39 @@ protected slots:
|
|||||||
Q_UNUSED( pushData );
|
Q_UNUSED( pushData );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a QNetworkReply of a list of chart sources.
|
||||||
|
*/
|
||||||
|
void chartSourcesList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a QNetworkReply of a list of charts for a particular source
|
||||||
|
*/
|
||||||
|
void chartsList();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a QNetworkReply for the chart data for a particular chart
|
||||||
|
*/
|
||||||
|
void chartReturned();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData );
|
/**
|
||||||
void fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData );
|
* Fetch list of chart sources (e.g., itunes, billboard)
|
||||||
|
* Populates the m_chartResources member.
|
||||||
|
*/
|
||||||
|
void fetchChartSourcesList();
|
||||||
|
/**
|
||||||
|
* Requests charts list for each chart source in m_chartResources
|
||||||
|
*/
|
||||||
|
void fetchAllChartSources();
|
||||||
|
/**
|
||||||
|
* Fetches a specific chart from a particular source.
|
||||||
|
* Updates the cache.
|
||||||
|
*/
|
||||||
|
void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData, const QString& source, const QString& chart_id );
|
||||||
|
|
||||||
|
void fetchChartFromCache( Tomahawk::InfoSystem::InfoRequestData requestData );
|
||||||
|
void fetchChartCapabilitiesFromCache( Tomahawk::InfoSystem::InfoRequestData requestData );
|
||||||
void dataError( Tomahawk::InfoSystem::InfoRequestData requestData );
|
void dataError( Tomahawk::InfoSystem::InfoRequestData requestData );
|
||||||
|
|
||||||
QStringList m_chartResources;
|
QStringList m_chartResources;
|
||||||
|
Reference in New Issue
Block a user