1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
4
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
6
* Tomahawk is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
11
* Tomahawk is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
20
#include "EchonestPlugin.h"
21
#include <echonest/ArtistTypes.h>
23
#include "utils/TomahawkUtils.h"
24
#include "utils/Logger.h"
26
#include <QNetworkConfiguration>
35
// for internal neatness
37
EchonestPlugin::EchonestPlugin()
40
qDebug() << Q_FUNC_INFO;
41
m_supportedGetTypes << Tomahawk::InfoSystem::InfoArtistFamiliarity << Tomahawk::InfoSystem::InfoArtistHotttness << Tomahawk::InfoSystem::InfoArtistTerms << Tomahawk::InfoSystem::InfoMiscTopTerms;
45
EchonestPlugin::~EchonestPlugin()
47
qDebug() << Q_FUNC_INFO;
52
EchonestPlugin::init()
54
Echonest::Config::instance()->setNetworkAccessManager( TomahawkUtils::nam() );
59
EchonestPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
61
switch ( requestData.type )
63
case Tomahawk::InfoSystem::InfoArtistBiography:
64
return getArtistBiography( requestData );
65
case Tomahawk::InfoSystem::InfoArtistFamiliarity:
66
return getArtistFamiliarity( requestData );
67
case Tomahawk::InfoSystem::InfoArtistHotttness:
68
return getArtistHotttnesss( requestData );
69
case Tomahawk::InfoSystem::InfoArtistTerms:
70
return getArtistTerms( requestData );
71
case Tomahawk::InfoSystem::InfoTrackEnergy:
72
return getSongProfile( requestData, "energy" );
73
case Tomahawk::InfoSystem::InfoMiscTopTerms:
74
return getMiscTopTerms( requestData );
77
emit info( requestData, QVariant() );
85
EchonestPlugin::getSongProfile( const Tomahawk::InfoSystem::InfoRequestData &requestData, const QString &item )
87
//WARNING: Totally not implemented yet
90
if( !isValidTrackData( requestData ) )
93
// Track track( input.toString() );
94
// Artist artist( customData.input()->property("artistName").toString() );
95
// reply->setProperty("artist", QVariant::fromValue<Artist>(artist));
96
// reply->setProperty( "input", input );
97
// m_replyMap[reply] = customData;
98
// connect(reply, SIGNAL(finished()), SLOT(getArtistBiographySlot()));
103
EchonestPlugin::getArtistBiography( const Tomahawk::InfoSystem::InfoRequestData &requestData )
105
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
109
InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
110
if ( !hash.contains( "artist" ) )
115
Echonest::Artist artist( hash["artist"] );
116
QNetworkReply *reply = artist.fetchBiographies();
117
reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
118
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
119
connect( reply, SIGNAL( finished() ), SLOT( getArtistBiographySlot() ) );
124
EchonestPlugin::getArtistFamiliarity( const Tomahawk::InfoSystem::InfoRequestData &requestData )
126
if( !isValidArtistData( requestData ) )
129
qDebug() << "Fetching artist familiarity!" << requestData.input;
130
Echonest::Artist artist( requestData.input.toString() );
131
QNetworkReply* reply = artist.fetchFamiliarity();
132
reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
133
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
134
connect( reply, SIGNAL( finished() ), SLOT( getArtistFamiliaritySlot() ) );
139
EchonestPlugin::getArtistHotttnesss( const Tomahawk::InfoSystem::InfoRequestData &requestData )
141
if( !isValidArtistData( requestData ) )
144
Echonest::Artist artist( requestData.input.toString() );
145
QNetworkReply* reply = artist.fetchHotttnesss();
146
reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
147
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
148
connect( reply, SIGNAL( finished() ), SLOT( getArtistHotttnesssSlot() ) );
153
EchonestPlugin::getArtistTerms( const Tomahawk::InfoSystem::InfoRequestData &requestData )
155
if( !isValidArtistData( requestData ) )
158
Echonest::Artist artist( requestData.input.toString() );
159
QNetworkReply* reply = artist.fetchTerms( Echonest::Artist::Weight );
160
reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
161
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
162
connect( reply, SIGNAL( finished() ), SLOT( getArtistTermsSlot() ) );
167
EchonestPlugin::getMiscTopTerms( const Tomahawk::InfoSystem::InfoRequestData &requestData )
169
QNetworkReply* reply = Echonest::Artist::topTerms( 20 );
170
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
171
connect( reply, SIGNAL( finished() ), SLOT( getMiscTopSlot() ) );
176
EchonestPlugin::getArtistBiographySlot()
178
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
179
Echonest::Artist artist = artistFromReply( reply );
180
Echonest::BiographyList biographies = artist.biographies();
181
QVariantMap biographyMap;
182
Q_FOREACH( const Echonest::Biography& biography, biographies )
184
QVariantHash siteData;
185
siteData[ "site" ] = biography.site();
186
siteData[ "url" ] = biography.url().toString();
187
siteData[ "text" ] = biography.text();
188
siteData[ "attribution" ] = biography.license().attribution;
189
siteData[ "licensetype" ] = biography.license().type;
190
biographyMap[ biography.site() ] = siteData;
192
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
193
emit info( requestData, biographyMap );
194
reply->deleteLater();
199
EchonestPlugin::getArtistFamiliaritySlot()
201
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
202
Echonest::Artist artist = artistFromReply( reply );
203
qreal familiarity = artist.familiarity();
204
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
205
emit info( requestData, familiarity );
206
reply->deleteLater();
211
EchonestPlugin::getArtistHotttnesssSlot()
213
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
214
Echonest::Artist artist = artistFromReply( reply );
215
qreal hotttnesss = artist.hotttnesss();
216
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
217
emit info( requestData, hotttnesss );
218
reply->deleteLater();
223
EchonestPlugin::getArtistTermsSlot()
225
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
226
Echonest::Artist artist = artistFromReply( reply );
227
Echonest::TermList terms = artist.terms();
228
QVariantMap termsMap;
229
Q_FOREACH( const Echonest::Term& term, terms ) {
230
QVariantHash termHash;
231
termHash[ "weight" ] = QString::number( term.weight() );
232
termHash[ "frequency" ] = QString::number( term.frequency() );
233
termsMap[ term.name() ] = termHash;
235
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
236
emit info( requestData, termsMap );
237
reply->deleteLater();
242
EchonestPlugin::getMiscTopSlot()
244
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
245
Echonest::TermList terms = Echonest::Artist::parseTopTerms( reply );
246
QVariantMap termsMap;
247
Q_FOREACH( const Echonest::Term& term, terms ) {
248
QVariantHash termHash;
249
termHash[ "weight" ] = QString::number( term.weight() );
250
termHash[ "frequency" ] = QString::number( term.frequency() );
251
termsMap[ term.name() ] = termHash;
253
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
254
emit info( requestData, termsMap );
255
reply->deleteLater();
260
EchonestPlugin::isValidArtistData( const Tomahawk::InfoSystem::InfoRequestData &requestData )
262
if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QString >() )
264
emit info( requestData, QVariant() );
267
QString artistName = requestData.input.toString();
268
if ( artistName.isEmpty() )
270
emit info( requestData, QVariant() );
278
EchonestPlugin::isValidTrackData( const Tomahawk::InfoSystem::InfoRequestData &requestData )
280
if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QString >() )
282
emit info( requestData, QVariant() );
285
QString trackName = requestData.input.toString();
286
if ( trackName.isEmpty() )
288
emit info( requestData, QVariant() );
291
if ( !requestData.customData.contains( "artistName" ) || requestData.customData[ "artistName" ].toString().isEmpty() )
293
emit info( requestData, QVariant() );
301
EchonestPlugin::artistFromReply( QNetworkReply* reply )
303
Echonest::Artist artist = reply->property("artist").value<Echonest::Artist>();
305
artist.parseProfile( reply );
306
} catch( const Echonest::ParseError& e ) {
307
qWarning() << "Caught parser error from echonest!" << e.what();
316
Q_EXPORT_PLUGIN2( Tomahawk::InfoSystem::InfoPlugin, Tomahawk::InfoSystem::EchonestPlugin )