PageRenderTime 173ms CodeModel.GetById 80ms app.highlight 18ms RepoModel.GetById 73ms app.codeStats 0ms

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