1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-20 15:59:42 +01:00

Resulthintcheck

This commit is contained in:
Hugo Lindström 2012-12-03 20:14:01 +01:00
parent 4db3ea8d36
commit c5dd1ebf2f
11 changed files with 464 additions and 100 deletions

View File

@ -37,8 +37,8 @@
#include "utils/TomahawkCache.h"
#include "Source.h"
#define CHART_URL "http://charts.tomahawk-player.org/"
//#define CHART_URL "http://localhost:8080/"
//#define CHART_URL "http://charts.tomahawk-player.org/"
#define CHART_URL "http://localhost:8080/"
#include <qjson/parser.h>
#include <qjson/serializer.h>
@ -665,8 +665,11 @@ ChartsPlugin::chartReturned()
else
setChartType( None );
int count = 0;
foreach ( const QVariant& chartR, chartResponse )
{
if( count++ >= 1)
break;
QString title, artist, album, streamUrl;
QVariantMap chartMap = chartR.toMap();

View File

@ -120,6 +120,8 @@ set( libGuiSources
utils/BinaryInstallerHelper.cpp
utils/BinaryExtractWorker.cpp
utils/SharedTimeLine.cpp
utils/ResultHintChecker.cpp
utils/CustomResultHintChecker.cpp
utils/WebResultHintChecker.cpp
utils/NetworkReply.cpp

View File

@ -35,7 +35,7 @@
#include "PlayableItem.h"
#include "utils/TomahawkUtils.h"
#include "utils/Logger.h"
#include <utils/WebResultHintChecker.h>
#include <utils/ResultHintChecker.h>
using namespace Tomahawk;
@ -281,7 +281,7 @@ PlaylistModel::insertEntries( const QList< Tomahawk::plentry_ptr >& entries, int
connect( plitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
WebResultHintChecker::checkQuery( entry->query() );
ResultHintChecker::checkQuery( entry->query() );
}
if ( !m_waitingForResolved.isEmpty() )

View File

@ -0,0 +1,119 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CustomResultHintChecker.h"
#include "ResultHintChecker.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QXmlStreamReader>
#include "Query.h"
#include "utils/NetworkReply.h"
#include "utils/Closure.h"
using namespace Tomahawk;
CustomResultHintChecker::CustomResultHintChecker( const query_ptr& q )
: ResultHintChecker( q )
, m_query( q )
{
if( !isValid() )
{
qDebug() << Q_FUNC_INFO << "invalid:" << url();
return;
}
qDebug() << Q_FUNC_INFO << url();
handleResultHint();
}
void
CustomResultHintChecker::handleResultHint()
{
if ( url().startsWith( "hnhh" ) )
{
QUrl httpUrl = QUrl::fromUserInput( url() );
httpUrl.setScheme( "http" );
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( httpUrl ) ) );
connect( this, SIGNAL( result( result_ptr ) ), this, SLOT( handleHnhhResult( result_ptr ) ) );
connect( reply, SIGNAL( finished() ), SLOT( hnhhFinished() ) );
return;
}
}
void
CustomResultHintChecker::handleHnhhResult( result_ptr result )
{
if( !result.isNull() )
{
qDebug() << Q_FUNC_INFO << "Setting expiration for hotnewhiphop" << result->toString();
qDebug() << Q_FUNC_INFO << url() << resultHint();
}
}
void
CustomResultHintChecker::hnhhFinished()
{
NetworkReply* r = qobject_cast<NetworkReply*>( sender() );
r->deleteLater();
bool foundStreamable = false;
// Intentionally accepts unknown error
if ( r->reply()->error() != ( QNetworkReply::NoError | QNetworkReply::UnknownNetworkError ) )
{
QXmlStreamReader xmlStream( r->reply()->readAll() );
if ( xmlStream.error() != QXmlStreamReader::NoError )
{
qDebug() << "XML ERROR!" << xmlStream.errorString();
}
else
{
while (!xmlStream.atEnd() )
{
xmlStream.readNext();
if ( xmlStream.isStartElement() )
{
if ( xmlStream.name().toString() == "song" )
{
const QString streamUrl = QString( xmlStream.attributes().value("filename").toLatin1() );
// Dont save this resulthint, it needs to revalidate
qDebug() << Q_FUNC_INFO << "GOT STREAMABLE " << streamUrl;
setResultHint( streamUrl, false );
foundStreamable = true;
break;
}
}
}
}
}
if ( !foundStreamable)
{
removeHint();
}
deleteLater();
}

View File

@ -0,0 +1,53 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef Custom_RESULT_HINT_CHECKER_H
#define Custom_RESULT_HINT_CHECKER_H
#include "Typedefs.h"
#include <QUrl>
#include <QObject>
#include "ResultHintChecker.h"
namespace Tomahawk
{
/**
* @brief The CustomResultHintChecker class
* This class is to be used with custom protocol to retrieve
* streamable urls for resulthints.
*/
class CustomResultHintChecker : public ResultHintChecker
{
Q_OBJECT
public:
CustomResultHintChecker( const query_ptr& q );
virtual ~CustomResultHintChecker(){}
private slots:
/* HotNewHiphop specific */
void hnhhFinished();
void handleHnhhResult( result_ptr result );
private:
void handleResultHint();
QString m_streamUrl;
query_ptr m_query;
};
}
#endif

View File

@ -0,0 +1,201 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "WebResultHintChecker.h"
#include "CustomResultHintChecker.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include "Query.h"
#include "Result.h"
#include "Source.h"
#include "Pipeline.h"
#include "utils/NetworkReply.h"
#include "utils/Logger.h"
using namespace Tomahawk;
ResultHintChecker::ResultHintChecker( const query_ptr& q )
: QObject( 0 )
, m_query( q )
, m_isValid( false )
{
Q_ASSERT( !m_query.isNull() );
m_url = q->resultHint();
connect( m_query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( onResolvingFinished( bool ) ) );
connect( m_query.data(), SIGNAL( resultsRemoved( const Tomahawk::result_ptr& ) ), this, SLOT( onResultsRemoved( const Tomahawk::result_ptr& ) ) );
connect( m_query.data(), SIGNAL( resultsChanged() ), this, SLOT( onResultsChanged() ) );
connect( m_query.data(), SIGNAL( updated() ), this, SLOT( onResultsChanged() ) );
connect( m_query.data(), SIGNAL( resultsAdded( const QList<Tomahawk::result_ptr>& ) ), this, SLOT( onResultsAdded( const QList<Tomahawk::result_ptr>& ) ) );
check( m_url );
}
ResultHintChecker::~ResultHintChecker()
{
}
/**
* @brief ResultHintChecker::checkQuery
* @param query
* Static function to initiate resulthint checkers
*/
void
ResultHintChecker::checkQuery( const query_ptr& query )
{
if ( !query->resultHint().isEmpty() && TomahawkUtils::whitelistedHttpResultHint( query->resultHint() ) )
new WebResultHintChecker( query );
else if ( !query->resultHint().isEmpty() && TomahawkUtils::whitelistedCustomProtocolResultHint( query->resultHint() ) )
new CustomResultHintChecker( query );
}
void
ResultHintChecker::onResultsRemoved(const result_ptr &result)
{
qDebug() << Q_FUNC_INFO << result->toString();
}
void
ResultHintChecker::onResultsChanged()
{
foreach( const result_ptr& rp, m_query->results() )
{
qDebug() << Q_FUNC_INFO << rp->toString();
}
}
void
ResultHintChecker::checkQueries( const QList< query_ptr >& queries )
{
foreach ( const query_ptr& query, queries )
checkQuery( query );
}
void
ResultHintChecker::onResolvingFinished( bool hasResults )
{
foreach ( const result_ptr& result, m_query->results() )
{
qDebug() << Q_FUNC_INFO << "result url():" << result->url();
}
}
void
ResultHintChecker::onResultsAdded( const QList<Tomahawk::result_ptr>& results )
{
foreach( const result_ptr& result, results )
{
qDebug() << Q_FUNC_INFO << "result url():" << result->url();
}
}
QString
ResultHintChecker::resultHint() const
{
return m_query->resultHint();
}
bool
ResultHintChecker::isValid()
{
return m_isValid;
}
void
ResultHintChecker::check( const QUrl &url )
{
qDebug() << Q_FUNC_INFO << url;
// Nothing to do
if ( url.isEmpty() ||
( !TomahawkUtils::whitelistedHttpResultHint( url.toString() ) &&
!TomahawkUtils::whitelistedCustomProtocolResultHint( url.toString() )
) )
{
if ( !url.isEmpty() || m_query->saveHTTPResultHint() )
removeHint();
deleteLater();
return;
}
m_isValid = true;
}
result_ptr
ResultHintChecker::getResultPtr()
{
result_ptr foundResult;
foreach ( const result_ptr& result, m_query->results() )
{
if ( result->url() == m_url )
{
foundResult = result;
break;
}
}
return foundResult;
}
void
ResultHintChecker::setResultHint(const QString &url, const bool saveHint)
{
qDebug() << Q_FUNC_INFO << "Setting new resulthint" << url << "save it? " << saveHint;
result_ptr foundResult = getResultPtr();
if ( !foundResult.isNull() )
{
qDebug() << Q_FUNC_INFO << "Removing old result " << foundResult->toString();
m_query->removeResult( foundResult );
}
m_query->setResultHint( url );
m_query->setSaveHTTPResultHint( saveHint );
}
void
ResultHintChecker::removeHint()
{
tLog() << "Removing invalid resulthint from query:" << m_url;
result_ptr foundResult = getResultPtr();
if ( !foundResult.isNull() )
m_query->removeResult( foundResult );
if ( m_query->resultHint() == m_url )
{
m_query->setResultHint( QString() );
}
m_query->setSaveHTTPResultHint( false );
}

View File

@ -0,0 +1,66 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef RESULT_HINT_CHECKER_H
#define RESULT_HINT_CHECKER_H
#include "Typedefs.h"
#include <QObject>
namespace Tomahawk
{
class ResultHintChecker : public QObject
{
Q_OBJECT
public:
explicit ResultHintChecker( const query_ptr& q );
virtual ~ResultHintChecker();
static void checkQuery( const query_ptr& query );
static void checkQueries( const QList< query_ptr >& queries );
bool isValid();
QString url() const { return m_url; }
QString resultHint() const;
void setUrl( const QString& url ) { m_url = url; }
void setResultHint( const QString &url, const bool saveHint );
void removeHint();
signals:
void result( result_ptr );
private slots:
void onResolvingFinished( bool hasResults );
void onResultsAdded( const QList<Tomahawk::result_ptr>& results );
void onResultsRemoved( const Tomahawk::result_ptr& result );
void onResultsChanged();
private:
void check( const QUrl& url );
result_ptr getResultPtr();
query_ptr m_query;
QString m_url;
bool m_isValid;
};
}
#endif

View File

@ -950,6 +950,12 @@ whitelistedHttpResultHint( const QString& url )
}
bool
whitelistedCustomProtocolResultHint( const QString& url )
{
return url.startsWith( "hnhh" );
}
} // ns
#include "TomahawkUtils.moc"

View File

@ -129,6 +129,7 @@ namespace TomahawkUtils
void copyWithAuthentication( const QString& srcFile, const QDir dest, QObject* receiver );
DLLEXPORT bool whitelistedHttpResultHint( const QString& url );
DLLEXPORT bool whitelistedCustomProtocolResultHint( const QString& url );
/**
* This helper is designed to help "update" an existing playlist with a newer revision of itself.

View File

@ -1,7 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
*
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
@ -17,107 +17,29 @@
*/
#include "WebResultHintChecker.h"
#include "ResultHintChecker.h"
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include "Query.h"
#include "Result.h"
#include "Source.h"
#include "Pipeline.h"
#include "utils/NetworkReply.h"
#include "utils/Logger.h"
using namespace Tomahawk;
WebResultHintChecker::WebResultHintChecker( const query_ptr& q )
: QObject( 0 )
, m_query( q )
: ResultHintChecker( q )
{
Q_ASSERT( !m_query.isNull() );
m_url = q->resultHint();
if ( Pipeline::instance()->isResolving( m_query ) )
connect( m_query.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( onResolvingFinished( bool ) ) );
else
check( QUrl::fromUserInput( m_url ) );
}
WebResultHintChecker::~WebResultHintChecker()
{
}
void
WebResultHintChecker::checkQuery( const query_ptr& query )
{
if ( !query->resultHint().isEmpty() && query->resultHint().startsWith( "http" ) )
new WebResultHintChecker( query );
}
void
WebResultHintChecker::checkQueries( const QList< query_ptr >& queries )
{
foreach ( const query_ptr& query, queries )
checkQuery( query );
}
void
WebResultHintChecker::onResolvingFinished( bool hasResults )
{
Q_UNUSED( hasResults );
check( QUrl::fromUserInput( m_url ) );
}
void
WebResultHintChecker::check( const QUrl &url )
{
// Nothing to do
if ( url.isEmpty() || !url.toString().startsWith( "http" ) )
if( !isValid() )
{
if ( !url.isEmpty() || m_query->saveHTTPResultHint() )
removeHint();
deleteLater();
return;
}
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( url ) ) );
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->head( QNetworkRequest( QUrl::fromUserInput( url() ) ) ) );
connect( reply, SIGNAL( finished() ), SLOT( headFinished() ) );
}
void
WebResultHintChecker::removeHint()
{
tLog() << "Removing HTTP result from query since HEAD request failed to verify it was a valid url:" << m_url;
result_ptr foundResult;
foreach ( const result_ptr& result, m_query->results() )
{
if ( result->url() == m_url )
{
foundResult = result;
break;
}
}
if ( !foundResult.isNull() )
m_query->removeResult( foundResult );
if ( m_query->resultHint() == m_url )
m_query->setResultHint( QString() );
m_query->setSaveHTTPResultHint( false );
}
void
WebResultHintChecker::headFinished()
{

View File

@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2012, Leo Franchi <lfranchi@kde.org>
* Copyright 2012, Hugo Lindström <hugolm84@gmail.com>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -19,31 +20,21 @@
#define WEB_RESULT_HINT_CHECKER_H
#include "Typedefs.h"
#include <QObject>
#include "ResultHintChecker.h"
namespace Tomahawk
{
class WebResultHintChecker : public QObject
class WebResultHintChecker : public ResultHintChecker
{
Q_OBJECT
public:
WebResultHintChecker( const query_ptr& q );
virtual ~WebResultHintChecker();
static void checkQuery( const query_ptr& query );
static void checkQueries( const QList< query_ptr >& queries );
virtual ~WebResultHintChecker(){}
private slots:
void headFinished();
void check( const QUrl& url );
void onResolvingFinished( bool hasResults );
private:
void removeHint();
query_ptr m_query;
QString m_url;
};