1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-13 20:41:58 +02:00

Utilize new detailed information, allowing any new sources after 0.6.99

This commit is contained in:
Hugo Lindström 2013-04-05 00:48:25 +02:00
parent a47e1043fc
commit 96c4bfd610
2 changed files with 74 additions and 110 deletions

View File

@ -55,12 +55,8 @@ ChartsPlugin::ChartsPlugin()
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << QThread::currentThread();
// If you add resource, update version as well
m_chartVersion = "2.6.4";
m_chartVersion = "2.6.5";
m_supportedGetTypes << InfoChart << InfoChartCapabilities;
// Charts that have geo or genre types
m_geoChartIds << "itunes" << "hotnewhiphop" << "djshop.de" << "rdio";
m_cacheIdentifier = TomahawkUtils::md5( QString("ChartsPlugin" + m_chartVersion ).toUtf8() );
}
@ -401,6 +397,7 @@ ChartsPlugin::fetchExpiredSources()
}
}
void
ChartsPlugin::fetchAllChartSources()
{
@ -441,7 +438,7 @@ ChartsPlugin::chartsList()
{
QJson::Parser p;
bool ok;
const QVariantMap res = p.parse( reply, &ok ).toMap();
QVariantMap res = p.parse( reply, &ok ).toMap();
if ( !ok )
{
@ -450,8 +447,12 @@ ChartsPlugin::chartsList()
return;
}
/// Got types, append!
const QString source = reply->property("chart_source").toString();
const QVariantMap details = res.take( "details" ).toMap();
const QVariantMap response = res;
const bool haveExtra = details.value( "have_extra", false ).toBool();
// This is what we use as ID
const QString source = reply->property( "chart_source" ).toString();
const QString prettyName = details.value( "name", source ).toString();
const qlonglong expires = QString( reply->rawHeader( QString( "Expires" ).toLocal8Bit() ) ).toLongLong( &ok );
if ( !ok )
@ -463,27 +464,29 @@ ChartsPlugin::chartsList()
// We'll populate charts with the data from the server
QVariantMap charts;
QString chartName;
QStringList defaultChain;
if ( m_geoChartIds.contains( source ) )
if ( haveExtra )
{
// Some charts can have an extra param, itunes has geo, WAH has emerging/mainstream
// Some charts can have an extra param, itunes has geo, HNHH has upcomming/all/mainstream
// Itunes has geographic-area based charts. So we build a breadcrumb of
// ITunes - Country - Albums - Top Chart Type
// - Tracks - Top Chart Type
// NameID - Extra - Type -> Chart Name
QHash< QString, QVariantMap > extraType;
QStringList processed;
foreach ( const QVariant& chartObj, res.values() )
foreach ( const QVariant& chartObj, response.values() )
{
if ( !chartObj.toMap().isEmpty() )
{
const QVariantMap chart = chartObj.toMap();
const QString id = chart.value( "id" ).toString();
const QString geo = chart.value( "geo" ).toString();
QString name = chart.value( "genre" ).toString();
const QString type = QString( chart.value( "type" ).toString() + "s" );
const QString name = chart.value( "display_name" ).toString();
const QString type = QString( chart.value( "type" ).toString() );
const bool isDefault = ( chart.contains( "default" ) && chart[ "default" ].toInt() == 1 );
QString extra;
// Hack!
// Japan charts contains multiple duplicates, all which are linked
@ -496,81 +499,37 @@ ChartsPlugin::chartsList()
processed << name;
}
QString extra;
if ( !geo.isEmpty() )
{
if ( !m_cachedCountries.contains( geo ) )
{
extra = Tomahawk::CountryUtils::fullCountryFromCode( geo );
for ( int i = 1; i < extra.size(); i++ )
{
if ( extra.at( i ).isUpper() )
{
extra.insert( i, " " );
i++;
}
}
m_cachedCountries[ geo ] = extra;
}
else
extra = m_cachedCountries[ geo ];
}
extra = countryName( geo );
else
extra = chart.value( "extra" ).toString();
if ( source == "hotnewhiphop" || source == "djshop.de" || source == "rdio" )
name = chart.value( "name" ).toString();
if ( name.isEmpty() ) // not a specific chart, an all chart
name = tr( "Top Overall" );
InfoStringHash c;
c[ "id" ] = id;
c[ "label" ] = name;
c[ "type" ] = "album";
if ( isDefault )
c[ "default" ] = "true";
c[ "type" ] = type;
/// If this item has expired, set it to 0.
if ( isDefault )
{
c[ "default" ] = "true";
defaultChain.clear();
defaultChain.append( extra );
defaultChain.append( type );
defaultChain.append( name );
}
// If this item has expired, set it to 0.
c[ "expires" ] = ( ok ? QString::number (expires ) : QString::number( 0 ) );
QList< Tomahawk::InfoSystem::InfoStringHash > extraTypeData = extraType[ extra ][ type ].value< QList< Tomahawk::InfoSystem::InfoStringHash > >();
extraTypeData.append( c );
extraType[ extra ][ type ] = QVariant::fromValue< QList< Tomahawk::InfoSystem::InfoStringHash > >( extraTypeData );
if ( isDefault )
{
defaultChain.clear();
defaultChain.append( extra );
defaultChain.append( type );
defaultChain.append( name );
}
}
foreach ( const QString& c, extraType.keys() )
{
charts[ c ] = extraType[ c ];
}
if ( source == "itunes" )
{
chartName = "iTunes";
}
else if ( source == "soundcloudwall" )
{
chartName = "SoundCloudWall";
}
else if ( source == "hotnewhiphop" )
{
chartName = "HotNewHiphop";
}
else if ( source == "djshop.de" )
{
chartName = "DjShop.de";
}
else if ( source == "rdio" )
{
chartName = "Rdio";
}
}
}
else
@ -582,47 +541,36 @@ ChartsPlugin::chartsList()
QList< InfoStringHash > trackCharts;
QList< InfoStringHash > artistCharts;
foreach ( const QVariant& chartObj, res.values() )
foreach ( const QVariant& chartObj, response.values() )
{
if ( !chartObj.toMap().isEmpty() )
{
const QVariantMap chart = chartObj.toMap();
const QString type = chart.value( "type" ).toString();
const QString name = chart.value( "display_name" ).toString();
const bool isDefault = ( chart.contains( "default" ) && chart[ "default" ].toInt() == 1 );
InfoStringHash c;
c[ "id" ] = chart.value( "id" ).toString();
c[ "label" ] = name;
c[ "expires" ] = ( ok ? QString::number( expires ) : QString::number( 0 ) );
if ( chart.value( "genre").isValid() )
c[ "label" ] = chart.value( "genre" ).toString();
else
c[ "label" ] = chart.value( "name" ).toString();
c[ "type" ] = type.toLower();
if ( isDefault )
{
c[ "default" ] = "true";
if ( type == "Album" )
{
c[ "type" ] = "album";
albumCharts.append( c );
}
else if ( type == "Track" )
{
c[ "type" ] = "tracks";
trackCharts.append( c );
}
else if ( type == "Artist" )
{
c[ "type" ] = "artists";
artistCharts.append( c );
}
if ( isDefault )
{
defaultChain.clear();
defaultChain.append( type + "s" ); //UGLY but it's plural to the user, see below
defaultChain.append( type );
defaultChain.append( c[ "label" ] );
}
if ( type == "Albums" )
albumCharts.append( c );
else if ( type == "Tracks" )
trackCharts.append( c );
else if ( type == "Artists" )
artistCharts.append( c );
}
if ( !artistCharts.isEmpty() )
@ -632,21 +580,16 @@ ChartsPlugin::chartsList()
if ( !trackCharts.isEmpty() )
charts.insert( tr( "Tracks" ), QVariant::fromValue< QList< Tomahawk::InfoSystem::InfoStringHash > >( trackCharts ) );
/// @note For displaying purposes, upper the first letter
/// @note Remeber to lower it when fetching this!
chartName = source;
chartName[0] = chartName[0].toUpper();
}
}
/// Add the possible charts and its types to breadcrumb
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Adding to charts:" << chartName;
// Add the possible charts and its types to breadcrumb
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Adding to charts:" << prettyName;
QVariantMap defaultMap = m_allChartsMap.value( "defaults" ).value< QVariantMap >();
defaultMap[ source ] = defaultChain;
m_allChartsMap[ "defaults" ] = defaultMap;
m_allChartsMap[ "defaultSource" ] = "itunes";
m_allChartsMap.insert( chartName , QVariant::fromValue< QVariantMap >( charts ) );
m_allChartsMap.insert( prettyName , QVariant::fromValue< QVariantMap >( charts ) );
m_refetchSource.removeOne( source );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Fetched " << source << " have " << m_refetchSource << "left";
}
@ -818,6 +761,27 @@ ChartsPlugin::chartReturned()
}
}
QString
ChartsPlugin::countryName( const QString& cc )
{
if ( m_cachedCountries.contains( cc ) )
return m_cachedCountries[ cc ];
QString name = Tomahawk::CountryUtils::fullCountryFromCode( cc );
for ( int i = 1; i < name.size(); i++ )
{
if ( name.at( i ).isUpper() )
{
name.insert( i, " " );
i++;
}
}
m_cachedCountries[ cc ] = name;
return name;
}
qlonglong
ChartsPlugin::getMaxAge( const QByteArray &rawHeader ) const
{

View File

@ -49,7 +49,6 @@ public:
Track = 0x01,
Album = 0x02,
Artist = 0x04
};
void setChartType( ChartType type ) { m_chartType = type; }
@ -102,22 +101,23 @@ private:
void fetchChartCapabilitiesFromCache( Tomahawk::InfoSystem::InfoRequestData requestData );
void dataError( Tomahawk::InfoSystem::InfoRequestData requestData );
QString countryName( const QString& cc );
qlonglong getMaxAge( const QByteArray &rawHeader ) const;
qlonglong getMaxAge( const qlonglong expires ) const;
QVariantMap m_allChartsMap;
QHash< QString, QString > m_cachedCountries;
QList< Tomahawk::InfoSystem::InfoStringHash > m_chartResources;
QList< InfoRequestData > m_cachedRequests;
QStringList m_refetchSource;
QString m_cacheIdentifier;
QString m_chartVersion;
ChartType m_chartType;
QVariantMap m_allChartsMap;
uint m_chartsFetchJobs;
QList< InfoRequestData > m_cachedRequests;
QHash< QString, QString > m_cachedCountries;
QPointer< QNetworkAccessManager > m_nam;
QStringList m_geoChartIds;
bool m_fetchAll;
QString m_cacheIdentifier;
};
}