Skip to content

Commit

Permalink
Adapt to new libechonest v2 api
Browse files Browse the repository at this point in the history
  • Loading branch information
lfranchi committed Jul 25, 2012
1 parent 611ca21 commit 44a5406
Showing 1 changed file with 35 additions and 15 deletions.
50 changes: 35 additions & 15 deletions src/libtomahawk/playlist/dynamic/echonest/EchonestGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,13 @@ EchonestGenerator::EchonestGenerator ( QObject* parent )

EchonestGenerator::~EchonestGenerator()
{
if ( !m_dynPlaylist->sessionId().isNull() )
{
// Running session, delete it
QNetworkReply* deleteReply = m_dynPlaylist->deleteSession();
connect( deleteReply, SIGNAL( finished() ), deleteReply, SLOT( deleteLater() ) );
}

delete m_dynPlaylist;
}

Expand Down Expand Up @@ -194,6 +201,13 @@ EchonestGenerator::generate( int number )
void
EchonestGenerator::startOnDemand()
{
if ( !m_dynPlaylist->sessionId().isNull() )
{
// Running session, delete it
QNetworkReply* deleteReply = m_dynPlaylist->deleteSession();
connect( deleteReply, SIGNAL( finished() ), deleteReply, SLOT( deleteLater() ) );
}

connect( this, SIGNAL( paramsGenerated( Echonest::DynamicPlaylist::PlaylistParams ) ), this, SLOT( doStartOnDemand( Echonest::DynamicPlaylist::PlaylistParams ) ) );
try {
getParams();
Expand Down Expand Up @@ -225,7 +239,7 @@ EchonestGenerator::doStartOnDemand( const Echonest::DynamicPlaylist::PlaylistPar
{
disconnect( this, SIGNAL( paramsGenerated( Echonest::DynamicPlaylist::PlaylistParams ) ), this, SLOT( doStartOnDemand( Echonest::DynamicPlaylist::PlaylistParams ) ) );

QNetworkReply* reply = m_dynPlaylist->start( params );
QNetworkReply* reply = m_dynPlaylist->create( params );
qDebug() << "starting a dynamic playlist from echonest!" << reply->url().toString();
connect( reply, SIGNAL( finished() ), this, SLOT( dynamicStarted() ) );
}
Expand All @@ -240,14 +254,15 @@ EchonestGenerator::fetchNext( int rating )
return;
}

QNetworkReply* reply;
if( m_steeredSinceLastTrack ) {
qDebug() << "Steering dynamic playlist!" << m_steerData.first << m_steerData.second;
reply = m_dynPlaylist->fetchNextSong( Echonest::DynamicPlaylist::DynamicControls() << m_steerData );
m_steeredSinceLastTrack = false;
} else {
reply = m_dynPlaylist->fetchNextSong( rating );
if ( rating > -1 )
{
Echonest::DynamicPlaylist::DynamicFeedback feedback;
feedback.append( Echonest::DynamicPlaylist::DynamicFeedbackParamData( Echonest::DynamicPlaylist::RateSong, QString( "last^%1").arg( rating * 2 ).toUtf8() ) );
QNetworkReply* reply = m_dynPlaylist->feedback( feedback );
connect( reply, SIGNAL( finished() ), reply, SLOT( deleteLater() ) ); // we don't care about the result, just send it off
}

QNetworkReply* reply = m_dynPlaylist->next( 1, 0 );
qDebug() << "getting next song from echonest" << reply->url().toString();
connect( reply, SIGNAL( finished() ), this, SLOT( dynamicFetched() ) );
}
Expand Down Expand Up @@ -369,9 +384,8 @@ EchonestGenerator::dynamicStarted()

try
{
Echonest::Song song = m_dynPlaylist->parseStart( reply );
query_ptr songQuery = queryFromSong( song );
emit nextTrackGenerated( songQuery );
m_dynPlaylist->parseCreate( reply );
fetchNext();
} catch( const Echonest::ParseError& e ) {
qWarning() << "libechonest threw an error parsing the start of the dynamic playlist:" << e.errorType() << e.what();
emit error( "The Echo Nest returned an error starting the station", e.what() );
Expand All @@ -386,12 +400,18 @@ EchonestGenerator::dynamicFetched()
Q_ASSERT( qobject_cast< QNetworkReply* >( sender() ) );
QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() );

resetSteering();

try
{
Echonest::Song song = m_dynPlaylist->parseNextSong( reply );
query_ptr songQuery = queryFromSong( song );
Echonest::DynamicPlaylist::FetchPair fetched = m_dynPlaylist->parseNext( reply );

if ( fetched.first.size() != 1 )
{
qWarning() << "Did not get any track when looking up the next song from the echo nest!";
emit error( "No more songs from The Echo Nest available in the station", "" );
return;
}

query_ptr songQuery = queryFromSong( fetched.first.first() );
emit nextTrackGenerated( songQuery );
} catch( const Echonest::ParseError& e ) {
qWarning() << "libechonest threw an error parsing the next song of the dynamic playlist:" << e.errorType() << e.what();
Expand Down

0 comments on commit 44a5406

Please sign in to comment.