2011-10-26 16:45:20 +02:00
|
|
|
/****************************************************************************************
|
|
|
|
* 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 "Generator_p.h"
|
|
|
|
#include "CatalogUpdateEntry.h"
|
|
|
|
|
|
|
|
// QJSon
|
|
|
|
#include <qjson/serializer.h>
|
|
|
|
|
|
|
|
#include <QVariant>
|
|
|
|
#include <quuid.h>
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
|
|
QByteArray Echonest::Generator::catalogEntriesToJson( const Echonest::CatalogUpdateEntries& items )
|
|
|
|
{
|
|
|
|
QJson::Serializer s;
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
QVariant itms = catalogEntriesToVariant( items );
|
|
|
|
QByteArray serialized = s.serialize( itms );
|
|
|
|
// qDebug() << "Serialized:" << serialized;
|
|
|
|
return serialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
QByteArray Echonest::Generator::catalogEntryToJson( const Echonest::CatalogUpdateEntry& item )
|
|
|
|
{
|
|
|
|
QJson::Serializer s;
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
QVariant itm = catalogEntryToVariant( item );
|
|
|
|
QByteArray serialized = s.serialize( itm );
|
|
|
|
// qDebug() << "Serialized:" << serialized;
|
|
|
|
return serialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QVariantList Echonest::Generator::catalogEntriesToVariant( const Echonest::CatalogUpdateEntries& items )
|
|
|
|
{
|
|
|
|
// copy the catalog item into a QVariant
|
|
|
|
QVariantList itemList;
|
|
|
|
foreach( const Echonest::CatalogUpdateEntry& item, items )
|
|
|
|
itemList << catalogEntryToVariant( item );
|
2011-11-23 16:10:33 +01:00
|
|
|
|
|
|
|
qDebug() << "Generated " << itemList.size() << "entries to catalog variant!";
|
2011-10-26 16:45:20 +02:00
|
|
|
return itemList;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant Echonest::Generator::catalogEntryToVariant( const Echonest::CatalogUpdateEntry& item )
|
|
|
|
{
|
|
|
|
QVariantMap itemMap;
|
|
|
|
QVariantMap itm;
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
itemMap[ QLatin1String( "action" ) ] = Echonest::catalogUpdateActionToLiteral( item.action() );
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.itemId().isEmpty() )
|
|
|
|
itm[ QLatin1String( "item_id" ) ] = QUuid::createUuid().toString().replace( QLatin1Char( '{' ), QString() ).replace( QLatin1Char( '}' ), QString() );
|
|
|
|
else
|
|
|
|
itm[ QLatin1String( "item_id" ) ] = item.itemId();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.fingerprint().isEmpty() )
|
|
|
|
itm[ QLatin1String( "fp_code" ) ] = item.fingerprint();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.songId().isEmpty() )
|
|
|
|
itm[ QLatin1String( "song_id" ) ] = item.songId();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.songName().isEmpty() )
|
|
|
|
itm[ QLatin1String( "song_name" ) ] = item.songName();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.artistId().isEmpty() )
|
|
|
|
itm[ QLatin1String( "artist_id" ) ] = item.artistId();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.artistName().isEmpty() )
|
|
|
|
itm[ QLatin1String( "artist_name" ) ] = item.artistName();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.release().isEmpty() )
|
|
|
|
itm[ QLatin1String( "release" ) ] = item.release();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.genre().isEmpty() )
|
|
|
|
itm[ QLatin1String( "genre" ) ] = item.genre();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.trackNumber() > -1 )
|
|
|
|
itm[ QLatin1String( "track_number" ) ] = item.trackNumber();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.discNumber() > -1 )
|
|
|
|
itm[ QLatin1String( "disc_number" ) ] = item.discNumber();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( !item.url().isEmpty() )
|
|
|
|
itm[ QLatin1String( "url" ) ] = item.url();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.favoriteSet() )
|
|
|
|
itm[ QLatin1String( "favorite" ) ] = item.favorite();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.bannedSet() )
|
|
|
|
itm[ QLatin1String( "banned" ) ] = item.banned();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.playCount() > -1 )
|
|
|
|
itm[ QLatin1String( "play_count" ) ] = item.playCount();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.skipCount() > -1 )
|
|
|
|
itm[ QLatin1String( "skip_count" ) ] = item.skipCount();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
if( item.rating() > -1 )
|
|
|
|
itm[ QLatin1String( "rating" ) ] = item.rating();
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
itemMap[ QLatin1String( "item" ) ] = itm;
|
2011-11-23 16:10:33 +01:00
|
|
|
|
2011-10-26 16:45:20 +02:00
|
|
|
return itemMap;
|
|
|
|
}
|