PageRenderTime 93ms CodeModel.GetById 40ms app.highlight 11ms RepoModel.GetById 40ms app.codeStats 0ms

/src/libtomahawk/utils/ShortenedLinkParser.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 130 lines | 82 code | 27 blank | 21 comment | 6 complexity | 43999dd9e89c00b2b8879b2ac178735c MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
  4 *   Copyright 2010-2015, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "ShortenedLinkParser.h"
 21
 22#include <QtNetwork/QNetworkAccessManager>
 23
 24#include "DropJobNotifier.h"
 25#include "Query.h"
 26#include "Source.h"
 27#include "jobview/ErrorStatusMessage.h"
 28#include "jobview/JobStatusModel.h"
 29#include "jobview/JobStatusView.h"
 30#include "utils/NetworkReply.h"
 31#include "utils/TomahawkUtilsGui.h"
 32#include "utils/Logger.h"
 33#include "utils/NetworkAccessManager.h"
 34
 35using namespace Tomahawk;
 36
 37
 38ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* parent )
 39    : QObject( parent )
 40{
 41    foreach ( const QString& url, urls )
 42        if ( handlesUrl( url ) )
 43            lookupUrl( url ) ;
 44}
 45
 46
 47ShortenedLinkParser::~ShortenedLinkParser()
 48{
 49}
 50
 51
 52bool
 53ShortenedLinkParser::handlesUrl( const QString& url )
 54{
 55    // Whitelisted links
 56    return ( url.contains( "t.co" ) ||
 57             url.contains( "bit.ly" ) ||
 58             url.contains( "j.mp" ) ||
 59             url.contains( "spoti.fi" ) ||
 60             url.contains( "ow.ly" ) ||
 61             url.contains( "fb.me" ) ||
 62             url.contains( "itun.es" ) ||
 63             url.contains( "tinyurl.com" ) ||
 64             url.contains( "tinysong.com" ) ||
 65             url.contains( "grooveshark.com/s/~/" ) || // These redirect to the 'real' grooveshark track url
 66             url.contains( "grooveshark.com/#/s/~/" ) ||
 67             url.contains( "rd.io" ) ||
 68             url.contains( "snd.sc" ) );
 69}
 70
 71
 72void
 73ShortenedLinkParser::lookupUrl( const QString& url )
 74{
 75    tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Looking up..." << url;
 76    QString cleaned = url;
 77    if ( cleaned.contains( "/#/s/" ) )
 78        cleaned.replace( "/#", "" );
 79
 80    NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( cleaned ) ) ) );
 81
 82    // Deezer is doing a nasty redirect to /comingsoon in some countries.
 83    // This removes valubale information from the URL.
 84    reply->blacklistHostFromRedirection( "www.deezer.com" );
 85    reply->blacklistHostFromRedirection( "deezer.com" );
 86
 87    connect( reply, SIGNAL( finished( QUrl ) ), SLOT( lookupFinished( QUrl ) ) );
 88
 89    m_queries.insert( reply );
 90
 91    m_expandJob = new DropJobNotifier( pixmap(), "shortened", DropJob::Track, reply );
 92    JobStatusView::instance()->model()->addJob( m_expandJob );
 93}
 94
 95
 96void
 97ShortenedLinkParser::lookupFinished( const QUrl& url )
 98{
 99    NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
100    Q_ASSERT( r );
101    r->deleteLater();
102
103    if ( r->reply()->error() != QNetworkReply::NoError )
104        JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Network error parsing shortened link!" ) ) );
105
106    tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Got an un-shortened url:" << r->reply()->url().toString();
107    m_links << url.toString();
108    m_queries.remove( r );
109
110    checkFinished();
111}
112
113
114void
115ShortenedLinkParser::checkFinished()
116{
117    if ( m_queries.isEmpty() ) // we're done
118    {
119        emit urls( m_links );
120
121        deleteLater();
122    }
123}
124
125
126QPixmap
127ShortenedLinkParser::pixmap()
128{
129    return TomahawkUtils::defaultPixmap( TomahawkUtils::Add );
130}