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

« back to all changes in this revision

Viewing changes to src/libtomahawk/utils/ExfmParser.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 "ExfmParser.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/TomahawkUtils.h"
 
34
#include "utils/Logger.h"
 
35
 
 
36
using namespace Tomahawk;
 
37
 
 
38
QPixmap* ExfmParser::s_pixmap = 0;
 
39
 
 
40
 
 
41
ExfmParser::ExfmParser( const QStringList& Urls, bool createNewPlaylist, QObject* parent )
 
42
    : QObject ( parent )
 
43
    , m_single( false )
 
44
    , m_trackMode( true )
 
45
    , m_createNewPlaylist( createNewPlaylist )
 
46
    , m_browseJob( 0 )
 
47
    , m_type( DropJob::All )
 
48
 
 
49
{
 
50
    foreach ( const QString& url, Urls )
 
51
        lookupUrl( url );
 
52
}
 
53
 
 
54
 
 
55
ExfmParser::ExfmParser( const QString& Url, bool createNewPlaylist, QObject* parent )
 
56
    : QObject ( parent )
 
57
    , m_single( true )
 
58
    , m_trackMode( true )
 
59
    , m_createNewPlaylist( createNewPlaylist )
 
60
    , m_browseJob( 0 )
 
61
    , m_type( DropJob::All )
 
62
{
 
63
    lookupUrl( Url );
 
64
}
 
65
 
 
66
 
 
67
ExfmParser::~ExfmParser()
 
68
{
 
69
}
 
70
 
 
71
 
 
72
void
 
73
ExfmParser::lookupUrl( const QString& link )
 
74
{
 
75
    const QString apiBase = "http://ex.fm/api/v3";
 
76
    QString url( link );
 
77
    QStringList paths;
 
78
    foreach( const QString& path, QUrl( link ).path().split( "/" ) )
 
79
    {
 
80
        if ( !path.isEmpty() )
 
81
            paths << path;
 
82
    }
 
83
 
 
84
    // User request
 
85
    if ( paths.count() == 1 )
 
86
    {
 
87
        m_type = DropJob::Artist;
 
88
        url = QString( apiBase + "/user/%1/trending" ).arg( paths.takeFirst() );
 
89
    }
 
90
    else
 
91
    {
 
92
        if ( url.contains( "/explore/site-of-the-day" ) ) // We treat site as artist
 
93
        {
 
94
            m_type = DropJob::Artist;
 
95
            url.replace( "/explore/site-of-the-day", "/sotd?results=1" );
 
96
        }
 
97
        else if ( url.contains( "/song/" ) )
 
98
        {
 
99
            m_type = DropJob::Track;
 
100
        }
 
101
        else if( ( url.contains( "site") && url.contains( "album" ) ) || url.contains( "mixtape-of-the-month" ) )
 
102
        {
 
103
            m_type = DropJob::Album;
 
104
            if ( url.contains( "album-of-the-week") )
 
105
                url = QString( apiBase + "/%1?%2" ).arg( "aotw" ).arg( "results=1" ); // Only need the first album, eg. this week
 
106
            if ( url.contains( "mixtape-of-the-month" ) )
 
107
                url = QString( apiBase + "/%1?%2" ).arg( "motm" ).arg( "results=1" ); // Only need the first mixtape, eg. this week
 
108
        }
 
109
        else
 
110
        {
 
111
            m_type = DropJob::Playlist;
 
112
            if ( url.contains( "tastemakers" ) )
 
113
                url.replace( "trending", "explore" );
 
114
        }
 
115
        if ( m_type == DropJob::Playlist )
 
116
        {
 
117
            url.replace( "/genre/", "/tag/" );
 
118
            url.replace( "/search/", "/song/search/" ); // We can only search for tracks, even though we want an artist or whatever
 
119
        }
 
120
 
 
121
        url.replace( "http://ex.fm", apiBase );
 
122
    }
 
123
 
 
124
    tDebug() << "Looking up URL..." << url;
 
125
    NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ) );
 
126
 
 
127
    if ( m_createNewPlaylist )
 
128
        connect( reply, SIGNAL( finished() ), SLOT( exfmLookupFinished() ) );
 
129
    else
 
130
        connect( reply, SIGNAL( finished() ), SLOT( exfmBrowseFinished() ) );
 
131
 
 
132
    m_browseJob = new DropJobNotifier( pixmap(), "Exfm", m_type, reply );
 
133
    JobStatusView::instance()->model()->addJob( m_browseJob );
 
134
 
 
135
    m_queries.insert( reply );
 
136
}
 
137
 
 
138
 
 
139
void
 
140
ExfmParser::parseTrack( const QVariantMap& res )
 
141
{
 
142
    QString title, artist, album;
 
143
    album = res.value( "album", QString() ).toString();
 
144
    title = res.value( "title", QString() ).toString();
 
145
    artist = res.value( "artist", QString() ).toString();
 
146
 
 
147
    if ( title.isEmpty() && artist.isEmpty() ) // don't have enough...
 
148
    {
 
149
        tLog() << "Didn't get an artist and track name from Exfm, not enough to build a query on. Aborting" << title << artist;
 
150
        return;
 
151
    }
 
152
 
 
153
    Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, album, uuid(), m_trackMode );
 
154
 
 
155
    if ( !q.isNull() )
 
156
    {
 
157
        tLog() << "Setting resulthint to " << res.value( "url" );
 
158
        q->setResultHint( res.value( "url" ).toString() );
 
159
        q->setProperty( "annotation", res.value( "url" ).toString() );
 
160
        m_tracks << q;
 
161
    }
 
162
}
 
163
 
 
164
 
 
165
void
 
166
ExfmParser::exfmLookupFinished()
 
167
{
 
168
    NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
 
169
    Q_ASSERT( r );
 
170
    m_queries.remove( r );
 
171
    r->deleteLater();
 
172
 
 
173
    if ( r->reply()->error() == QNetworkReply::NoError )
 
174
    {
 
175
        QJson::Parser p;
 
176
        bool ok;
 
177
        QVariantMap res = p.parse( r->reply(), &ok ).toMap();
 
178
 
 
179
        if ( !ok )
 
180
        {
 
181
            tLog() << "Failed to parse json from Exfm browse item:" << p.errorString() << "On line" << p.errorLine();
 
182
            return;
 
183
        }
 
184
 
 
185
        QStringList paths;
 
186
        foreach ( const QString& path, r->reply()->url().path().split( "/" ) )
 
187
        {
 
188
            if ( !path.isEmpty() )
 
189
                paths << path;
 
190
        }
 
191
 
 
192
        QString title, artist, desc;
 
193
        QVariantList tracks;
 
194
 
 
195
        if ( m_type == DropJob::Album )
 
196
        {
 
197
            QStringList meta = res.value( "site" ).toMap().value( "title" ).toString().split( ", by " );
 
198
            title = meta.takeFirst();
 
199
            artist = meta.takeLast();
 
200
            tracks = res.value( "site" ).toMap().value( "songs" ).toList();
 
201
        }
 
202
        else
 
203
        {
 
204
            // Take endpoint as title
 
205
            title = paths.takeLast();
 
206
            title[0] = title[0].toUpper();
 
207
 
 
208
            if ( paths.contains( "trending") )
 
209
                title = "Trending " + title;
 
210
            else if ( paths.contains( "explore" ) )
 
211
                title = "Explore " + title;
 
212
 
 
213
            if ( paths.contains( "user" ) )
 
214
            {
 
215
                int index = paths.indexOf( "user");
 
216
                if ( index != -1 && paths.size() > index+1 )
 
217
                    artist = paths.takeAt(paths.indexOf( "user" ) +1 );
 
218
            }else
 
219
                artist = "Ex.fm";
 
220
 
 
221
            tracks = res.value( "songs" ).toList();
 
222
        }
 
223
 
 
224
        // Make the title
 
225
        title = title + " by " + artist;
 
226
 
 
227
        foreach( const QVariant& track, tracks )
 
228
            parseTrack( track.toMap() );
 
229
 
 
230
        m_playlist = Playlist::create( SourceList::instance()->getLocal(),
 
231
                                       uuid(),
 
232
                                       title,
 
233
                                       desc,
 
234
                                       artist,
 
235
                                       false,
 
236
                                       m_tracks );
 
237
 
 
238
        connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) );
 
239
        return;
 
240
    }
 
241
}
 
242
 
 
243
 
 
244
void
 
245
ExfmParser::playlistCreated()
 
246
{
 
247
    ViewManager::instance()->show( m_playlist );
 
248
 
 
249
    deleteLater();
 
250
}
 
251
 
 
252
 
 
253
void
 
254
ExfmParser::exfmBrowseFinished()
 
255
{
 
256
    NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
 
257
    Q_ASSERT( r );
 
258
 
 
259
    r->deleteLater();
 
260
 
 
261
    if ( r->reply()->error() == QNetworkReply::NoError )
 
262
    {
 
263
        QJson::Parser p;
 
264
        bool ok;
 
265
        QVariantMap res = p.parse( r->reply(), &ok ).toMap();
 
266
 
 
267
        if ( !ok )
 
268
        {
 
269
            qDebug() << "Failed to parse ex.fm json";
 
270
            return;
 
271
        }
 
272
 
 
273
        if ( m_type != DropJob::Track )
 
274
        {
 
275
            QVariantList tracks;
 
276
            if( m_type == DropJob::Album )
 
277
                tracks = res.value( "site" ).toMap().value( "songs" ).toList();
 
278
            else
 
279
                tracks = res.value( "songs" ).toList();
 
280
 
 
281
            foreach( const QVariant& track, tracks )
 
282
                parseTrack( track.toMap() );
 
283
        }
 
284
        else
 
285
            parseTrack( res.value( "song" ).toMap() );
 
286
 
 
287
        if ( m_single && !m_tracks.isEmpty() )
 
288
            emit track( m_tracks.first() );
 
289
        else if ( !m_single && !m_tracks.isEmpty() )
 
290
            emit tracks( m_tracks );
 
291
 
 
292
        deleteLater();
 
293
    }
 
294
}
 
295
 
 
296
 
 
297
QPixmap
 
298
ExfmParser::pixmap() const
 
299
{
 
300
    if ( !s_pixmap )
 
301
        s_pixmap = new QPixmap( RESPATH "images/exfm.png" );
 
302
 
 
303
    return *s_pixmap;
 
304
}