diff --git a/src/infoplugins/generic/CMakeLists.txt b/src/infoplugins/generic/CMakeLists.txt index c077e1e77..86c3db52d 100644 --- a/src/infoplugins/generic/CMakeLists.txt +++ b/src/infoplugins/generic/CMakeLists.txt @@ -10,6 +10,7 @@ list(APPEND simple_plugins MusicBrainz Rovi Discogs + Songkick ) foreach(simple_plugin ${simple_plugins}) diff --git a/src/infoplugins/generic/songkick/SongkickPlugin.cpp b/src/infoplugins/generic/songkick/SongkickPlugin.cpp new file mode 100644 index 000000000..0eb7cecce --- /dev/null +++ b/src/infoplugins/generic/songkick/SongkickPlugin.cpp @@ -0,0 +1,155 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Jeff Mitchell + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "SongkickPlugin.h" + +#include +#include +#include + +#include "utils/TomahawkUtils.h" +#include "utils/Logger.h" + +using namespace Tomahawk::InfoSystem; + + +// for internal neatness + +SongkickPlugin::SongkickPlugin() + : InfoPlugin() + , m_apiKey( "61be4ea5aea7dd942d52b2f1311dd9fe" ) +{ + tDebug() << Q_FUNC_INFO; + m_supportedGetTypes << Tomahawk::InfoSystem::InfoTrackLyrics; +} + + +SongkickPlugin::~SongkickPlugin() +{ + qDebug() << Q_FUNC_INFO; +} + + +void +SongkickPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) +{ + tDebug() << Q_FUNC_INFO; + if( !isValidTrackData( requestData ) || requestData.type != Tomahawk::InfoSystem::InfoTrackLyrics ) + return; + InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); + + QString artist = hash["artist"]; + QString track = hash["track"]; + if( artist.isEmpty() || track.isEmpty() ) + { + emit info( requestData, QVariant() ); + return; + } + tDebug() << "artist is " << artist << ", track is " << track; + QString requestString( "http://api.musixmatch.com/ws/1.1/track.search?format=xml&page_size=1&f_has_lyrics=1" ); + QUrl url( requestString ); + url.addQueryItem( "apikey", m_apiKey ); + url.addQueryItem( "q_artist", artist ); + url.addQueryItem( "q_track", track ); + QNetworkReply* reply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + reply->setProperty( "requestData", QVariant::fromValue< Tomahawk::InfoSystem::InfoRequestData >( requestData ) ); + + connect( reply, SIGNAL( finished() ), SLOT( trackSearchSlot() ) ); +} + + +bool +SongkickPlugin::isValidTrackData( Tomahawk::InfoSystem::InfoRequestData requestData ) +{ + tDebug() << Q_FUNC_INFO; + + if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() ) + { + emit info( requestData, QVariant() ); + tDebug() << "SongkickPlugin::isValidTrackData: Data null, invalid, or can't convert" << requestData.input.isNull() << requestData.input.isValid() << requestData.input.canConvert< QVariantMap >(); + return false; + } + InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); + + if ( hash[ "track" ].isEmpty() ) + { + emit info( requestData, QVariant() ); + tDebug() << "SongkickPlugin::isValidTrackData: Track name is empty"; + return false; + } + if ( hash[ "artist" ].isEmpty() ) + { + emit info( requestData, QVariant() ); + tDebug() << "SongkickPlugin::isValidTrackData: No artist name found"; + return false; + } + return true; +} + + +void +SongkickPlugin::trackSearchSlot() +{ + tDebug() << Q_FUNC_INFO; + QNetworkReply* oldReply = qobject_cast( sender() ); + if ( !oldReply ) + return; //timeout will handle it + + QDomDocument doc; + doc.setContent(oldReply->readAll()); + qDebug() << doc.toString(); + QDomNodeList domNodeList = doc.elementsByTagName("track_id"); + if ( domNodeList.isEmpty() ) + { + emit info( oldReply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant() ); + return; + } + QString track_id = domNodeList.at(0).toElement().text(); + QString requestString( "http://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=%1&format=xml&apikey=%2" ); + QUrl url( requestString ); + url.addQueryItem( "apikey", m_apiKey ); + url.addQueryItem( "track_id", track_id ); + QNetworkReply* newReply = TomahawkUtils::nam()->get( QNetworkRequest( url ) ); + newReply->setProperty( "requestData", oldReply->property( "requestData" ) ); + connect( newReply, SIGNAL( finished() ), SLOT( trackLyricsSlot() ) ); +} + + +void +SongkickPlugin::trackLyricsSlot() +{ + tDebug() << Q_FUNC_INFO; + QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() ); + if ( !reply ) + return; //timeout will handle it + + QDomDocument doc; + doc.setContent( reply->readAll() ); + QDomNodeList domNodeList = doc.elementsByTagName( "lyrics_body" ); + if ( domNodeList.isEmpty() ) + { + emit info( reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant() ); + return; + } + QString lyrics = domNodeList.at(0).toElement().text(); + emit info( reply->property( "requestData" ).value< Tomahawk::InfoSystem::InfoRequestData >(), QVariant( lyrics ) ); +} + + +Q_EXPORT_PLUGIN2( Tomahawk::InfoSystem::InfoPlugin, Tomahawk::InfoSystem::SongkickPlugin ) diff --git a/src/infoplugins/generic/songkick/SongkickPlugin.h b/src/infoplugins/generic/songkick/SongkickPlugin.h new file mode 100644 index 000000000..89f7a7cfd --- /dev/null +++ b/src/infoplugins/generic/songkick/SongkickPlugin.h @@ -0,0 +1,73 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2011, Jeff Mitchell + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef SONGKICKPLUGIN_H +#define SONGKICKPLUGIN_H + +#include "infosystem/InfoSystem.h" +#include "infosystem/InfoSystemWorker.h" +#include "infoplugins/InfoPluginDllMacro.h" + +class QNetworkReply; + +namespace Tomahawk +{ + +namespace InfoSystem +{ + +class INFOPLUGINDLLEXPORT SongkickPlugin : public InfoPlugin +{ + Q_OBJECT + Q_INTERFACES( Tomahawk::InfoSystem::InfoPlugin ) + +public: + SongkickPlugin(); + virtual ~SongkickPlugin(); + +public slots: + void trackSearchSlot(); + void trackLyricsSlot(); + +protected slots: + virtual void init() {} + virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ); + + virtual void pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) + { + Q_UNUSED( pushData ); + } + +virtual void notInCacheSlot( Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ) + { + Q_UNUSED( criteria ); + Q_UNUSED( requestData ); + } + +private: + bool isValidTrackData( Tomahawk::InfoSystem::InfoRequestData requestData ); + + QString m_apiKey; +}; + +} + +} + +#endif // SONGKICKPLUGIN_H