1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 17:43:59 +02:00

Add Songkick -- right now a verbatim copy of MusixMatch while I wait for

an API key and docs
This commit is contained in:
Jeff Mitchell
2012-06-24 17:36:05 -04:00
parent 7dd88208a7
commit 0f0a83786b
3 changed files with 229 additions and 0 deletions

View File

@@ -10,6 +10,7 @@ list(APPEND simple_plugins
MusicBrainz MusicBrainz
Rovi Rovi
Discogs Discogs
Songkick
) )
foreach(simple_plugin ${simple_plugins}) foreach(simple_plugin ${simple_plugins})

View File

@@ -0,0 +1,155 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "SongkickPlugin.h"
#include <QNetworkReply>
#include <QDomDocument>
#include <QtPlugin>
#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<QNetworkReply*>( 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 )

View File

@@ -0,0 +1,73 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#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