1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-04-20 07:52:30 +02:00

add echonest generator beginnings

This commit is contained in:
Leo Franchi 2010-12-04 16:52:26 -05:00
parent c30f62e834
commit 228cab7080
4 changed files with 299 additions and 0 deletions

View File

@ -0,0 +1,86 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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 2 of the License, or (at your option) any later *
* version. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "echonest/echonestcontrol.h"
#include <echonest/Playlist.h>
#include <QComboBox>
#include <QLineEdit>
Tomahawk::EchonestControl::EchonestControl( const QString& type, const QStringList& typeSelectors, QObject* parent )
: DynamicControl ( type, typeSelectors, parent )
{
updateWidgets();
}
QWidget*
Tomahawk::EchonestControl::inputField()
{
return m_input.data();
}
QWidget*
Tomahawk::EchonestControl::matchSelector()
{
return m_match.data();
}
void
Tomahawk::EchonestControl::setSelectedType ( const QString& type )
{
Tomahawk::DynamicControl::setSelectedType ( type );
}
Echonest::DynamicPlaylist::PlaylistParamData
Tomahawk::EchonestControl::toENParam() const
{
return m_data;
}
void
Tomahawk::EchonestControl::updateWidgets()
{
// make sure the widgets are the proper kind for the selected type, and hook up to their slots
if( selectedType() == "Artist" ) {
QComboBox* match = new QComboBox();
QLineEdit* input = new QLineEdit();
match->addItem( "Limit To", Echonest::DynamicPlaylist::ArtistType );
match->addItem( "Similar To", Echonest::DynamicPlaylist::ArtistRadioType );
input->setPlaceholderText( "Artist name" );
connect( match, SIGNAL( currentIndexChanged(int) ), this, SLOT( updateData() ) );
connect( input, SIGNAL( textChanged(QString) ), this, SLOT( updateData() ) );
m_match = QWeakPointer< QWidget >( match );
m_input = QWeakPointer< QWidget >( input );
}
}
void Tomahawk::EchonestControl::updateData()
{
if( selectedType() == "Artist" ) {
QWeakPointer<QComboBox> combo = qWeakPointerCast( m_match );
if( !combo.isNull() )
m_data.first = static_cast<Echonest::DynamicPlaylist::PlaylistParam>( combo.data()->itemData( combo->currentIndex() ) );
QWeakPointer<QLineEdit> edit = qWeakPointerCast( m_input );
if( !edit.isNull() )
m_data.second = qWeakPointerCast->text();
}
}

View File

@ -0,0 +1,59 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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 2 of the License, or (at your option) any later *
* version. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#ifndef ECHONEST_CONTROL_H
#define ECHONEST_CONTROL_H
#include <echonest/Playlist.h>
#include "dynamic/dynamiccontrol.h"
namespace Tomahawk
{
class EchonestControl : public DynamicControl
{
Q_OBJECT
public:
virtual QWidget* inputField();
virtual QWidget* matchSelector();
/// Converts this to an echonest suitable parameter
Echonest::DynamicPlaylist::PlaylistParamData toENParam() const;
public slots:
virtual void setSelectedType ( const QString& type );
protected:
explicit EchonestControl( const QString& type, const QStringList& typeSelectors, QObject* parent = 0 );
private slots:
void updateData();
private:
void updateWidgets();
QWeakPointer< QWidget > m_input;
QWeakPointer< QWidget > m_match;
Echonest::DynamicPlaylist::PlaylistParamData m_data;
};
typedef QSharedPointer<EchonestControl> encontrol_ptr;
};
#endif

View File

@ -0,0 +1,99 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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 2 of the License, or (at your option) any later *
* version. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#include "echonest/echonestgenerator.h"
#include "echonest/echonestcontrol.h"
#include "query.h"
using namespace Tomahawk;
EchonestFactory::GeneratorFactoryInterface()
{}
GeneratorInterface*
EchonestFactory::create()
{
return new EchonestGenerator();
}
EchonestGenerator::EchonestGenerator ( QObject* parent )
: GeneratorInterface ( parent )
, m_type( "echonest" )
, m_mode( OnDemand )
{
m_typeSelectors << "Variety" << "Artist" << "Description" << "Tempo" << "Duration" << "Loudness"
<< "Danceability" << "Energy" << "Artist Familiarity" << "Artist Hotttnesss" << "Song Familiarity"
<< "Longitude" << "Latitude" << "Mode" << "Key" << "Sorting";
}
EchonestGenerator::~EchonestGenerator()
{
}
dyncontrol_ptr
EchonestGenerator::createControl( const QString& type ) const
{
return dyncontrol_ptr( new EchonestControl( type, m_typeSelectors ) );
}
void
EchonestGenerator::generate ( int number )
{
// convert to an echonest query, and fire it off
if( number < 0 ) { // dynamic
} else { // static
Echonest::DynamicPlaylist::PlaylistParams params;
foreach( const dyncontrol_ptr& control, m_controls )
params.append( control.dynamicCast<EchonestControl>()->toENParam() );
QNetworkReply* reply = Echonest::DynamicPlaylist::staticPlaylist( params );
connect( reply, SIGNAL( finished() ), this, SLOT( staticFinished() ) );
}
}
void
EchonestGenerator::staticFinished()
{
Q_ASSERT( sender() );
Q_ASSERT( qobject_cast< QNetworkReply* >( sender() ) );
QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() );
Echonest::SongList songs;
try {
songs = Echonest::DynamicPlaylist::parseStaticPlaylist( reply );
} catch( const Echonest::ParseError& e ) {
qWarning() << "libechonest threw an error trying to parse the static playlist!" << e.errorType() << e.what();
return;
}
QList< query_ptr > queries;
foreach( const Echonest::Song& song, songs ) {
qDebug() << "EchonestGenerator got song:" << song;
QVariantMap track;
track[ "artist" ] = song.artistName();
// track[ "album" ] = song.release(); // TODO should we include it? can be quite specific
track[ "track" ] = song.title();
queries << query_ptr( new Query( track ) );
}
emit generated( queries );
}

View File

@ -0,0 +1,55 @@
/****************************************************************************************
* Copyright (c) 2010 Leo Franchi <lfranchi@kde.org> *
* *
* This program 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 2 of the License, or (at your option) any later *
* version. *
* *
* This program 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 *
* this program. If not, see <http://www.gnu.org/licenses/>. *
****************************************************************************************/
#ifndef ECHONEST_GENERATOR_H
#define ECHONEST_GENERATOR_H
#include <echonest/Playlist.h>
#include "dynamic/generatorinterface.h"
#include "dynamic/generatorfactory.h"
namespace Tomahawk
{
class EchonestFactory : public GeneratorFactoryInterface
{
public:
EchonestFactory();
virtual GeneratorInterface* create();
};
class EchonestGenerator : public GeneratorInterface
{
Q_OBJECT
public:
explicit EchonestGenerator( QObject* parent );
virtual ~EchonestGenerator();
virtual dyncontrol_ptr createControl( const QString& type = QString() ) const;
virtual void generate ( int number = -1 );
private slots:
void staticFinished();
};
};
#endif