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

http://github.com/tomahawk-player/tomahawk · C++ · 143 lines · 110 code · 15 blank · 18 comment · 15 complexity · c588e828e4440549650c5dec37a79386 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.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 "musixmatchplugin.h"
  19. #include <QNetworkReply>
  20. #include <QDomDocument>
  21. #include "utils/tomahawkutils.h"
  22. #include "utils/logger.h"
  23. using namespace Tomahawk::InfoSystem;
  24. // for internal neatness
  25. MusixMatchPlugin::MusixMatchPlugin()
  26. : InfoPlugin()
  27. , m_apiKey("61be4ea5aea7dd942d52b2f1311dd9fe")
  28. {
  29. qDebug() << Q_FUNC_INFO;
  30. m_supportedGetTypes << Tomahawk::InfoSystem::InfoTrackLyrics;
  31. }
  32. MusixMatchPlugin::~MusixMatchPlugin()
  33. {
  34. qDebug() << Q_FUNC_INFO;
  35. }
  36. void
  37. MusixMatchPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
  38. {
  39. qDebug() << Q_FUNC_INFO;
  40. if( !isValidTrackData( requestData ) || !requestData.input.canConvert< QVariantMap >() || requestData.type != Tomahawk::InfoSystem::InfoTrackLyrics )
  41. return;
  42. QVariantMap hash = requestData.input.value< QVariantMap >();
  43. QString artist = hash["artistName"].toString();
  44. QString track = hash["trackName"].toString();
  45. if( artist.isEmpty() || track.isEmpty() )
  46. {
  47. emit info( requestData, QVariant() );
  48. return;
  49. }
  50. qDebug() << "artist is " << artist << ", track is " << track;
  51. QString requestString( "http://api.musixmatch.com/ws/1.1/track.search?format=xml&page_size=1&f_has_lyrics=1" );
  52. QUrl url( requestString );
  53. url.addQueryItem( "apikey", m_apiKey );
  54. url.addQueryItem( "q_artist", artist );
  55. url.addQueryItem( "q_track", track );
  56. QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
  57. reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) );
  58. connect( reply, SIGNAL( finished() ), SLOT( trackSearchSlot() ) );
  59. }
  60. bool
  61. MusixMatchPlugin::isValidTrackData( Tomahawk::InfoSystem::InfoRequestData requestData )
  62. {
  63. qDebug() << Q_FUNC_INFO;
  64. if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QVariantMap >() )
  65. {
  66. emit info( requestData, QVariant() );
  67. qDebug() << "MusixMatchPlugin::isValidTrackData: Data null, invalid, or can't convert";
  68. return false;
  69. }
  70. QVariantMap hash = requestData.input.value< QVariantMap >();
  71. if ( hash[ "trackName" ].toString().isEmpty() )
  72. {
  73. emit info( requestData, QVariant() );
  74. qDebug() << "MusixMatchPlugin::isValidTrackData: Track name is empty";
  75. return false;
  76. }
  77. if ( hash[ "artistName" ].toString().isEmpty() )
  78. {
  79. emit info( requestData, QVariant() );
  80. qDebug() << "MusixMatchPlugin::isValidTrackData: No artist name found";
  81. return false;
  82. }
  83. return true;
  84. }
  85. void
  86. MusixMatchPlugin::trackSearchSlot()
  87. {
  88. qDebug() << Q_FUNC_INFO;
  89. QNetworkReply* oldReply = qobject_cast<QNetworkReply*>( sender() );
  90. if ( !oldReply )
  91. return; //timeout will handle it
  92. QDomDocument doc;
  93. doc.setContent(oldReply->readAll());
  94. qDebug() << doc.toString();
  95. QDomNodeList domNodeList = doc.elementsByTagName("track_id");
  96. if ( domNodeList.isEmpty() )
  97. {
  98. emit info( oldReply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant() );
  99. return;
  100. }
  101. QString track_id = domNodeList.at(0).toElement().text();
  102. QString requestString( "http://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=%1&format=xml&apikey=%2" );
  103. QUrl url( requestString );
  104. url.addQueryItem( "apikey", m_apiKey );
  105. url.addQueryItem( "track_id", track_id );
  106. QNetworkReply* newReply = TomahawkUtils::nam()->get( QNetworkRequest( url ) );
  107. newReply->setProperty( "requestData", oldReply->property( "requestData" ) );
  108. connect( newReply, SIGNAL( finished() ), SLOT( trackLyricsSlot() ) );
  109. }
  110. void
  111. MusixMatchPlugin::trackLyricsSlot()
  112. {
  113. qDebug() << Q_FUNC_INFO;
  114. QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() );
  115. if ( !reply )
  116. return; //timeout will handle it
  117. QDomDocument doc;
  118. doc.setContent( reply->readAll() );
  119. QDomNodeList domNodeList = doc.elementsByTagName( "lyrics_body" );
  120. if ( domNodeList.isEmpty() )
  121. {
  122. emit info( reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant() );
  123. return;
  124. }
  125. QString lyrics = domNodeList.at(0).toElement().text();
  126. qDebug() << "Emitting lyrics: " << lyrics;
  127. emit info( reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant( lyrics ) );
  128. }