@@ -19,6 +19,8 @@
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <QObject>
|
||||
#include <QTimer>
|
||||
#include <QList>
|
||||
@@ -30,10 +32,11 @@
|
||||
#include "lyricsprovider.h"
|
||||
#include "lyricsproviders.h"
|
||||
|
||||
const int LyricsFetcherSearch::kSearchTimeoutMs = 6000;
|
||||
const int LyricsFetcherSearch::kSearchTimeoutMs = 3000;
|
||||
const int LyricsFetcherSearch::kGoodLyricsLength = 60;
|
||||
const float LyricsFetcherSearch::kHighScore = 2.5;
|
||||
|
||||
LyricsFetcherSearch::LyricsFetcherSearch(
|
||||
const LyricsSearchRequest &request, QObject *parent)
|
||||
LyricsFetcherSearch::LyricsFetcherSearch(const LyricsSearchRequest &request, QObject *parent)
|
||||
: QObject(parent),
|
||||
request_(request),
|
||||
cancel_requested_(false) {
|
||||
@@ -53,7 +56,11 @@ void LyricsFetcherSearch::TerminateSearch() {
|
||||
|
||||
void LyricsFetcherSearch::Start(LyricsProviders *lyrics_providers) {
|
||||
|
||||
for (LyricsProvider *provider : lyrics_providers->List()) {
|
||||
QList<LyricsProvider*> lyrics_providers_sorted = lyrics_providers->List();
|
||||
std::stable_sort(lyrics_providers_sorted.begin(), lyrics_providers_sorted.end(), ProviderCompareOrder);
|
||||
|
||||
for (LyricsProvider *provider : lyrics_providers_sorted) {
|
||||
if (!provider->is_enabled() || !provider->IsAuthenticated()) continue;
|
||||
connect(provider, SIGNAL(SearchFinished(quint64, LyricsSearchResults)), SLOT(ProviderSearchFinished(quint64, LyricsSearchResults)));
|
||||
const int id = lyrics_providers->NextId();
|
||||
const bool success = provider->StartSearch(request_.artist, request_.album, request_.title, id);
|
||||
@@ -70,13 +77,37 @@ void LyricsFetcherSearch::ProviderSearchFinished(const quint64 id, const LyricsS
|
||||
LyricsProvider *provider = pending_requests_.take(id);
|
||||
|
||||
LyricsSearchResults results_copy(results);
|
||||
for (int i = 0; i < results_copy.count(); ++i) {
|
||||
float higest_score = 0.0;
|
||||
for (int i = 0 ; i < results_copy.count() ; ++i) {
|
||||
results_copy[i].provider = provider->name();
|
||||
results_copy[i].score = 0.0;
|
||||
if (results_copy[i].artist.toLower() == request_.artist.toLower()) {
|
||||
results_copy[i].score += 0.5;
|
||||
}
|
||||
if (results_copy[i].album.toLower() == request_.album.toLower()) {
|
||||
results_copy[i].score += 0.5;
|
||||
}
|
||||
if (results_copy[i].title.toLower() == request_.title.toLower()) {
|
||||
results_copy[i].score += 0.5;
|
||||
}
|
||||
if (results_copy[i].artist.toLower() != request_.artist.toLower() && results_copy[i].title.toLower() != request_.title.toLower()) {
|
||||
results_copy[i].score -= 1.5;
|
||||
}
|
||||
if (results_copy[i].lyrics.length() > kGoodLyricsLength) results_copy[i].score += 1.0;
|
||||
if (results_copy[i].score > higest_score) higest_score = results_copy[i].score;
|
||||
}
|
||||
|
||||
results_.append(results_copy);
|
||||
std::stable_sort(results_.begin(), results_.end(), LyricsSearchResultCompareScore);
|
||||
|
||||
if (!pending_requests_.isEmpty()) {
|
||||
if (!results_.isEmpty() && higest_score >= kHighScore) { // Highest score, no need to wait for other providers.
|
||||
qLog(Debug) << "Got lyrics with high score from" << results_.last().provider << "for" << request_.artist << request_.title << "score" << results_.last().score << "finishing search.";
|
||||
TerminateSearch();
|
||||
}
|
||||
else {
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -89,14 +120,10 @@ void LyricsFetcherSearch::AllProvidersFinished() {
|
||||
if (cancel_requested_) return;
|
||||
|
||||
if (!results_.isEmpty()) {
|
||||
LyricsSearchResult result_use;
|
||||
result_use.score = 0.0;
|
||||
for (LyricsSearchResult result : results_) {
|
||||
if (result_use.lyrics.isEmpty() || result.score > result_use.score) result_use = result;
|
||||
}
|
||||
qLog(Debug) << "Using lyrics from" << result_use.provider << "for" << request_.artist << request_.title << "with score" << result_use.score;
|
||||
emit LyricsFetched(request_.id, result_use.provider, result_use.lyrics);
|
||||
qLog(Debug) << "Using lyrics from" << results_.last().provider << "for" << request_.artist << request_.title << "with score" << results_.last().score;
|
||||
emit LyricsFetched(request_.id, results_.last().provider, results_.last().lyrics);
|
||||
}
|
||||
|
||||
emit SearchFinished(request_.id, results_);
|
||||
|
||||
}
|
||||
@@ -111,3 +138,11 @@ void LyricsFetcherSearch::Cancel() {
|
||||
|
||||
}
|
||||
|
||||
bool LyricsFetcherSearch::ProviderCompareOrder(LyricsProvider *a, LyricsProvider *b) {
|
||||
return a->order() < b->order();
|
||||
}
|
||||
|
||||
bool LyricsFetcherSearch::LyricsSearchResultCompareScore(const LyricsSearchResult &a, const LyricsSearchResult &b) {
|
||||
return a.score < b.score;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user