PageRenderTime 27ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/src/infoplugins/generic/musixmatch/MusixMatchPlugin.cpp

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