From 47956a1bdef2818879569e968cc6d9eb73e563ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20Lindstr=C3=B6m?= Date: Fri, 21 Dec 2012 01:32:51 +0100 Subject: [PATCH] Support SC user favorites --- src/libtomahawk/utils/SoundcloudParser.cpp | 54 ++++++++++++++++++++-- src/libtomahawk/utils/SoundcloudParser.h | 2 + 2 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/libtomahawk/utils/SoundcloudParser.cpp b/src/libtomahawk/utils/SoundcloudParser.cpp index 5e10379f7..0b331f3a4 100644 --- a/src/libtomahawk/utils/SoundcloudParser.cpp +++ b/src/libtomahawk/utils/SoundcloudParser.cpp @@ -43,6 +43,7 @@ SoundcloudParser::SoundcloudParser( const QStringList& Urls, bool createNewPlayl , m_createNewPlaylist( createNewPlaylist ) , m_browseJob( 0 ) , m_type( DropJob::All ) + , m_getLikes( false ) { foreach ( const QString& url, Urls ) @@ -57,6 +58,7 @@ SoundcloudParser::SoundcloudParser( const QString& Url, bool createNewPlaylist, , m_createNewPlaylist( createNewPlaylist ) , m_browseJob( 0 ) , m_type( DropJob::All ) + , m_getLikes( false ) { lookupUrl( Url ); } @@ -71,8 +73,14 @@ void SoundcloudParser::lookupUrl( const QString& link ) { tDebug() << "Looking up URL..." << link; - - QUrl scLink( QString( "http://api.soundcloud.com/resolve.json?client_id=TiNg2DRYhBnp01DA3zNag&url=" ) + link ); + QString url = link; + if ( link.contains( "/likes" ) ) + { + qDebug() << Q_FUNC_INFO << "Requesting likes"; + url.replace( "/likes", "" ); + m_getLikes = true; + } + QUrl scLink( QString( "http://api.soundcloud.com/resolve.json?client_id=TiNg2DRYhBnp01DA3zNag&url=" ) + url ); NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( scLink ) ) ); connect( reply, SIGNAL( finished() ), SLOT( soundcloudLookupFinished() ) ); @@ -90,6 +98,7 @@ SoundcloudParser::parseTrack( const QVariantMap& res ) QString title, artist; title = res.value( "title", QString() ).toString(); artist = res.value( "user" ).toMap().value( "username", QString() ).toString(); + bool streamable = res.value( "streamable" ).toBool(); if ( title.isEmpty() && artist.isEmpty() ) // don't have enough... { @@ -97,6 +106,14 @@ SoundcloudParser::parseTrack( const QVariantMap& res ) return; } + if ( !streamable ) + { + JobStatusView::instance()->model()->addJob( + new ErrorStatusMessage( tr( "Track '%1' by %2 is not streamable." ).arg( title ).arg( artist ), 5 ) ); + tLog() << "Track is not streamble, aborting." << res.value( "uri" ).toString(); + return; + } + Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, QString(), uuid(), m_trackMode ); if ( !q.isNull() ) @@ -136,9 +153,21 @@ SoundcloudParser::soundcloudLookupFinished() m_type = DropJob::Track; if( res.value( "kind" ).toString() == "user" ) { - QUrl url = QUrl( QString( res.value( "uri" ).toString() + "/tracks.json?client_id=TiNg2DRYhBnp01DA3zNag" ) ); - qDebug() << Q_FUNC_INFO << url; + QUrl url; + if ( m_getLikes ) + { + url = QUrl( QString( res.value( "uri" ).toString() + "/favorites.json?client_id=TiNg2DRYhBnp01DA3zNag" ) ); + } + else + { + url = QUrl( QString( res.value( "uri" ).toString() + "/tracks.json?client_id=TiNg2DRYhBnp01DA3zNag" ) ); + } + NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ) ); + + if ( m_createNewPlaylist ) + m_userData = res; + connect( reply, SIGNAL( finished() ), SLOT( soundcloudArtistLookupFinished() ) ); return; } @@ -206,9 +235,26 @@ SoundcloudParser::soundcloudArtistLookupFinished() QJson::Parser p; bool ok; QVariantList res = p.parse( r->reply(), &ok ).toList(); + foreach( const QVariant& track, res ) parseTrack( track.toMap() ); + if ( m_createNewPlaylist ) + { + const QString user = m_userData.value( "full_name" ).toString(); + const QString title = user + "'s " + ( m_getLikes ? "Favorites" : "Tracks" ); + m_playlist = Playlist::create( SourceList::instance()->getLocal(), + uuid(), + title, + "", + user, + false, + m_tracks ); + + connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) ); + return; + + } if ( m_single && !m_tracks.isEmpty() ) emit track( m_tracks.first() ); else if ( !m_single && !m_tracks.isEmpty() ) diff --git a/src/libtomahawk/utils/SoundcloudParser.h b/src/libtomahawk/utils/SoundcloudParser.h index 37018b121..e003d086e 100644 --- a/src/libtomahawk/utils/SoundcloudParser.h +++ b/src/libtomahawk/utils/SoundcloudParser.h @@ -79,6 +79,8 @@ private: int m_subscribers; QList< query_ptr > m_tracks; + QVariantMap m_userData; + bool m_getLikes; QSet< NetworkReply* > m_queries; Tomahawk::playlist_ptr m_playlist; DropJobNotifier* m_browseJob;