1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-01-29 12:39:28 +01:00

Support album and artist results in JSResolvers

This commit is contained in:
Dominik Schmidt 2016-02-16 13:25:27 +01:00
parent dee504c997
commit 4dadbb5acd
4 changed files with 60 additions and 6 deletions

View File

@ -563,15 +563,18 @@ JSResolver::onResolveRequestDone( const QVariantMap& data )
}
else
{
QList< artist_ptr > artists = scriptAccount()->parseArtistVariantList( data.value( "artists" ).toList() );
Tomahawk::Pipeline::instance()->reportArtists( qid, artists );
QList< album_ptr > albums = scriptAccount()->parseAlbumVariantList( data.value( "albums" ).toList() );
Tomahawk::Pipeline::instance()->reportAlbums( qid, albums );
QList< Tomahawk::result_ptr > results = scriptAccount()->parseResultVariantList( data.value( "tracks" ).toList() );
foreach( const result_ptr& result, results )
{
result->setResolvedByResolver( this );
result->setFriendlySource( name() );
}
Tomahawk::Pipeline::instance()->reportResults( qid, this, results );
}

View File

@ -32,6 +32,8 @@
#include "ScriptInfoPlugin.h"
// TODO:
#include "../Artist.h"
#include "../Album.h"
#include "../Result.h"
#include "../Track.h"
#include <QTime>
@ -268,6 +270,48 @@ ScriptAccount::onJobDeleted( const QString& jobId )
}
QList< Tomahawk::artist_ptr >
ScriptAccount::parseArtistVariantList( const QVariantList& artistList )
{
QList< Tomahawk::artist_ptr > artists;
QString artist;
foreach( const QVariant& a, artistList )
{
artist = a.toString().trimmed();
if ( artist.isEmpty() )
continue;
artists << Tomahawk::Artist::get( artist );
}
return artists;
}
QList< Tomahawk::album_ptr >
ScriptAccount::parseAlbumVariantList( const QVariantList& albumList )
{
QList< Tomahawk::album_ptr > albums;
QString artistString;
QString albumString;
foreach( const QVariant& av, albumList )
{
QVariantMap m = av.toMap();
artistString = m.value( "artist" ).toString().trimmed();
albumString = m.value( "album" ).toString().trimmed();
if ( artistString.isEmpty() || albumString.isEmpty() )
continue;
albums << Tomahawk::Album::get( Tomahawk::Artist::get( artistString ), albumString );
}
return albums;
}
QList< Tomahawk::result_ptr >
ScriptAccount::parseResultVariantList( const QVariantList& reslist )
{

View File

@ -79,6 +79,8 @@ public:
virtual void showDebugger();
// helpers
QList< Tomahawk::artist_ptr > parseArtistVariantList( const QVariantList& artistList );
QList< Tomahawk::album_ptr > parseAlbumVariantList( const QVariantList& albumList );
QList< Tomahawk::result_ptr > parseResultVariantList( const QVariantList& reslist );
ScriptJob* resolve( const scriptobject_ptr& scriptObject, const query_ptr& query, const QString& resolveType );

View File

@ -372,13 +372,18 @@ SearchWidget::onAlbumsFound( const QList<Tomahawk::album_ptr>& albums )
int distance = TomahawkUtils::levenshtein( m_search, album->name() );
int maxlen = qMax( m_search.length(), album->name().length() );
float score = (float)( maxlen - distance ) / maxlen;
float scoreAlbum = (float)( maxlen - distance ) / maxlen;
if ( score <= 0.1 )
distance = TomahawkUtils::levenshtein( m_search, album->artist()->name() );
maxlen = qMax( m_search.length(), album->artist()->name().length() );
float scoreArtist = (float)( maxlen - distance ) / maxlen;
float scoreMax = qMax( scoreAlbum, scoreArtist );
if ( scoreMax <= 0.1 )
continue;
m_albums.insert( album, score );
// tDebug() << Q_FUNC_INFO << "found album:" << album->name() << "score:" << score;
m_albums.insert( album, scoreMax );
// tDebug() << Q_FUNC_INFO << "found album:" << album->name() << "scoreMax:" << scoreMax;
}
// updateAlbums();