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