1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-06 14:16:32 +02:00

Add cache for all charts. Later on move spotify and hypem to charts api.

This commit is contained in:
Hugo Lindström
2011-11-16 14:54:15 +01:00
parent 4ba59c07f9
commit b431c34d5f
5 changed files with 69 additions and 51 deletions

View File

@@ -152,8 +152,8 @@ ChartsPlugin::fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requ
} }
Tomahawk::InfoSystem::InfoStringHash criteria; Tomahawk::InfoSystem::InfoStringHash criteria;
criteria[ "InfoChartCapabilities" ] = "all"; criteria[ "InfoChartCapabilities" ] = "chartsplugin";
emit getCachedInfo( criteria, 86400000, requestData ); emit getCachedInfo( criteria, 604800000, requestData );
} }
void void
@@ -181,6 +181,9 @@ ChartsPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSy
{ {
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
if ( !m_allChartsMap.isEmpty() )
return;
/// Then get each chart from resource /// Then get each chart from resource
@@ -418,8 +421,8 @@ ChartsPlugin::chartTypes()
emit info( request, m_allChartsMap ); emit info( request, m_allChartsMap );
// update cache // update cache
Tomahawk::InfoSystem::InfoStringHash criteria; Tomahawk::InfoSystem::InfoStringHash criteria;
criteria[ "InfoChartCapabilities" ] = "all"; criteria[ "InfoChartCapabilities" ] = "chartsplugin";
emit updateCache( criteria, 86400000, request.type, m_allChartsMap ); emit updateCache( criteria, 604800000, request.type, m_allChartsMap );
} }
m_cachedRequests.clear(); m_cachedRequests.clear();
} }

View File

@@ -23,11 +23,9 @@
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QNetworkConfiguration> #include <QNetworkConfiguration>
#include <QNetworkReply> #include <QNetworkReply>
#include <QDomElement>
#include "album.h" #include "album.h"
#include "typedefs.h" #include "typedefs.h"
#include "audio/audioengine.h"
#include "tomahawksettings.h" #include "tomahawksettings.h"
#include "utils/tomahawkutils.h" #include "utils/tomahawkutils.h"
#include "utils/logger.h" #include "utils/logger.h"
@@ -140,16 +138,6 @@ hypemPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
} }
} }
void
hypemPlugin::pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input )
{
Q_UNUSED( caller )
Q_UNUSED( type)
Q_UNUSED( input )
}
void void
hypemPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ) hypemPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData )
{ {
@@ -173,8 +161,9 @@ hypemPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData )
/// Set the criterias for current chart /// Set the criterias for current chart
criteria["chart_id"] = hash["chart_id"]; criteria["chart_id"] = hash["chart_id"];
criteria["chart_source"] = hash["chart_source"]; criteria["chart_source"] = hash["chart_source"];
/// @todo
emit getCachedInfo( criteria, 0, requestData ); /// set cache time based on wether requested type is 3day, lastweek or recent.
emit getCachedInfo( criteria, 86400000, requestData );
} }
void void
@@ -198,7 +187,7 @@ hypemPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSys
case InfoChart: case InfoChart:
{ {
/// Fetch the chart, we need source and id /// Fetch the chart, we need source and id
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChart not in cache! Fetching...";
QUrl url = QUrl( QString( HYPEM_URL "%1/%2" ).arg( criteria["chart_id"].toLower() ).arg(HYPEM_END_URL) ); QUrl url = QUrl( QString( HYPEM_URL "%1/%2" ).arg( criteria["chart_id"].toLower() ).arg(HYPEM_END_URL) );
qDebug() << Q_FUNC_INFO << "Getting chart url" << url; qDebug() << Q_FUNC_INFO << "Getting chart url" << url;
@@ -212,6 +201,7 @@ hypemPlugin::notInCacheSlot( QHash<QString, QString> criteria, Tomahawk::InfoSys
case InfoChartCapabilities: case InfoChartCapabilities:
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChartCapabilities not in cache! Fetching...";
if ( m_chartsFetchJobs > 0 ) if ( m_chartsFetchJobs > 0 )
{ {
qDebug() << Q_FUNC_INFO << "InfoChartCapabilities still fetching!"; qDebug() << Q_FUNC_INFO << "InfoChartCapabilities still fetching!";
@@ -390,7 +380,14 @@ hypemPlugin::chartReturned()
emit info( requestData, returnedData ); emit info( requestData, returnedData );
// TODO update cache // update cache
Tomahawk::InfoSystem::InfoStringHash criteria;
Tomahawk::InfoSystem::InfoStringHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
criteria[ "chart_id" ] = origData[ "chart_id" ];
criteria[ "chart_source" ] = origData[ "chart_source" ];
/// @todo
/// set cache time based on wether requested type is 3day, lastweek or recent.
emit updateCache( criteria, 86400000, requestData.type, returnedData );
} }
else else
qDebug() << "Network error in fetching chart:" << reply->url().toString(); qDebug() << "Network error in fetching chart:" << reply->url().toString();

View File

@@ -57,8 +57,13 @@ public slots:
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 );
virtual void pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input )
{
Q_UNUSED( caller )
Q_UNUSED( type)
Q_UNUSED( input )
}
virtual void pushInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant data );
private: private:
void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData );

View File

@@ -23,7 +23,6 @@
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QNetworkConfiguration> #include <QNetworkConfiguration>
#include <QNetworkReply> #include <QNetworkReply>
#include <QDomElement>
#include "album.h" #include "album.h"
#include "typedefs.h" #include "typedefs.h"
@@ -47,19 +46,6 @@ SpotifyPlugin::SpotifyPlugin()
m_supportedGetTypes << InfoChart << InfoChartCapabilities; m_supportedGetTypes << InfoChart << InfoChartCapabilities;
// we never need to re-fetch
if ( !m_allChartsMap.isEmpty() )
return;
/// We need to fetch possible types before they are asked for
tDebug() << "SpotifyPlugin: InfoChart fetching possible resources";
QUrl url = QUrl( QString( SPOTIFY_API_URL "toplist/charts" ) );
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
tDebug() << Q_FUNC_INFO << "fetching:" << url;
connect( reply, SIGNAL( finished() ), SLOT( chartTypes() ) );
m_chartsFetchJobs++;
} }
@@ -107,14 +93,6 @@ SpotifyPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
} }
void
SpotifyPlugin::pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input )
{
Q_UNUSED( caller )
Q_UNUSED( type)
Q_UNUSED( input )
}
void void
SpotifyPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ) SpotifyPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData )
{ {
@@ -123,17 +101,22 @@ SpotifyPlugin::fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData )
dataError( requestData ); dataError( requestData );
return; return;
} }
InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
Tomahawk::InfoSystem::InfoStringHash criteria; Tomahawk::InfoSystem::InfoStringHash criteria;
if ( !hash.contains( "chart_id" ) ) /// Each request needs to contain both a id and source
if ( !hash.contains( "chart_id" ) && !hash.contains( "chart_source" ) )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Hash did not contain required params!";
dataError( requestData ); dataError( requestData );
return; return;
} else {
criteria["chart_id"] = hash["chart_id"];
}
emit getCachedInfo( criteria, 604800000 /* Expire chart cache in 1 week */, requestData ); }
/// Set the criterias for current chart
criteria["chart_id"] = hash["chart_id"];
criteria["chart_source"] = hash["chart_source"];
emit getCachedInfo( criteria, 86400000 /* Expire chart cache in 1 day */, requestData );
} }
void void
SpotifyPlugin::fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData ) SpotifyPlugin::fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData requestData )
@@ -145,7 +128,8 @@ SpotifyPlugin::fetchChartCapabilities( Tomahawk::InfoSystem::InfoRequestData req
} }
Tomahawk::InfoSystem::InfoStringHash criteria; Tomahawk::InfoSystem::InfoStringHash criteria;
emit getCachedInfo( criteria, 0, requestData ); criteria[ "InfoChartCapabilities" ] = "spotifyplugin";
emit getCachedInfo( criteria, 604800000, requestData );
} }
void void
@@ -157,6 +141,7 @@ SpotifyPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, To
case InfoChart: case InfoChart:
{ {
/// Fetch the chart, we need source and id /// Fetch the chart, we need source and id
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChart not in cache! Fetching...";
QUrl url = QUrl( QString( SPOTIFY_API_URL "toplist/%1/" ).arg( criteria["chart_id"] ) ); QUrl url = QUrl( QString( SPOTIFY_API_URL "toplist/%1/" ).arg( criteria["chart_id"] ) );
qDebug() << Q_FUNC_INFO << "Getting chart url" << url; qDebug() << Q_FUNC_INFO << "Getting chart url" << url;
@@ -169,6 +154,21 @@ SpotifyPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, To
} }
case InfoChartCapabilities: case InfoChartCapabilities:
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoChartCapabilities not in cache! Fetching...";
// we never need to re-fetch
if ( !m_allChartsMap.isEmpty() )
return;
/// We need to fetch possible types before they are asked for
tDebug() << "SpotifyPlugin: InfoChart fetching possible resources";
QUrl url = QUrl( QString( SPOTIFY_API_URL "toplist/charts" ) );
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
tDebug() << Q_FUNC_INFO << "fetching:" << url;
connect( reply, SIGNAL( finished() ), SLOT( chartTypes() ) );
m_chartsFetchJobs++;
if ( m_chartsFetchJobs > 0 ) if ( m_chartsFetchJobs > 0 )
{ {
qDebug() << Q_FUNC_INFO << "InfoChartCapabilities still fetching!"; qDebug() << Q_FUNC_INFO << "InfoChartCapabilities still fetching!";
@@ -275,6 +275,9 @@ SpotifyPlugin::chartTypes()
foreach ( InfoRequestData request, m_cachedRequests ) foreach ( InfoRequestData request, m_cachedRequests )
{ {
emit info( request, m_allChartsMap ); emit info( request, m_allChartsMap );
Tomahawk::InfoSystem::InfoStringHash criteria;
criteria[ "InfoChartCapabilities" ] = "spotifyplugin";
emit updateCache( criteria,604800000, request.type, m_allChartsMap );
} }
m_cachedRequests.clear(); m_cachedRequests.clear();
} }
@@ -379,9 +382,14 @@ SpotifyPlugin::chartReturned()
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(); Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
emit info( requestData, returnedData ); emit info( requestData, returnedData );
// update cache
Tomahawk::InfoSystem::InfoStringHash criteria;
Tomahawk::InfoSystem::InfoStringHash origData = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
criteria[ "chart_id" ] = origData[ "chart_id" ];
criteria[ "chart_source" ] = origData[ "chart_source" ];
emit updateCache( criteria, 86400000, requestData.type, returnedData );
} }
else else
qDebug() << "Network error in fetching chart:" << reply->url().toString(); qDebug() << "Network error in fetching chart:" << reply->url().toString();

View File

@@ -57,7 +57,12 @@ public slots:
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 );
virtual void pushInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVariant data ); virtual void pushInfo( const QString caller, const Tomahawk::InfoSystem::InfoType type, const QVariant input )
{
Q_UNUSED( caller )
Q_UNUSED( type)
Q_UNUSED( input )
}
private: private:
void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData );