/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. #include "ShortenedLinkParser.h"
  20. #include <QtNetwork/QNetworkAccessManager>
  21. #include "DropJobNotifier.h"
  22. #include "Query.h"
  23. #include "Source.h"
  24. #include "jobview/ErrorStatusMessage.h"
  25. #include "jobview/JobStatusModel.h"
  26. #include "jobview/JobStatusView.h"
  27. #include "utils/NetworkReply.h"
  28. #include "utils/TomahawkUtilsGui.h"
  29. #include "utils/Logger.h"
  30. #include "utils/NetworkAccessManager.h"
  31. using namespace Tomahawk;
  32. ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* parent )
  33. : QObject( parent )
  34. {
  35. foreach ( const QString& url, urls )
  36. if ( handlesUrl( url ) )
  37. lookupUrl( url ) ;
  38. }
  39. ShortenedLinkParser::~ShortenedLinkParser()
  40. {
  41. }
  42. bool
  43. ShortenedLinkParser::handlesUrl( const QString& url )
  44. {
  45. // Whitelisted links
  46. return ( url.contains( "t.co" ) ||
  47. url.contains( "bit.ly" ) ||
  48. url.contains( "j.mp" ) ||
  49. url.contains( "spoti.fi" ) ||
  50. url.contains( "ow.ly" ) ||
  51. url.contains( "fb.me" ) ||
  52. url.contains( "itun.es" ) ||
  53. url.contains( "tinyurl.com" ) ||
  54. url.contains( "tinysong.com" ) ||
  55. url.contains( "grooveshark.com/s/~/" ) || // These redirect to the 'real' grooveshark track url
  56. url.contains( "grooveshark.com/#/s/~/" ) ||
  57. url.contains( "rd.io" ) ||
  58. url.contains( "snd.sc" ) );
  59. }
  60. void
  61. ShortenedLinkParser::lookupUrl( const QString& url )
  62. {
  63. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Looking up..." << url;
  64. QString cleaned = url;
  65. if ( cleaned.contains( "/#/s/" ) )
  66. cleaned.replace( "/#", "" );
  67. NetworkReply* reply = new NetworkReply( Tomahawk::Utils::nam()->get( QNetworkRequest( QUrl( cleaned ) ) ) );
  68. // Deezer is doing a nasty redirect to /comingsoon in some countries.
  69. // This removes valubale information from the URL.
  70. reply->blacklistHostFromRedirection( "www.deezer.com" );
  71. reply->blacklistHostFromRedirection( "deezer.com" );
  72. connect( reply, SIGNAL( finished( QUrl ) ), SLOT( lookupFinished( QUrl ) ) );
  73. m_queries.insert( reply );
  74. m_expandJob = new DropJobNotifier( pixmap(), "shortened", DropJob::Track, reply );
  75. JobStatusView::instance()->model()->addJob( m_expandJob );
  76. }
  77. void
  78. ShortenedLinkParser::lookupFinished( const QUrl& url )
  79. {
  80. NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
  81. Q_ASSERT( r );
  82. r->deleteLater();
  83. if ( r->reply()->error() != QNetworkReply::NoError )
  84. JobStatusView::instance()->model()->addJob( new ErrorStatusMessage( tr( "Network error parsing shortened link!" ) ) );
  85. tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Got an un-shortened url:" << r->reply()->url().toString();
  86. m_links << url.toString();
  87. m_queries.remove( r );
  88. checkFinished();
  89. }
  90. void
  91. ShortenedLinkParser::checkFinished()
  92. {
  93. if ( m_queries.isEmpty() ) // we're done
  94. {
  95. emit urls( m_links );
  96. deleteLater();
  97. }
  98. }
  99. QPixmap
  100. ShortenedLinkParser::pixmap()
  101. {
  102. return TomahawkUtils::defaultPixmap( TomahawkUtils::Add );
  103. }