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

« back to all changes in this revision

Viewing changes to src/infoplugins/generic/echonest/EchonestPlugin.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 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
#include "EchonestPlugin.h"
 
21
#include <echonest/ArtistTypes.h>
 
22
 
 
23
#include "utils/TomahawkUtils.h"
 
24
#include "utils/Logger.h"
 
25
 
 
26
#include <QNetworkConfiguration>
 
27
#include <QtPlugin>
 
28
 
 
29
namespace Tomahawk
 
30
{
 
31
 
 
32
namespace InfoSystem
 
33
{
 
34
 
 
35
// for internal neatness
 
36
 
 
37
EchonestPlugin::EchonestPlugin()
 
38
    : InfoPlugin()
 
39
{
 
40
    qDebug() << Q_FUNC_INFO;
 
41
    m_supportedGetTypes << Tomahawk::InfoSystem::InfoArtistFamiliarity << Tomahawk::InfoSystem::InfoArtistHotttness << Tomahawk::InfoSystem::InfoArtistTerms << Tomahawk::InfoSystem::InfoMiscTopTerms;
 
42
}
 
43
 
 
44
 
 
45
EchonestPlugin::~EchonestPlugin()
 
46
{
 
47
    qDebug() << Q_FUNC_INFO;
 
48
}
 
49
 
 
50
 
 
51
void
 
52
EchonestPlugin::init()
 
53
{
 
54
    Echonest::Config::instance()->setNetworkAccessManager( TomahawkUtils::nam() );
 
55
}
 
56
 
 
57
 
 
58
void
 
59
EchonestPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
 
60
{
 
61
    switch ( requestData.type )
 
62
    {
 
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 );
 
75
        default:
 
76
        {
 
77
            emit info( requestData, QVariant() );
 
78
            return;
 
79
        }
 
80
    }
 
81
}
 
82
 
 
83
 
 
84
void
 
85
EchonestPlugin::getSongProfile( const Tomahawk::InfoSystem::InfoRequestData &requestData, const QString &item )
 
86
{
 
87
    //WARNING: Totally not implemented yet
 
88
    Q_UNUSED( item );
 
89
 
 
90
    if( !isValidTrackData( requestData ) )
 
91
        return;
 
92
 
 
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()));
 
99
}
 
100
 
 
101
 
 
102
void
 
103
EchonestPlugin::getArtistBiography( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
104
{
 
105
    if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
 
106
    {
 
107
        return;
 
108
    }
 
109
    InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
 
110
    if ( !hash.contains( "artist" ) )
 
111
    {
 
112
        return;
 
113
    }
 
114
 
 
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() ) );
 
120
}
 
121
 
 
122
 
 
123
void
 
124
EchonestPlugin::getArtistFamiliarity( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
125
{
 
126
    if( !isValidArtistData( requestData ) )
 
127
        return;
 
128
 
 
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() ) );
 
135
}
 
136
 
 
137
 
 
138
void
 
139
EchonestPlugin::getArtistHotttnesss( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
140
{
 
141
    if( !isValidArtistData( requestData ) )
 
142
        return;
 
143
 
 
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() ) );
 
149
}
 
150
 
 
151
 
 
152
void
 
153
EchonestPlugin::getArtistTerms( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
154
{
 
155
    if( !isValidArtistData( requestData ) )
 
156
        return;
 
157
 
 
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() ) );
 
163
}
 
164
 
 
165
 
 
166
void
 
167
EchonestPlugin::getMiscTopTerms( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
168
{
 
169
    QNetworkReply* reply = Echonest::Artist::topTerms( 20 );
 
170
    reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
 
171
    connect( reply, SIGNAL( finished() ), SLOT( getMiscTopSlot() ) );
 
172
}
 
173
 
 
174
 
 
175
void
 
176
EchonestPlugin::getArtistBiographySlot()
 
177
{
 
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 )
 
183
    {
 
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;
 
191
    }
 
192
    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
 
193
    emit info( requestData, biographyMap );
 
194
    reply->deleteLater();
 
195
}
 
196
 
 
197
 
 
198
void
 
199
EchonestPlugin::getArtistFamiliaritySlot()
 
200
{
 
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();
 
207
}
 
208
 
 
209
 
 
210
void
 
211
EchonestPlugin::getArtistHotttnesssSlot()
 
212
{
 
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();
 
219
}
 
220
 
 
221
 
 
222
void
 
223
EchonestPlugin::getArtistTermsSlot()
 
224
{
 
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;
 
234
    }
 
235
    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
 
236
    emit info( requestData, termsMap );
 
237
    reply->deleteLater();
 
238
}
 
239
 
 
240
 
 
241
void
 
242
EchonestPlugin::getMiscTopSlot()
 
243
{
 
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;
 
252
    }
 
253
    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
 
254
    emit info( requestData, termsMap );
 
255
    reply->deleteLater();
 
256
}
 
257
 
 
258
 
 
259
bool
 
260
EchonestPlugin::isValidArtistData( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
261
{
 
262
    if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QString >() )
 
263
    {
 
264
        emit info( requestData, QVariant() );
 
265
        return false;
 
266
    }
 
267
    QString artistName = requestData.input.toString();
 
268
    if ( artistName.isEmpty() )
 
269
    {
 
270
        emit info( requestData, QVariant() );
 
271
        return false;
 
272
    }
 
273
    return true;
 
274
}
 
275
 
 
276
 
 
277
bool
 
278
EchonestPlugin::isValidTrackData( const Tomahawk::InfoSystem::InfoRequestData &requestData )
 
279
{
 
280
    if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QString >() )
 
281
    {
 
282
        emit info( requestData, QVariant() );
 
283
        return false;
 
284
    }
 
285
    QString trackName = requestData.input.toString();
 
286
    if ( trackName.isEmpty() )
 
287
    {
 
288
        emit info( requestData, QVariant() );
 
289
        return false;
 
290
    }
 
291
    if ( !requestData.customData.contains( "artistName" ) || requestData.customData[ "artistName" ].toString().isEmpty() )
 
292
    {
 
293
        emit info( requestData, QVariant() );
 
294
        return false;
 
295
    }
 
296
    return true;
 
297
}
 
298
 
 
299
 
 
300
Echonest::Artist
 
301
EchonestPlugin::artistFromReply( QNetworkReply* reply )
 
302
{
 
303
    Echonest::Artist artist = reply->property("artist").value<Echonest::Artist>();
 
304
    try {
 
305
        artist.parseProfile( reply );
 
306
    } catch( const Echonest::ParseError& e ) {
 
307
        qWarning() << "Caught parser error from echonest!" << e.what();
 
308
    }
 
309
    return artist;
 
310
}
 
311
 
 
312
} //ns InfoSystem
 
313
 
 
314
} //ns Tomahawk
 
315
 
 
316
Q_EXPORT_PLUGIN2( Tomahawk::InfoSystem::InfoPlugin, Tomahawk::InfoSystem::EchonestPlugin )