PageRenderTime 303ms CodeModel.GetById 50ms app.highlight 162ms RepoModel.GetById 80ms app.codeStats 1ms

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

http://github.com/tomahawk-player/tomahawk
C++ | 190 lines | 128 code | 41 blank | 21 comment | 15 complexity | 274c097b46929818d961d53f70f263d1 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "RoviPlugin.h"
 20
 21#include "utils/logger.h"
 22
 23#include <QDateTime>
 24#include <QNetworkReply>
 25#include <parser.h>
 26
 27using namespace Tomahawk::InfoSystem;
 28
 29
 30RoviPlugin::RoviPlugin()
 31    : InfoPlugin()
 32{
 33    m_supportedGetTypes << InfoAlbumSongs;
 34
 35    /*
 36     *    Your API Key is 7jxr9zggt45h6rg2n4ss3mrj
 37     *    Your secret is XUnYutaAW6
 38     */
 39    m_apiKey = "7jxr9zggt45h6rg2n4ss3mrj";
 40    m_secret = "XUnYutaAW6";
 41}
 42
 43RoviPlugin::~RoviPlugin()
 44{
 45
 46}
 47
 48
 49void
 50RoviPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
 51{
 52    if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
 53    {
 54        emit info( requestData, QVariant() );
 55        return;
 56    }
 57    InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >();
 58    if ( !hash.contains( "artist" ) || !hash.contains( "album" ) )
 59    {
 60        emit info( requestData, QVariant() );
 61        return;
 62    }
 63
 64    Tomahawk::InfoSystem::InfoStringHash criteria;
 65    criteria["artist"] = hash["artist"];
 66    criteria["album"] = hash["album"];
 67
 68    emit getCachedInfo( criteria, 0, requestData );
 69}
 70
 71void
 72RoviPlugin::notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData )
 73{
 74    switch ( requestData.type )
 75    {
 76        case InfoAlbumSongs:
 77        {
 78            QUrl baseUrl = QUrl( "http://api.rovicorp.com/search/v2/music/search" );
 79            baseUrl.addQueryItem( "query", QString( "%1 %2" ).arg( criteria[ "artist" ] ).arg( criteria[ "album" ] ) );
 80            baseUrl.addQueryItem( "entitytype", "album" );
 81            baseUrl.addQueryItem( "include", "album:tracks" );
 82
 83            QNetworkReply* reply = makeRequest( baseUrl );
 84
 85            reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
 86            connect( reply, SIGNAL( finished() ), this, SLOT( albumLookupFinished() ) );
 87            connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), this, SLOT( albumLookupError( QNetworkReply::NetworkError ) ) );
 88            break;
 89        }
 90        default:
 91        {
 92            Q_ASSERT( false );
 93            break;
 94        }
 95    }
 96}
 97
 98void
 99RoviPlugin::albumLookupError( QNetworkReply::NetworkError error )
100{
101    if ( error == QNetworkReply::NoError )
102        return;
103
104    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
105    Q_ASSERT( reply );
106
107    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
108
109    emit info( requestData, QVariant() );
110
111}
112
113void
114RoviPlugin::albumLookupFinished()
115{
116    QNetworkReply* reply = qobject_cast<QNetworkReply*>( sender() );
117    Q_ASSERT( reply );
118
119    if ( reply->error() != QNetworkReply::NoError )
120        return;
121
122    Tomahawk::InfoSystem::InfoRequestData requestData = reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >();
123
124    QJson::Parser p;
125    bool ok;
126    QVariantMap response = p.parse( reply, &ok ).toMap();
127
128    if ( !ok || response.isEmpty() || !response.contains( "searchResponse" ) )
129    {
130        tLog() << "Error parsing JSON from Rovi!" << p.errorString() << response;
131        emit info( requestData, QVariant() );
132        return;
133    }
134
135    QVariantList resultList = response[ "searchResponse" ].toMap().value( "results" ).toList();
136    if ( resultList.size() == 0 )
137    {
138        emit info( requestData, QVariant() );
139        return;
140    }
141
142    QVariantMap results = resultList.first().toMap();
143    QVariantList tracks = results[ "album" ].toMap()[ "tracks" ].toList();
144
145    if ( tracks.isEmpty() )
146    {
147        tLog() << "Error parsing JSON from Rovi!" << p.errorString() << response;
148        emit info( requestData, QVariant() );
149    }
150
151
152    QStringList trackNameList;
153    foreach ( const QVariant& track, tracks )
154    {
155        const QVariantMap trackData = track.toMap();
156        if ( trackData.contains( "title" ) )
157            trackNameList << trackData[ "title" ].toString();
158    }
159
160    QVariantMap returnedData;
161    returnedData["tracks"] = trackNameList;
162
163    emit info( requestData, returnedData );
164
165    Tomahawk::InfoSystem::InfoStringHash criteria;
166    criteria["artist"] = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>()["artist"];
167    criteria["album"] = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>()["album"];
168
169    emit updateCache( criteria, 0, requestData.type, returnedData );
170}
171
172
173QNetworkReply*
174RoviPlugin::makeRequest( QUrl url )
175{
176    url.addQueryItem( "apikey", m_apiKey );
177    url.addEncodedQueryItem( "sig", generateSig() );
178
179    qDebug() << "Rovi request url:" << url.toString();
180    return TomahawkUtils::nam()->get( QNetworkRequest( url ) );
181}
182
183
184QByteArray
185RoviPlugin::generateSig() const
186{
187    QByteArray raw = m_apiKey + m_secret + QString::number( QDateTime::currentMSecsSinceEpoch() / 1000 ).toLatin1();
188    return TomahawkUtils::md5( raw ).toLatin1();
189
190}