1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2012, Hugo Lindstrƶm <hugolm84@gmail.com>
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.
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.
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/>.
19
#include "ExfmParser.h"
21
#include <QtNetwork/QNetworkAccessManager>
23
#include <qjson/parser.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"
36
using namespace Tomahawk;
38
QPixmap* ExfmParser::s_pixmap = 0;
41
ExfmParser::ExfmParser( const QStringList& Urls, bool createNewPlaylist, QObject* parent )
45
, m_createNewPlaylist( createNewPlaylist )
47
, m_type( DropJob::All )
50
foreach ( const QString& url, Urls )
55
ExfmParser::ExfmParser( const QString& Url, bool createNewPlaylist, QObject* parent )
59
, m_createNewPlaylist( createNewPlaylist )
61
, m_type( DropJob::All )
67
ExfmParser::~ExfmParser()
73
ExfmParser::lookupUrl( const QString& link )
75
const QString apiBase = "http://ex.fm/api/v3";
78
foreach( const QString& path, QUrl( link ).path().split( "/" ) )
80
if ( !path.isEmpty() )
85
if ( paths.count() == 1 )
87
m_type = DropJob::Artist;
88
url = QString( apiBase + "/user/%1/trending" ).arg( paths.takeFirst() );
92
if ( url.contains( "/explore/site-of-the-day" ) ) // We treat site as artist
94
m_type = DropJob::Artist;
95
url.replace( "/explore/site-of-the-day", "/sotd?results=1" );
97
else if ( url.contains( "/song/" ) )
99
m_type = DropJob::Track;
101
else if( ( url.contains( "site") && url.contains( "album" ) ) || url.contains( "mixtape-of-the-month" ) )
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
111
m_type = DropJob::Playlist;
112
if ( url.contains( "tastemakers" ) )
113
url.replace( "trending", "explore" );
115
if ( m_type == DropJob::Playlist )
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
121
url.replace( "http://ex.fm", apiBase );
124
tDebug() << "Looking up URL..." << url;
125
NetworkReply* reply = new NetworkReply( TomahawkUtils::nam()->get( QNetworkRequest( QUrl( url ) ) ) );
127
if ( m_createNewPlaylist )
128
connect( reply, SIGNAL( finished() ), SLOT( exfmLookupFinished() ) );
130
connect( reply, SIGNAL( finished() ), SLOT( exfmBrowseFinished() ) );
132
m_browseJob = new DropJobNotifier( pixmap(), "Exfm", m_type, reply );
133
JobStatusView::instance()->model()->addJob( m_browseJob );
135
m_queries.insert( reply );
140
ExfmParser::parseTrack( const QVariantMap& res )
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();
147
if ( title.isEmpty() && artist.isEmpty() ) // don't have enough...
149
tLog() << "Didn't get an artist and track name from Exfm, not enough to build a query on. Aborting" << title << artist;
153
Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, album, uuid(), m_trackMode );
157
tLog() << "Setting resulthint to " << res.value( "url" );
158
q->setResultHint( res.value( "url" ).toString() );
159
q->setProperty( "annotation", res.value( "url" ).toString() );
166
ExfmParser::exfmLookupFinished()
168
NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
170
m_queries.remove( r );
173
if ( r->reply()->error() == QNetworkReply::NoError )
177
QVariantMap res = p.parse( r->reply(), &ok ).toMap();
181
tLog() << "Failed to parse json from Exfm browse item:" << p.errorString() << "On line" << p.errorLine();
186
foreach ( const QString& path, r->reply()->url().path().split( "/" ) )
188
if ( !path.isEmpty() )
192
QString title, artist, desc;
195
if ( m_type == DropJob::Album )
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();
204
// Take endpoint as title
205
title = paths.takeLast();
206
title[0] = title[0].toUpper();
208
if ( paths.contains( "trending") )
209
title = "Trending " + title;
210
else if ( paths.contains( "explore" ) )
211
title = "Explore " + title;
213
if ( paths.contains( "user" ) )
215
int index = paths.indexOf( "user");
216
if ( index != -1 && paths.size() > index+1 )
217
artist = paths.takeAt(paths.indexOf( "user" ) +1 );
221
tracks = res.value( "songs" ).toList();
225
title = title + " by " + artist;
227
foreach( const QVariant& track, tracks )
228
parseTrack( track.toMap() );
230
m_playlist = Playlist::create( SourceList::instance()->getLocal(),
238
connect( m_playlist.data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistCreated() ) );
245
ExfmParser::playlistCreated()
247
ViewManager::instance()->show( m_playlist );
254
ExfmParser::exfmBrowseFinished()
256
NetworkReply* r = qobject_cast< NetworkReply* >( sender() );
261
if ( r->reply()->error() == QNetworkReply::NoError )
265
QVariantMap res = p.parse( r->reply(), &ok ).toMap();
269
qDebug() << "Failed to parse ex.fm json";
273
if ( m_type != DropJob::Track )
276
if( m_type == DropJob::Album )
277
tracks = res.value( "site" ).toMap().value( "songs" ).toList();
279
tracks = res.value( "songs" ).toList();
281
foreach( const QVariant& track, tracks )
282
parseTrack( track.toMap() );
285
parseTrack( res.value( "song" ).toMap() );
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 );
298
ExfmParser::pixmap() const
301
s_pixmap = new QPixmap( RESPATH "images/exfm.png" );