diff --git a/src/libtomahawk/utils/groovesharkparser.cpp b/src/libtomahawk/utils/groovesharkparser.cpp index 59c898822..dcc28e620 100644 --- a/src/libtomahawk/utils/groovesharkparser.cpp +++ b/src/libtomahawk/utils/groovesharkparser.cpp @@ -2,7 +2,7 @@ * * Copyright 2010-2011, Leo Franchi * Copyright 2010-2011, Hugo Lindström - * Copyright 2010-2011, Stefan Derkits + * Copyright 2010-2012, Stefan Derkits * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -91,42 +91,56 @@ GroovesharkParser::lookupGroovesharkPlaylist( const QString& linkRaw ) { tLog() << "Parsing Grooveshark Playlist URI:" << linkRaw; - QStringList urlParts = linkRaw.split( "/" ); + QString urlFragment = QUrl( linkRaw ).fragment( ); + if ( urlFragment.isEmpty() ) { + tDebug() << "no fragment, setting fragment to path"; + urlFragment = QUrl(linkRaw).path(); + } + + tDebug() << urlFragment; + + int paramStartingPostition = urlFragment.indexOf( "?" ); + + if ( paramStartingPostition != -1 ) + urlFragment.truncate( paramStartingPostition ); + bool ok; - QString playlistStr = urlParts.last(); - playlistStr.truncate(playlistStr.indexOf("?")); - int playlistID = playlistStr.toInt( &ok, 10 ); + + QStringList urlParts = urlFragment.split( "/", QString::SkipEmptyParts ); + + tDebug() << urlParts; + + int playlistID = urlParts.at( 2 ).toInt( &ok, 10 ); if (!ok) { tDebug() << "incorrect grooveshark url"; return; } - m_title = urlParts.at( urlParts.size()-2 ); + + + m_title = urlParts.at( 1 ); tDebug() << "should get playlist " << playlistID; DropJob::DropType type; - if ( linkRaw.contains( "playlist" ) ) - type = DropJob::Playlist; + type = DropJob::Playlist; QString base_url( "http://api.grooveshark.com/ws3.php?sig=" ); QByteArray data = QString( "{\"method\":\"getPlaylistSongs\",\"parameters\":{\"playlistID\":\"%1\"},\"header\":{\"wsKey\":\"tomahawkplayer\"}}" ).arg( playlistID ).toLocal8Bit(); - - - - + + QCA::MessageAuthenticationCode hmac( "hmac(md5)", m_apiKey ); QCA::SecureArray secdata( data ); hmac.update(secdata); QCA::SecureArray resultArray = hmac.final(); - + QString hash = QCA::arrayToHex( resultArray.toByteArray() ); QUrl url = QUrl( base_url + hash ); - + tDebug() << "Looking up URL..." << url.toString(); QNetworkReply* reply = TomahawkUtils::nam()->post( QNetworkRequest( url ), data ); diff --git a/src/libtomahawk/utils/shortenedlinkparser.cpp b/src/libtomahawk/utils/shortenedlinkparser.cpp index 6c37f951e..6d89aba69 100644 --- a/src/libtomahawk/utils/shortenedlinkparser.cpp +++ b/src/libtomahawk/utils/shortenedlinkparser.cpp @@ -35,7 +35,8 @@ ShortenedLinkParser::ShortenedLinkParser ( const QStringList& urls, QObject* par : QObject( parent ) { foreach ( const QString& url, urls ) - lengthenUrl( url ); + if ( handlesUrl( url ) ) + lookupUrl( url ) ; } @@ -60,13 +61,9 @@ ShortenedLinkParser::handlesUrl( const QString& url ) url.contains( "rd.io" ) ); } - void -ShortenedLinkParser::lengthenUrl( const QString& url ) +ShortenedLinkParser::lookupUrl ( const QString& url ) { - if ( !handlesUrl( url ) ) - return; - tDebug() << "Looking up..." << url; QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ); @@ -75,7 +72,6 @@ ShortenedLinkParser::lengthenUrl( const QString& url ) m_queries.insert( reply ); } - void ShortenedLinkParser::lookupFinished() { @@ -85,14 +81,19 @@ ShortenedLinkParser::lookupFinished() 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(); + tDebug() << "RedirectionTargetAttribute set on " << redir; + m_queries.remove( r ); + r->deleteLater(); + lookupUrl( redir.toUrl().toString() ); + } + else + { + tLog() << "Got a redirected url:" << r->url().toString(); + m_links << r->url().toString(); + m_queries.remove( r ); + r->deleteLater(); + checkFinished(); } - - r->deleteLater(); - - m_queries.remove( r ); - checkFinished(); } diff --git a/src/libtomahawk/utils/shortenedlinkparser.h b/src/libtomahawk/utils/shortenedlinkparser.h index 51c208725..4fd358749 100644 --- a/src/libtomahawk/utils/shortenedlinkparser.h +++ b/src/libtomahawk/utils/shortenedlinkparser.h @@ -55,7 +55,7 @@ signals: void urls( const QStringList& urls ); private: - void lengthenUrl( const QString& url ); + void lookupUrl( const QString& url ); void checkFinished(); QStringList m_links;