1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "RoviPlugin.h"
23
#include <QNetworkReply>
27
#include "utils/Logger.h"
29
using namespace Tomahawk::InfoSystem;
32
RoviPlugin::RoviPlugin()
35
m_supportedGetTypes << InfoAlbumSongs;
38
* Your API Key is 7jxr9zggt45h6rg2n4ss3mrj
39
* Your secret is XUnYutaAW6
41
m_apiKey = "7jxr9zggt45h6rg2n4ss3mrj";
42
m_secret = "XUnYutaAW6";
46
RoviPlugin::~RoviPlugin()
52
RoviPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
54
if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
56
emit info( requestData, QVariant() );
59
InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
60
if ( !hash.contains( "artist" ) || !hash.contains( "album" ) )
62
emit info( requestData, QVariant() );
66
Tomahawk::InfoSystem::InfoStringHash criteria;
67
criteria["artist"] = hash["artist"];
68
criteria["album"] = hash["album"];
70
emit getCachedInfo( criteria, 0, requestData );
75
RoviPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData )
77
switch ( requestData.type )
81
QUrl baseUrl = QUrl( "http://api.rovicorp.com/search/v2/music/search" );
82
baseUrl.addQueryItem( "query", QString( "%1 %2" ).arg( criteria[ "artist" ] ).arg( criteria[ "album" ] ) );
83
baseUrl.addQueryItem( "entitytype", "album" );
84
baseUrl.addQueryItem( "include", "album:tracks" );
86
QNetworkReply* reply = makeRequest( baseUrl );
88
reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
89
connect( reply, SIGNAL( finished() ), this, SLOT( albumLookupFinished() ) );
90
connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( albumLookupError( QNetworkReply::NetworkError ) ) );
103
RoviPlugin::albumLookupError( QNetworkReply::NetworkError error )
105
if ( error == QNetworkReply::NoError )
108
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
111
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
113
emit info( requestData, QVariant() );
119
RoviPlugin::albumLookupFinished()
121
QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
124
if ( reply->error() != QNetworkReply::NoError )
127
Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
131
QVariantMap response = p.parse( reply, &ok ).toMap();
133
if ( !ok || response.isEmpty() || !response.contains( "searchResponse" ) )
135
tLog() << "Error parsing JSON from Rovi!" << p.errorString() << response;
136
emit info( requestData, QVariant() );
140
QVariantList resultList = response[ "searchResponse" ].toMap().value( "results" ).toList();
141
if ( resultList.size() == 0 )
143
emit info( requestData, QVariant() );
147
QVariantMap results = resultList.first().toMap();
148
QVariantList tracks = results[ "album" ].toMap()[ "tracks" ].toList();
150
if ( tracks.isEmpty() )
152
tLog() << "Error parsing JSON from Rovi!" << p.errorString() << response;
153
emit info( requestData, QVariant() );
157
QStringList trackNameList;
158
foreach ( const QVariant& track, tracks )
160
const QVariantMap trackData = track.toMap();
161
if ( trackData.contains( "title" ) )
162
trackNameList << trackData[ "title" ].toString();
165
QVariantMap returnedData;
166
returnedData["tracks"] = trackNameList;
168
emit info( requestData, returnedData );
170
Tomahawk::InfoSystem::InfoStringHash criteria;
171
criteria["artist"] = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>()["artist"];
172
criteria["album"] = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>()["album"];
174
emit updateCache( criteria, 0, requestData.type, returnedData );
179
RoviPlugin::makeRequest( QUrl url )
181
url.addQueryItem( "apikey", m_apiKey );
182
url.addEncodedQueryItem( "sig", generateSig() );
184
qDebug() << "Rovi request url:" << url.toString();
185
return TomahawkUtils::nam()->get( QNetworkRequest( url ) );
190
RoviPlugin::generateSig() const
192
QByteArray raw = m_apiKey + m_secret + QString::number( QDateTime::currentMSecsSinceEpoch() / 1000 ).toLatin1();
193
return TomahawkUtils::md5( raw ).toLatin1();
197
Q_EXPORT_PLUGIN2( Tomahawk::InfoSystem::InfoPlugin, Tomahawk::InfoSystem::RoviPlugin )