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

« back to all changes in this revision

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