PageRenderTime 165ms CodeModel.GetById 40ms app.highlight 81ms RepoModel.GetById 40ms app.codeStats 0ms

/src/libtomahawk/infosystem/infoplugins/generic/echonestplugin.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 295 lines | 225 code | 44 blank | 26 comment | 16 complexity | 4a48798c03bca5c4d237b8bab0dbeba7 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  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 "echonestplugin.h"
 20#include <echonest/Artist.h>
 21#include <echonest/ArtistTypes.h>
 22
 23#include "utils/tomahawkutils.h"
 24#include "utils/logger.h"
 25
 26#include <QNetworkConfiguration>
 27
 28using namespace Tomahawk::InfoSystem;
 29using namespace Echonest;
 30
 31// for internal neatness
 32
 33EchoNestPlugin::EchoNestPlugin()
 34    : InfoPlugin()
 35{
 36    qDebug() << Q_FUNC_INFO;
 37    m_supportedGetTypes << Tomahawk::InfoSystem::InfoArtistBiography << Tomahawk::InfoSystem::InfoArtistFamiliarity << Tomahawk::InfoSystem::InfoArtistHotttness << Tomahawk::InfoSystem::InfoArtistTerms << Tomahawk::InfoSystem::InfoMiscTopTerms;
 38    Echonest::Config::instance()->setNetworkAccessManager( TomahawkUtils::nam() );
 39}
 40
 41
 42EchoNestPlugin::~EchoNestPlugin()
 43{
 44    qDebug() << Q_FUNC_INFO;
 45}
 46
 47
 48void
 49EchoNestPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
 50{
 51    switch ( requestData.type )
 52    {
 53        case Tomahawk::InfoSystem::InfoArtistBiography:
 54            return getArtistBiography( requestData );
 55        case Tomahawk::InfoSystem::InfoArtistFamiliarity:
 56            return getArtistFamiliarity( requestData );
 57        case Tomahawk::InfoSystem::InfoArtistHotttness:
 58            return getArtistHotttnesss( requestData );
 59        case Tomahawk::InfoSystem::InfoArtistTerms:
 60            return getArtistTerms( requestData );
 61        case Tomahawk::InfoSystem::InfoTrackEnergy:
 62            return getSongProfile( requestData, "energy" );
 63        case Tomahawk::InfoSystem::InfoMiscTopTerms:
 64            return getMiscTopTerms( requestData );
 65        default:
 66        {
 67            emit info( requestData, QVariant() );
 68            return;
 69        }
 70    }
 71}
 72
 73
 74void
 75EchoNestPlugin::getSongProfile( const Tomahawk::InfoSystem::InfoRequestData &requestData, const QString &item )
 76{
 77    //WARNING: Totally not implemented yet
 78    Q_UNUSED( item );
 79
 80    if( !isValidTrackData( requestData ) )
 81        return;
 82
 83//     Track track( input.toString() );
 84//     Artist artist( customData.input()->property("artistName").toString() );
 85//     reply->setProperty("artist", QVariant::fromValue<Artist>(artist));
 86//     reply->setProperty( "input", input );
 87//     m_replyMap[reply] = customData;
 88//     connect(reply, SIGNAL(finished()), SLOT(getArtistBiographySlot()));
 89}
 90
 91
 92void
 93EchoNestPlugin::getArtistBiography( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 94{
 95    if( !isValidArtistData( requestData ) )
 96        return;
 97
 98    Echonest::Artist artist( requestData.input.toString() );
 99    QNetworkReply *reply = artist.fetchBiographies();
100    reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
101    reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
102    connect( reply, SIGNAL( finished() ), SLOT( getArtistBiographySlot() ) );
103}
104
105
106void
107EchoNestPlugin::getArtistFamiliarity( const Tomahawk::InfoSystem::InfoRequestData &requestData )
108{
109    if( !isValidArtistData( requestData ) )
110        return;
111
112    qDebug() << "Fetching artist familiarity!" << requestData.input;
113    Echonest::Artist artist( requestData.input.toString() );
114    QNetworkReply* reply = artist.fetchFamiliarity();
115    reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
116    reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
117    connect( reply, SIGNAL( finished() ), SLOT( getArtistFamiliaritySlot() ) );
118}
119
120
121void
122EchoNestPlugin::getArtistHotttnesss( const Tomahawk::InfoSystem::InfoRequestData &requestData )
123{
124    if( !isValidArtistData( requestData ) )
125        return;
126
127    Echonest::Artist artist( requestData.input.toString() );
128    QNetworkReply* reply = artist.fetchHotttnesss();
129    reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
130    reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
131    connect( reply, SIGNAL( finished() ), SLOT( getArtistHotttnesssSlot() ) );
132}
133
134
135void
136EchoNestPlugin::getArtistTerms( const Tomahawk::InfoSystem::InfoRequestData &requestData )
137{
138    if( !isValidArtistData( requestData ) )
139        return;
140
141    Echonest::Artist artist( requestData.input.toString() );
142    QNetworkReply* reply = artist.fetchTerms( Echonest::Artist::Weight );
143    reply->setProperty( "artist", QVariant::fromValue< Echonest::Artist >( artist ) );
144    reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
145    connect( reply, SIGNAL( finished() ), SLOT( getArtistTermsSlot() ) );
146}
147
148
149void
150EchoNestPlugin::getMiscTopTerms( const Tomahawk::InfoSystem::InfoRequestData &requestData )
151{
152    QNetworkReply* reply = Echonest::Artist::topTerms( 20 );
153    reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
154    connect( reply, SIGNAL( finished() ), SLOT( getMiscTopSlot() ) );
155}
156
157
158void
159EchoNestPlugin::getArtistBiographySlot()
160{
161    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
162    Echonest::Artist artist = artistFromReply( reply );
163    BiographyList biographies = artist.biographies();
164    QVariantMap biographyMap;
165    Q_FOREACH( const Biography& biography, biographies )
166    {
167        QVariantHash siteData;
168        siteData[ "site" ] = biography.site();
169        siteData[ "url" ] = biography.url().toString();
170        siteData[ "text" ] = biography.text();
171        siteData[ "attribution" ] = biography.license().attribution;
172        siteData[ "licensetype" ] = biography.license().type;
173        siteData[ "attribution" ] = biography.license().url.toString();
174        biographyMap[ biography.site() ] = siteData;
175    }
176    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
177    emit info( requestData, biographyMap );
178    reply->deleteLater();
179}
180
181
182void
183EchoNestPlugin::getArtistFamiliaritySlot()
184{
185    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
186    Echonest::Artist artist = artistFromReply( reply );
187    qreal familiarity = artist.familiarity();
188    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
189    emit info( requestData, familiarity );
190    reply->deleteLater();
191}
192
193
194void
195EchoNestPlugin::getArtistHotttnesssSlot()
196{
197    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
198    Echonest::Artist artist = artistFromReply( reply );
199    qreal hotttnesss = artist.hotttnesss();
200    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
201    emit info( requestData, hotttnesss );
202    reply->deleteLater();
203}
204
205
206void
207EchoNestPlugin::getArtistTermsSlot()
208{
209    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
210    Echonest::Artist artist = artistFromReply( reply );
211    TermList terms = artist.terms();
212    QVariantMap termsMap;
213    Q_FOREACH( const Echonest::Term& term, terms ) {
214        QVariantHash termHash;
215        termHash[ "weight" ] = QString::number( term.weight() );
216        termHash[ "frequency" ] = QString::number( term.frequency() );
217        termsMap[ term.name() ] = termHash;
218    }
219    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
220    emit info( requestData, termsMap );
221    reply->deleteLater();
222}
223
224
225void
226EchoNestPlugin::getMiscTopSlot()
227{
228    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
229    TermList terms = Echonest::Artist::parseTopTerms( reply );
230    QVariantMap termsMap;
231    Q_FOREACH( const Echonest::Term& term, terms ) {
232        QVariantHash termHash;
233        termHash[ "weight" ] = QString::number( term.weight() );
234        termHash[ "frequency" ] = QString::number( term.frequency() );
235        termsMap[ term.name() ] = termHash;
236    }
237    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
238    emit info( requestData, termsMap );
239    reply->deleteLater();
240}
241
242
243bool
244EchoNestPlugin::isValidArtistData( const Tomahawk::InfoSystem::InfoRequestData &requestData )
245{
246    if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QString >() )
247    {
248        emit info( requestData, QVariant() );
249        return false;
250    }
251    QString artistName = requestData.input.toString();
252    if ( artistName.isEmpty() )
253    {
254        emit info( requestData, QVariant() );
255        return false;
256    }
257    return true;
258}
259
260
261bool
262EchoNestPlugin::isValidTrackData( const Tomahawk::InfoSystem::InfoRequestData &requestData )
263{
264    if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QString >() )
265    {
266        emit info( requestData, QVariant() );
267        return false;
268    }
269    QString trackName = requestData.input.toString();
270    if ( trackName.isEmpty() )
271    {
272        emit info( requestData, QVariant() );
273        return false;
274    }
275    if ( !requestData.customData.contains( "artistName" ) || requestData.customData[ "artistName" ].toString().isEmpty() )
276    {
277        emit info( requestData, QVariant() );
278        return false;
279    }
280    return true;
281}
282
283
284Artist
285EchoNestPlugin::artistFromReply( QNetworkReply* reply )
286{
287    Echonest::Artist artist = reply->property("artist").value<Echonest::Artist>();
288    try {
289        artist.parseProfile( reply );
290    } catch( const Echonest::ParseError& e ) {
291        qWarning() << "Caught parser error from echonest!" << e.what();
292    }
293    return artist;
294}
295//