~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/utils/SoundcloudParser.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2012, Hugo Lindstrƶm <hugolm84@gmail.com>
 
4
 *
 
5
 *   Tomahawk is free software: you can redistribute it and/or modify
 
6
 *   it under the terms of the GNU General Public License as published by
 
7
 *   the Free Software Foundation, either version 3 of the License, or
 
8
 *   (at your option) any later version.
 
9
 *
 
10
 *   Tomahawk is distributed in the hope that it will be useful,
 
11
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 
13
 *   GNU General Public License for more details.
 
14
 *
 
15
 *   You should have received a copy of the GNU General Public License
 
16
 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
 
 
19
#include "SoundcloudParser.h"
 
20
 
 
21
#include <QtNetwork/QNetworkAccessManager>
 
22
 
 
23
#include <qjson/parser.h>
 
24
 
 
25
#include "Query.h"
 
26
#include "SourceList.h"
 
27
#include "DropJobNotifier.h"
 
28
#include "ViewManager.h"
 
29
#include "jobview/JobStatusView.h"
 
30
#include "jobview/JobStatusModel.h"
 
31
#include "jobview/ErrorStatusMessage.h"
 
32
#include "utils/NetworkReply.h"
 
33
#include "utils/TomahawkUtilsGui.h"
 
34
#include "utils/Logger.h"
 
35
 
 
36
using namespace Tomahawk;
 
37
 
 
38
 
 
39
SoundcloudParser::SoundcloudParser( const QStringList& Urls, bool createNewPlaylist, QObject* parent )
 
40
    : QObject ( parent )
 
41
    , m_single( false )
 
42
    , m_trackMode( true )
 
43
    , m_createNewPlaylist( createNewPlaylist )
 
44
    , m_browseJob( 0 )
 
45
    , m_type( DropJob::All )
 
46
    , m_getLikes( false )
 
47
 
 
48
{
 
49
    foreach ( const QString& url, Urls )
 
50
        lookupUrl( url );
 
51
}
 
52
 
 
53
 
 
54
SoundcloudParser::SoundcloudParser( const QString& Url, bool createNewPlaylist, QObject* parent )
 
55
    : QObject ( parent )
 
56
    , m_single( true )
 
57
    , m_trackMode( true )
 
58
    , m_createNewPlaylist( createNewPlaylist )
 
59
    , m_browseJob( 0 )
 
60
    , m_type( DropJob::All )
 
61
    , m_getLikes( false )
 
62
{
 
63
    lookupUrl( Url );
 
64
}
 
65
 
 
66
 
 
67
SoundcloudParser::~SoundcloudParser()
 
68
{
 
69
}
 
70
 
 
71
 
 
72
void
 
73
SoundcloudParser::lookupUrl( const QString& link )
 
74
{
 
75
    tDebug() << "Looking up URL..." << link;
 
76
    QString url = link;
 
77
    if ( link.contains( "/likes" ) )
 
78
    {
 
79
        qDebug() << Q_FUNC_INFO << "Requesting likes";
 
80
        url.replace( "/likes", "" );
 
81
        m_getLikes = true;
 
82
    }
 
83
    QUrl scLink( QString( "http://api.soundcloud.com/resolve.json?client_id=TiNg2DRYhBnp01DA3zNag&url=" ) + url );
 
84
 
 
85
    NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( scLink ) ) );
 
86
    connect( reply, SIGNAL( finished() ), SLOT( soundcloudLookupFinished() ) );
 
87
 
 
88
    m_browseJob = new DropJobNotifier( pixmap(), "Soundcloud", DropJob::All, reply );
 
89
    JobStatusView::instance()->model()->addJob( m_browseJob );
 
90
 
 
91
    m_queries.insert( reply );
 
92
}
 
93
 
 
94
 
 
95
void
 
96
SoundcloudParser::parseTrack( const QVariantMap& res )
 
97
{
 
98
    QString title, artist;
 
99
    title = res.value( "title", QString() ).toString();
 
100
    artist = res.value( "user" ).toMap().value( "username", QString() ).toString();
 
101
    bool streamable = res.value( "streamable" ).toBool();
 
102
 
 
103
    if ( title.isEmpty() && artist.isEmpty() ) // don't have enough...
 
104
    {
 
105
        tLog() << "Didn't get an artist and track name from Soundcloud, not enough to build a query on. Aborting" << title << artist;
 
106
        return;
 
107
    }
 
108
 
 
109
    if ( !streamable )
 
110
    {
 
111
        JobStatusView::instance()->model()->addJob(
 
112
            new ErrorStatusMessage( tr( "Track '%1' by %2 is not streamable." ).arg( title ).arg( artist ), 5 ) );
 
113
        tLog() << "Track is not streamble, aborting." << res.value( "uri" ).toString();
 
114
        return;
 
115
    }
 
116
 
 
117
    Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, QString(), uuid(), m_trackMode );
 
118
 
 
119
    if ( !q.isNull() )
 
120
    {
 
121
        QUrl url = QUrl::fromUserInput( res.value( "stream_url" ).toString() );
 
122
        url.addQueryItem( "client_id", "TiNg2DRYhBnp01DA3zNag" );
 
123
        tLog() << "Setting resulthint to " << res.value( "stream_url" ) << url.toString();
 
124
        q->setResultHint( url.toString() );
 
125
        q->setSaveHTTPResultHint( true );
 
126
        m_tracks << q;
 
127
    }
 
128
}
 
129
 
 
130
 
 
131
void
 
132
SoundcloudParser::soundcloudLookupFinished()
 
133
{
 
134
    NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
 
135
    Q_ASSERT( r );
 
136
    m_queries.remove( r );
 
137
    r->deleteLater();
 
138
 
 
139
    if ( r->reply()->error() == QNetworkReply::NoError )
 
140
    {
 
141
        QJson::Parser p;
 
142
        bool ok;
 
143
        QVariantMap res = p.parse( r->reply(), &ok ).toMap();
 
144
        if ( !ok )
 
145
        {
 
146
            tLog() << "Failed to parse json from Soundcloud browse item:" << p.errorString() << "On line" << p.errorLine();
 
147
            return;
 
148
        }
 
149
 
 
150
        if( res.value( "kind" ).toString() == "playlist" )
 
151
            m_type = DropJob::Playlist;
 
152
        if( res.value( "kind" ).toString() == "track" )
 
153
            m_type = DropJob::Track;
 
154
        if( res.value( "kind" ).toString() == "user" )
 
155
        {
 
156
            QUrl url;
 
157
            if ( m_getLikes )
 
158
            {
 
159
                url = QUrl( QString( res.value( "uri" ).toString() + "/favorites.json?client_id=TiNg2DRYhBnp01DA3zNag" ) );
 
160
            }
 
161
            else
 
162
            {
 
163
                url = QUrl( QString( res.value( "uri" ).toString() + "/tracks.json?client_id=TiNg2DRYhBnp01DA3zNag" ) );
 
164
            }
 
165
 
 
166
            NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ) );
 
167
 
 
168
            if ( m_createNewPlaylist )
 
169
                m_userData = res;
 
170
 
 
171
            connect( reply, SIGNAL( finished() ), SLOT( soundcloudArtistLookupFinished() ) );
 
172
            return;
 
173
        }
 
174
 
 
175
        if ( m_type == DropJob::Track )
 
176
        {
 
177
            parseTrack( res );
 
178
        }
 
179
        else if ( m_type == DropJob::Playlist )
 
180
        {
 
181
            QString title, user, desc;
 
182
            title = res.value( "title" ).toString();
 
183
            desc = res.value( "description" ).toString();
 
184
            user = res.value( "user" ).toMap().value( "username" ).toString();
 
185
 
 
186
            QVariantList tracks = res.value( "tracks" ).toList();
 
187
 
 
188
            foreach( const QVariant& track, tracks )
 
189
                parseTrack( track.toMap() );
 
190
 
 
191
            if ( m_createNewPlaylist )
 
192
            {
 
193
                m_playlist = Playlist::create( SourceList::instance()->getLocal(),
 
194
                                           uuid(),
 
195
                                           title,
 
196
                                           desc,
 
197
                                           user,
 
198
                                           false,
 
199
                                           m_tracks );
 
200
 
 
201
                connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) );
 
202
                return;
 
203
            }
 
204
        }
 
205
    }
 
206
 
 
207
    if ( m_single && !m_tracks.isEmpty() )
 
208
        emit track( m_tracks.first() );
 
209
    else if ( !m_single && !m_tracks.isEmpty() )
 
210
        emit tracks( m_tracks );
 
211
 
 
212
    deleteLater();
 
213
}
 
214
 
 
215
 
 
216
void
 
217
SoundcloudParser::playlistCreated()
 
218
{
 
219
    ViewManager::instance()->show( m_playlist );
 
220
 
 
221
    deleteLater();
 
222
}
 
223
 
 
224
 
 
225
void
 
226
SoundcloudParser::soundcloudArtistLookupFinished()
 
227
{
 
228
    NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
 
229
    Q_ASSERT( r );
 
230
 
 
231
    r->deleteLater();
 
232
 
 
233
    if ( r->reply()->error() == QNetworkReply::NoError )
 
234
    {
 
235
        QJson::Parser p;
 
236
        bool ok;
 
237
        QVariantList res = p.parse( r->reply(), &ok ).toList();
 
238
 
 
239
        foreach( const QVariant& track, res )
 
240
            parseTrack( track.toMap() );
 
241
 
 
242
        if ( m_createNewPlaylist )
 
243
        {
 
244
            const QString user = m_userData.value( "full_name" ).toString();
 
245
            const QString title = user + "'s " + ( m_getLikes ? "Favorites" : "Tracks" );
 
246
            m_playlist = Playlist::create( SourceList::instance()->getLocal(),
 
247
                                       uuid(),
 
248
                                       title,
 
249
                                       "",
 
250
                                       user,
 
251
                                       false,
 
252
                                       m_tracks );
 
253
 
 
254
            connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) );
 
255
            return;
 
256
 
 
257
        }
 
258
        if ( m_single && !m_tracks.isEmpty() )
 
259
            emit track( m_tracks.first() );
 
260
        else if ( !m_single && !m_tracks.isEmpty() )
 
261
            emit tracks( m_tracks );
 
262
 
 
263
        deleteLater();
 
264
 
 
265
    }
 
266
}
 
267
 
 
268
 
 
269
QPixmap
 
270
SoundcloudParser::pixmap() const
 
271
{
 
272
    return TomahawkUtils::defaultPixmap( TomahawkUtils::SoundcloudIcon );
 
273
}