mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-09 07:36:48 +02:00
Expand bit.ly, t.co, and j.mp links to spotify tracks
This commit is contained in:
@@ -168,6 +168,7 @@ set( libSources
|
||||
utils/xspfgenerator.cpp
|
||||
utils/jspfloader.cpp
|
||||
utils/spotifyparser.cpp
|
||||
utils/shortenedlinkparser.cpp
|
||||
|
||||
widgets/newplaylistwidget.cpp
|
||||
widgets/searchwidget.cpp
|
||||
@@ -340,6 +341,7 @@ set( libHeaders
|
||||
utils/xspfgenerator.h
|
||||
utils/jspfloader.h
|
||||
utils/spotifyparser.h
|
||||
utils/shortenedlinkparser.h
|
||||
|
||||
widgets/newplaylistwidget.h
|
||||
widgets/searchwidget.h
|
||||
|
@@ -45,6 +45,7 @@
|
||||
#include "utils/jspfloader.h"
|
||||
#include <QMimeData>
|
||||
#include "utils/spotifyparser.h"
|
||||
#include "utils/shortenedlinkparser.h"
|
||||
|
||||
GlobalActionManager* GlobalActionManager::s_instance = 0;
|
||||
|
||||
@@ -740,6 +741,13 @@ GlobalActionManager::acceptsMimeData( const QMimeData* data, bool tracksOnly )
|
||||
( tracksOnly ? data->data( "text/plain" ).contains( "track" ) : true ) )
|
||||
return true;
|
||||
|
||||
// We whitelist t.co and bit.ly (and j.mp) since they do some link checking. Often playable (e.g. spotify..) links hide behind them,
|
||||
// so we do an extra level of lookup
|
||||
if ( ( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "bit.ly" ) ) ||
|
||||
( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "j.mp" ) ) ||
|
||||
( data->hasFormat( "text/plain" ) && data->data( "text/plain" ).contains( "t.co" ) ) )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -755,7 +763,7 @@ GlobalActionManager::tracksFromMimeData( const QMimeData* data )
|
||||
{
|
||||
QString plainData = QString::fromUtf8( data->data( "text/plain" ).constData() );
|
||||
tDebug() << "Got text/plain mime data:" << data->data( "text/plain" ) << "decoded to:" << plainData;
|
||||
if( plainData.contains( "open.spotify.com/track") ||
|
||||
if ( plainData.contains( "open.spotify.com/track") ||
|
||||
plainData.contains( "spotify:track" ) )
|
||||
{
|
||||
QStringList tracks = plainData.split( "\n" );
|
||||
@@ -763,10 +771,34 @@ GlobalActionManager::tracksFromMimeData( const QMimeData* data )
|
||||
tDebug() << "Got a list of spotify urls!" << tracks;
|
||||
SpotifyParser* spot = new SpotifyParser( tracks, this );
|
||||
connect( spot, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ) );
|
||||
} else if ( plainData.contains( "bit.ly" ) ||
|
||||
plainData.contains( "j.mp" ) ||
|
||||
plainData.contains( "t.co" ) )
|
||||
{
|
||||
QStringList tracks = plainData.split( "\n" );
|
||||
|
||||
tDebug() << "Got a list of shortened urls!" << tracks;
|
||||
ShortenedLinkParser* parser = new ShortenedLinkParser( tracks, this );
|
||||
connect( parser, SIGNAL( urls( QStringList ) ), this, SLOT( expandedUrls( QStringList ) ) );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
GlobalActionManager::expandedUrls( QStringList urls )
|
||||
{
|
||||
QStringList spotifyUrls;
|
||||
foreach ( const QString& url, urls )
|
||||
{
|
||||
if( url.contains( "open.spotify.com/track") || url.contains( "spotify:track" ) )
|
||||
spotifyUrls << url;
|
||||
}
|
||||
|
||||
SpotifyParser* spot = new SpotifyParser( spotifyUrls, this );
|
||||
connect( spot, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ) );
|
||||
}
|
||||
|
||||
|
||||
QList< query_ptr >
|
||||
GlobalActionManager::tracksFromQueryList( const QMimeData* data )
|
||||
{
|
||||
|
@@ -76,6 +76,7 @@ private slots:
|
||||
void showPlaylist();
|
||||
|
||||
void xspfCreated( const QByteArray& xspf );
|
||||
void expandedUrls( QStringList );
|
||||
private:
|
||||
explicit GlobalActionManager( QObject* parent = 0 );
|
||||
void doBookmark( const Tomahawk::playlist_ptr& pl, const Tomahawk::query_ptr& q );
|
||||
|
89
src/libtomahawk/utils/shortenedlinkparser.cpp
Normal file
89
src/libtomahawk/utils/shortenedlinkparser.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
|
||||
*
|
||||
* 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 "shortenedlinkparser.h"
|
||||
|
||||
#include "utils/logger.h"
|
||||
#include "utils/tomahawkutils.h"
|
||||
#include "query.h"
|
||||
|
||||
#include <qjson/parser.h>
|
||||
|
||||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
|
||||
using namespace Tomahawk;
|
||||
|
||||
ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* parent )
|
||||
: QObject( parent )
|
||||
{
|
||||
foreach ( const QString& url, urls )
|
||||
lengthenUrl( url );
|
||||
}
|
||||
|
||||
ShortenedLinkParser::~ShortenedLinkParser() {}
|
||||
|
||||
void
|
||||
ShortenedLinkParser::lengthenUrl( const QString& url )
|
||||
{
|
||||
// Whitelisted links
|
||||
if ( !( url.contains( "t.co" ) ||
|
||||
url.contains( "bit.ly" ) ||
|
||||
url.contains( "j.mp" ) ) )
|
||||
return;
|
||||
|
||||
tDebug() << "Looking up..." << url;
|
||||
|
||||
QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) );
|
||||
connect( reply, SIGNAL( finished() ), this, SLOT( lookupFinished() ) );
|
||||
|
||||
m_queries.insert( reply );
|
||||
}
|
||||
|
||||
void
|
||||
ShortenedLinkParser::lookupFinished()
|
||||
{
|
||||
QNetworkReply* r = qobject_cast< QNetworkReply* >( sender() );
|
||||
Q_ASSERT( r );
|
||||
|
||||
QVariant redir = r->attribute( QNetworkRequest::RedirectionTargetAttribute );
|
||||
if ( redir.isValid() && !redir.toUrl().isEmpty() )
|
||||
{
|
||||
tLog() << "Got a redirected url:" << redir.toUrl().toString();
|
||||
m_links << redir.toUrl().toString();
|
||||
}
|
||||
|
||||
r->deleteLater();
|
||||
|
||||
m_queries.remove( r );
|
||||
checkFinished();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
ShortenedLinkParser::checkFinished()
|
||||
{
|
||||
if ( m_queries.isEmpty() ) // we're done
|
||||
{
|
||||
qDebug() << "DONE and found redirected urls:" << m_links;
|
||||
emit urls( m_links );
|
||||
|
||||
deleteLater();
|
||||
}
|
||||
|
||||
}
|
61
src/libtomahawk/utils/shortenedlinkparser.h
Normal file
61
src/libtomahawk/utils/shortenedlinkparser.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
|
||||
*
|
||||
* 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 SNORTENED_LINK_PARSER_H
|
||||
#define SNORTENED_LINK_PARSER_H
|
||||
|
||||
#include "dllmacro.h"
|
||||
#include "typedefs.h"
|
||||
|
||||
#include <QObject>
|
||||
#include <QSet>
|
||||
#include <QtCore/QStringList>
|
||||
|
||||
class QNetworkReply;
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
/**
|
||||
* Small class to parse whitelisted shortened links into the redirected urls
|
||||
*
|
||||
* Connect to urls() to get the result
|
||||
*
|
||||
*/
|
||||
class DLLEXPORT ShortenedLinkParser : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit ShortenedLinkParser( const QStringList& urls, QObject* parent = 0 );
|
||||
virtual ~ShortenedLinkParser();
|
||||
|
||||
signals:
|
||||
void urls( const QStringList& urls );
|
||||
|
||||
private:
|
||||
void lengthenUrl( const QString& url );
|
||||
void checkFinished();
|
||||
|
||||
QStringList m_links;
|
||||
QSet< QNetworkReply* > m_queries;
|
||||
public slots:
|
||||
void lookupFinished();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user