1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-17 19:37:09 +02:00

Only fetch invalid sources

This commit is contained in:
Hugo Lindström
2013-04-02 12:28:00 +02:00
parent be14446423
commit 3b3889e8ff
2 changed files with 80 additions and 34 deletions

View File

@@ -51,15 +51,17 @@ namespace InfoSystem
ChartsPlugin::ChartsPlugin() ChartsPlugin::ChartsPlugin()
: InfoPlugin() : InfoPlugin()
, m_chartsFetchJobs( 0 ) , m_chartsFetchJobs( 0 )
, m_fetchAll( true )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << QThread::currentThread(); tDebug( LOGVERBOSE ) << Q_FUNC_INFO << QThread::currentThread();
// If you add resource, update version aswell // If you add resource, update version as well
m_chartVersion = "2.6.3"; m_chartVersion = "2.6.3";
m_supportedGetTypes << InfoChart << InfoChartCapabilities; m_supportedGetTypes << InfoChart << InfoChartCapabilities;
// Charts that have geo or genre types // Charts that have geo or genre types
m_geoChartIds << "itunes" << "hotnewhiphop" << "djshop.de" << "rdio"; m_geoChartIds << "itunes" << "hotnewhiphop" << "djshop.de" << "rdio";
m_cacheIdentifier = TomahawkUtils::md5( QString("ChartsPlugin" + m_chartVersion ).toUtf8() );
} }
@@ -72,7 +74,7 @@ ChartsPlugin::~ChartsPlugin()
void void
ChartsPlugin::init() ChartsPlugin::init()
{ {
QVariant data = TomahawkUtils::Cache::instance()->getData( "ChartsPlugin", "chart_sources" ); QVariant data = TomahawkUtils::Cache::instance()->getData( m_cacheIdentifier, "chart_sources" );
if ( data.canConvert< QList< Tomahawk::InfoSystem::InfoStringHash > >() ) if ( data.canConvert< QList< Tomahawk::InfoSystem::InfoStringHash > >() )
{ {
const QList< Tomahawk::InfoSystem::InfoStringHash > sourceList = data.value< QList< Tomahawk::InfoSystem::InfoStringHash > >(); const QList< Tomahawk::InfoSystem::InfoStringHash > sourceList = data.value< QList< Tomahawk::InfoSystem::InfoStringHash > >();
@@ -87,20 +89,27 @@ ChartsPlugin::init()
} }
m_chartResources << sourceHash; m_chartResources << sourceHash;
} }
data = TomahawkUtils::Cache::instance()->getData( m_cacheIdentifier, "allCharts" );
if ( data.canConvert< QVariantMap >() )
{
m_allChartsMap = data.toMap();
if ( !m_allChartsMap.empty() )
m_fetchAll = false;
}
} }
else else
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Migrating"; tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Migrating";
m_refetchSource << "ALL";
} }
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "total sources" << m_chartResources.size() << m_chartResources; tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "total sources" << m_chartResources.size() << m_chartResources;
if( m_chartResources.size() == 0 || m_refetchSource.size() != 0 ) if( ( m_chartResources.size() == 0 || m_refetchSource.size() != 0 ) || m_fetchAll )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Will refetch on next request. Empty or Invalid CACHE" << m_chartResources.size() << m_refetchSource; tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Will refetch on next request. Empty or Invalid CACHE" << m_chartResources.size() << m_refetchSource << "fetchAll?" << m_fetchAll;
} }
} }
@@ -300,8 +309,6 @@ ChartsPlugin::chartSourcesList()
return; return;
} }
/// This will re/fetch all sources
/// @todo: Fetch only requested source
m_chartResources.clear(); m_chartResources.clear();
foreach ( const QVariant &rsource, sources ) foreach ( const QVariant &rsource, sources )
@@ -323,27 +330,42 @@ ChartsPlugin::chartSourcesList()
source_expire[ "chart_source" ] = source; source_expire[ "chart_source" ] = source;
source_expire[ "chart_expires" ] = QString::number(expires); source_expire[ "chart_expires" ] = QString::number(expires);
m_chartResources << source_expire; m_chartResources << source_expire;
}
if ( maxAge == 0 ) if( !m_fetchAll )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "MaxAge for " << source << " is 0. Fetching all"; if ( maxAge == 0 )
reply->setProperty( "only_source_list", false ); {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "MaxAge for " << source << " is 0. reFetching";
reply->setProperty( "only_source_list", false );
}
}
else
{
m_refetchSource << source;
}
} }
} }
/// We can store the source list for how long as we want /// We can store the source list for how long as we want
/// In init, we check expiration for each source, and refetch if invalid /// In init, we check expiration for each source, and refetch if invalid
/// 2 days seems fair enough though /// 2 days seems fair enough though
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "storing sources in cache" << m_chartResources; tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "storing sources in cache" << m_chartResources;
TomahawkUtils::Cache::instance()->putData( "ChartsPlugin", 172800000 /* 2 days */, "chart_sources", QVariant::fromValue< QList< Tomahawk::InfoSystem::InfoStringHash > > ( m_chartResources ) ); TomahawkUtils::Cache::instance()->putData( m_cacheIdentifier, 172800000 /* 2 days */, "chart_sources", QVariant::fromValue< QList< Tomahawk::InfoSystem::InfoStringHash > > ( m_chartResources ) );
if( !reply->property( "only_source_list" ).toBool() ) if( !reply->property( "only_source_list" ).toBool() )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Fetching all sources!";
fetchAllChartSources(); if ( !m_fetchAll )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "reFetching" << m_refetchSource;
fetchExpiredSources();
}
else
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Fetching all sources!" << m_refetchSource;
fetchAllChartSources();
m_fetchAll = false;
}
} }
} }
else else
@@ -351,26 +373,42 @@ ChartsPlugin::chartSourcesList()
} }
void
ChartsPlugin::fetchSource(const QString& source)
{
QUrl url = QUrl( QString( CHART_URL "charts/%1" ).arg( source ) );
TomahawkUtils::urlAddQueryItem( url, "version", TomahawkUtils::appFriendlyVersion() );
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
reply->setProperty( "chart_source", source );
tDebug() << Q_FUNC_INFO << "fetching:" << url;
connect( reply, SIGNAL( finished() ), SLOT( chartsList() ) );
m_chartsFetchJobs++;
}
void
ChartsPlugin::fetchExpiredSources()
{
if ( !m_refetchSource.isEmpty() )
{
foreach ( const QString& source, m_refetchSource )
{
fetchSource(source);
}
}
}
void void
ChartsPlugin::fetchAllChartSources() ChartsPlugin::fetchAllChartSources()
{ {
if ( !m_chartResources.isEmpty() && m_allChartsMap.isEmpty() ) if ( !m_chartResources.isEmpty() && m_allChartsMap.isEmpty() )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "InfoNewRelease fetching source data";
foreach ( const Tomahawk::InfoSystem::InfoStringHash source, m_chartResources ) foreach ( const Tomahawk::InfoSystem::InfoStringHash source, m_chartResources )
{ {
QUrl url = QUrl( QString( CHART_URL "charts/%1" ).arg( source[ "chart_source" ] ) ); fetchSource(source[ "chart_source" ]);
TomahawkUtils::urlAddQueryItem( url, "version", TomahawkUtils::appFriendlyVersion() );
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
reply->setProperty( "chart_source", source[ "chart_source" ] );
tDebug() << Q_FUNC_INFO << "fetching:" << url;
connect( reply, SIGNAL( finished() ), SLOT( chartsList() ) );
m_chartsFetchJobs++;
} }
} }
} }
@@ -608,6 +646,9 @@ ChartsPlugin::chartsList()
m_allChartsMap[ "defaults" ] = defaultMap; m_allChartsMap[ "defaults" ] = defaultMap;
m_allChartsMap[ "defaultSource" ] = "itunes"; m_allChartsMap[ "defaultSource" ] = "itunes";
m_allChartsMap.insert( chartName , QVariant::fromValue< QVariantMap >( charts ) ); m_allChartsMap.insert( chartName , QVariant::fromValue< QVariantMap >( charts ) );
m_refetchSource.removeOne( source );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Fetched " << source << " have " << m_refetchSource << "left";
} }
else else
{ {
@@ -620,15 +661,17 @@ ChartsPlugin::chartsList()
foreach ( InfoRequestData request, m_cachedRequests ) foreach ( InfoRequestData request, m_cachedRequests )
{ {
emit info( request, m_allChartsMap ); emit info( request, m_allChartsMap );
// update cache
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Updating cache with" << m_allChartsMap.size() << "charts"; tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Updating cache with" << m_allChartsMap.size() << "charts";
Tomahawk::InfoSystem::InfoStringHash criteria; Tomahawk::InfoSystem::InfoStringHash criteria;
criteria[ "InfoChartCapabilities" ] = "chartsplugin"; criteria[ "InfoChartCapabilities" ] = "chartsplugin";
criteria[ "InfoChartVersion" ] = m_chartVersion; criteria[ "InfoChartVersion" ] = m_chartVersion;
/// We can cache it the lot for 2 days, it will be checked on next request /// We can cache it the lot for 2 days, it will be checked on next request
emit updateCache( criteria, 172800000 /* 2 days */, request.type,m_allChartsMap ); emit updateCache( criteria, 172800000 /* 2 days */, request.type, m_allChartsMap );
} }
TomahawkUtils::Cache::instance()->putData( m_cacheIdentifier, 172800000 /* 2 days */, "allCharts", m_allChartsMap );
m_cachedRequests.clear(); m_cachedRequests.clear();
} }
} }
@@ -782,7 +825,6 @@ ChartsPlugin::getMaxAge( const QByteArray &rawHeader ) const
qlonglong expires = QString( rawHeader ).toLongLong( &ok ); qlonglong expires = QString( rawHeader ).toLongLong( &ok );
if ( ok ) if ( ok )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Got rawheader " << QString( rawHeader ) << ":" << expires;
return getMaxAge( expires ); return getMaxAge( expires );
} }
return 0; return 0;

View File

@@ -95,8 +95,10 @@ private:
* Updates the cache. * Updates the cache.
*/ */
void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData, const QString& source, const QString& chart_id ); void fetchChart( Tomahawk::InfoSystem::InfoRequestData requestData, const QString& source, const QString& chart_id );
void fetchSource( const QString& source );
void fetchChartFromCache( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChartFromCache( Tomahawk::InfoSystem::InfoRequestData requestData );
void fetchExpiredSources();
void fetchChartCapabilitiesFromCache( Tomahawk::InfoSystem::InfoRequestData requestData ); void fetchChartCapabilitiesFromCache( Tomahawk::InfoSystem::InfoRequestData requestData );
void dataError( Tomahawk::InfoSystem::InfoRequestData requestData ); void dataError( Tomahawk::InfoSystem::InfoRequestData requestData );
@@ -114,6 +116,8 @@ private:
QHash< QString, QString > m_cachedCountries; QHash< QString, QString > m_cachedCountries;
QPointer< QNetworkAccessManager > m_nam; QPointer< QNetworkAccessManager > m_nam;
QStringList m_geoChartIds; QStringList m_geoChartIds;
bool m_fetchAll;
QString m_cacheIdentifier;
}; };
} }