From 677944d403e0eed43f764adb2277777dbf32de18 Mon Sep 17 00:00:00 2001 From: Leo Franchi Date: Thu, 24 May 2012 20:59:17 -0400 Subject: [PATCH] Discogs infoplugin, only albums contents atm. --- src/infoplugins/generic/CMakeLists.txt | 1 + .../generic/discogs/DiscogsPlugin.cpp | 189 ++++++++++++++++++ .../generic/discogs/DiscogsPlugin.h | 63 ++++++ .../generic/musicbrainz/MusicBrainzPlugin.cpp | 26 --- .../generic/musicbrainz/MusicBrainzPlugin.h | 3 - 5 files changed, 253 insertions(+), 29 deletions(-) create mode 100644 src/infoplugins/generic/discogs/DiscogsPlugin.cpp create mode 100644 src/infoplugins/generic/discogs/DiscogsPlugin.h diff --git a/src/infoplugins/generic/CMakeLists.txt b/src/infoplugins/generic/CMakeLists.txt index 7473744f1..c077e1e77 100644 --- a/src/infoplugins/generic/CMakeLists.txt +++ b/src/infoplugins/generic/CMakeLists.txt @@ -9,6 +9,7 @@ list(APPEND simple_plugins MusixMatch MusicBrainz Rovi + Discogs ) foreach(simple_plugin ${simple_plugins}) diff --git a/src/infoplugins/generic/discogs/DiscogsPlugin.cpp b/src/infoplugins/generic/discogs/DiscogsPlugin.cpp new file mode 100644 index 000000000..f4dc88652 --- /dev/null +++ b/src/infoplugins/generic/discogs/DiscogsPlugin.cpp @@ -0,0 +1,189 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2012 Leo Franchi + * + * 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 "DiscogsPlugin.h" + +#include +#include +#include + +#include "utils/TomahawkUtils.h" +#include "utils/Logger.h" +#include "utils/Closure.h" +#include + +using namespace Tomahawk::InfoSystem; + + +DiscogsPlugin::DiscogsPlugin() + : InfoPlugin() +{ + qDebug() << Q_FUNC_INFO; + m_supportedGetTypes << Tomahawk::InfoSystem::InfoAlbumSongs; +} + + +DiscogsPlugin::~DiscogsPlugin() {} + + +void +DiscogsPlugin::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ) +{ + if ( !requestData.input.canConvert< Tomahawk::InfoSystem::InfoStringHash >() ) + { + emit info( requestData, QVariant() ); + return; + } + + InfoStringHash hash = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash >(); + if ( !hash.contains( "artist" ) || !hash.contains( "album" ) ) + { + emit info( requestData, QVariant() ); + return; + } + + switch ( requestData.type ) + { + case InfoAlbumSongs: + { + + Tomahawk::InfoSystem::InfoStringHash criteria; + criteria["artist"] = hash["artist"]; + criteria["album"] = hash["album"]; + + emit getCachedInfo( criteria, 2419200000, requestData ); + + break; + } + + default: + { + Q_ASSERT( false ); + break; + } + } +} + + +void +DiscogsPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requestData ) +{ + switch ( requestData.type ) + { + case InfoAlbumSongs: + { + QString requestString( "http://api.discogs.com/database/search" ); + QUrl url( requestString ); + url.addQueryItem( "type", "release" ); + url.addQueryItem( "release_title", criteria[ "album" ] ); + url.addQueryItem( "artist", criteria[ "artist" ] ); + QNetworkRequest req( url ); + req.setRawHeader( "User-Agent", "TomahawkPlayer/1.0 +http://tomahawk-player.org" ); + QNetworkReply* reply = TomahawkUtils::nam()->get( req ); + + NewClosure( reply, SIGNAL( finished() ), this, SLOT( albumSearchSlot( Tomahawk::InfoSystem::InfoRequestData, QNetworkReply* ) ), requestData, reply ); + break; + } + + default: + { + Q_ASSERT( false ); + break; + } + } +} + + +void +DiscogsPlugin::albumSearchSlot( const InfoRequestData &requestData, QNetworkReply *reply ) +{ + QJson::Parser p; + QVariantMap results = p.parse( reply ).toMap(); + + if ( !results.contains( "results" ) || results.value( "results" ).toList().isEmpty() ) + { + emit info( requestData, QVariant() ); + return; + } + + const QVariantMap result = results.value( "results" ).toList().first().toMap(); + if ( !result.contains( "id" ) ) + { + emit info( requestData, QVariant() ); + return; + } + + const int id = result.value( "id" ).toInt(); + QUrl url( QString( "http://api.discogs.com/release/%1" ).arg( id ) ); + QNetworkRequest req( url ); + req.setRawHeader( "User-Agent", "TomahawkPlayer/1.0 +http://tomahawk-player.org" ); + + QNetworkReply* reply2 = TomahawkUtils::nam()->get( req ); + NewClosure( reply2, SIGNAL( finished() ), this, SLOT( albumInfoSlot( Tomahawk::InfoSystem::InfoRequestData, QNetworkReply* ) ), requestData, reply2 ); +} + + +void +DiscogsPlugin::albumInfoSlot( const InfoRequestData& requestData, QNetworkReply* reply ) +{ + QJson::Parser p; + QVariantMap results = p.parse( reply ).toMap(); + + if ( !results.contains( "resp" ) ) + { + emit info( requestData, QVariant() ); + return; + } + + const QVariantMap resp = results[ "resp" ].toMap(); + if ( !resp.contains( "release" ) ) + { + emit info( requestData, QVariant() ); + return; + } + + const QVariantMap release = resp[ "release" ].toMap(); + if ( !release.contains( "tracklist" ) || release[ "tracklist" ].toList().isEmpty() ) + { + emit info( requestData, QVariant() ); + return; + } + + QStringList trackNameList; + foreach ( const QVariant& v, release[ "tracklist" ].toList() ) + { + const QVariantMap track = v.toMap(); + if ( track.contains( "title" ) ) + trackNameList << track[ "title" ].toString(); + } + + QVariantMap returnedData; + returnedData["tracks"] = trackNameList; + + emit info( requestData, returnedData ); + + Tomahawk::InfoSystem::InfoStringHash criteria; + criteria["artist"] = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>()["artist"]; + criteria["album"] = requestData.input.value< Tomahawk::InfoSystem::InfoStringHash>()["album"]; + + emit updateCache( criteria, 0, requestData.type, returnedData ); +} + + + +Q_EXPORT_PLUGIN2( Tomahawk::InfoSystem::InfoPlugin, Tomahawk::InfoSystem::DiscogsPlugin ) diff --git a/src/infoplugins/generic/discogs/DiscogsPlugin.h b/src/infoplugins/generic/discogs/DiscogsPlugin.h new file mode 100644 index 000000000..654fe7e52 --- /dev/null +++ b/src/infoplugins/generic/discogs/DiscogsPlugin.h @@ -0,0 +1,63 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2012 Leo Franchi + * + * 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 DISCOGS_PLUGIN_H +#define DISCOGS_PLUGIN_H + +#include "Typedefs.h" +#include "infosystem/InfoSystem.h" +#include "infosystem/InfoSystemWorker.h" +#include "infoplugins/InfoPluginDllMacro.h" + +class QNetworkReply; + +namespace Tomahawk +{ + +namespace InfoSystem +{ + +class INFOPLUGINDLLEXPORT DiscogsPlugin : public InfoPlugin +{ + Q_OBJECT + Q_INTERFACES( Tomahawk::InfoSystem::InfoPlugin ) + +public: + DiscogsPlugin(); + virtual ~DiscogsPlugin(); + +protected slots: + virtual void init() {} + virtual void getInfo( Tomahawk::InfoSystem::InfoRequestData requestData ); + virtual void notInCacheSlot( InfoStringHash criteria, InfoRequestData requestData ); + + virtual void pushInfo( Tomahawk::InfoSystem::InfoPushData ) {} +private slots: + void albumSearchSlot( const Tomahawk::InfoSystem::InfoRequestData& , QNetworkReply* ); + void albumInfoSlot( const Tomahawk::InfoSystem::InfoRequestData& , QNetworkReply* ); + +private: + bool isValidTrackData( Tomahawk::InfoSystem::InfoRequestData requestData ); +}; + +} + +} + +Q_DECLARE_METATYPE( QNetworkReply* ) +#endif diff --git a/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp b/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp index 625a527ce..3a52e3e52 100644 --- a/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp +++ b/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.cpp @@ -129,32 +129,6 @@ MusicBrainzPlugin::notInCacheSlot( InfoStringHash criteria, InfoRequestData requ } -bool -MusicBrainzPlugin::isValidTrackData( Tomahawk::InfoSystem::InfoRequestData requestData ) -{ - if ( requestData.input.isNull() || !requestData.input.isValid() || !requestData.input.canConvert< QVariantMap >() ) - { - emit info( requestData, QVariant() ); - qDebug() << Q_FUNC_INFO << "Data null, invalid, or can't convert"; - return false; - } - QVariantMap hash = requestData.input.value< QVariantMap >(); - if ( hash[ "trackName" ].toString().isEmpty() ) - { - emit info( requestData, QVariant() ); - qDebug() << Q_FUNC_INFO << "Track name is empty"; - return false; - } - if ( hash[ "artistName" ].toString().isEmpty() ) - { - emit info( requestData, QVariant() ); - qDebug() << Q_FUNC_INFO << "No artist name found"; - return false; - } - return true; -} - - void MusicBrainzPlugin::artistSearchSlot() { diff --git a/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.h b/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.h index 21043caeb..57ca6bc99 100644 --- a/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.h +++ b/src/infoplugins/generic/musicbrainz/MusicBrainzPlugin.h @@ -59,9 +59,6 @@ private slots: void albumFoundSlot(); void tracksFoundSlot(); - -private: - bool isValidTrackData( Tomahawk::InfoSystem::InfoRequestData requestData ); }; }