File indexing completed on 2025-02-23 04:27:31
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Jeff Mitchell <mitchell@kde.org> * 0003 * Copyright (c) 2007-2008 Leo Franchi <lfranchi@gmail.com> * 0004 * * 0005 * This program is free software; you can redistribute it and/or modify it under * 0006 * the terms of the GNU General Public License as published by the Free Software * 0007 * Foundation; either version 2 of the License, or (at your option) any later * 0008 * version. * 0009 * * 0010 * This program is distributed in the hope that it will be useful, but WITHOUT ANY * 0011 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * 0012 * PARTICULAR PURPOSE. See the GNU General Public License for more details. * 0013 * * 0014 * You should have received a copy of the GNU General Public License along with * 0015 * this program. If not, see <http://www.gnu.org/licenses/>. * 0016 ****************************************************************************************/ 0017 0018 #include "SongkickEngine.h" 0019 0020 #include "JsonQt/lib/JsonToVariant.h" 0021 #include "JsonQt/lib/ParseException.h" 0022 0023 #include "core/support/Amarok.h" 0024 #include "core/support/Debug.h" 0025 #include "ContextObserver.h" 0026 #include "ContextView.h" 0027 #include "EngineController.h" 0028 0029 #include <KIO/Job> 0030 0031 #include <QFile> 0032 #include <QLocale> 0033 #include <QUrl> 0034 0035 using namespace Context; 0036 0037 SongkickEngine::SongkickEngine( QObject* parent, const QList<QVariant>& args ) 0038 : DataEngine( parent ) 0039 , ContextObserver( ContextView::self() ) 0040 , m_datesJob( ) 0041 , m_currentTrack( 0 ) 0042 , m_ontour( true ) 0043 , m_dates( true ) 0044 { 0045 Q_UNUSED( args ) 0046 DEBUG_BLOCK 0047 0048 m_sources << I18N_NOOP( "ontour" ) << I18N_NOOP( "dates" ); 0049 } 0050 0051 QStringList SongkickEngine::sources() const 0052 { 0053 DEBUG_BLOCK 0054 return m_sources; 0055 } 0056 0057 bool SongkickEngine::sourceRequestEvent( const QString& name ) 0058 { 0059 DEBUG_BLOCK 0060 debug() << "sourceRequested with name " << name; 0061 0062 removeAllData( name ); 0063 setData( name, "fetching" ); 0064 update(); 0065 return true; 0066 } 0067 0068 void SongkickEngine::message( const ContextState& state ) 0069 { 0070 DEBUG_BLOCK 0071 if( state == Current ) 0072 update(); 0073 } 0074 0075 void SongkickEngine::metadataChanged( Meta::TrackPtr track ) 0076 { 0077 Q_UNUSED( track ) 0078 DEBUG_BLOCK 0079 0080 update(); 0081 } 0082 0083 void SongkickEngine::update() 0084 { 0085 DEBUG_BLOCK 0086 0087 unsubscribeFrom( m_currentTrack ); 0088 Meta::TrackPtr currentTrack = The::engineController()->currentTrack(); 0089 m_currentTrack = currentTrack; 0090 subscribeTo( currentTrack ); 0091 0092 if ( !currentTrack ) 0093 { 0094 debug() << "No current track!"; 0095 return; 0096 } 0097 else if ( !currentTrack->artist() ) 0098 { 0099 debug() << "No artist found!"; 0100 return; 0101 } 0102 0103 QString country = QLocale::system().name().right( 2 ).toLower(); 0104 QUrl ontourUrl( QString( "http://api.songkick.com/api/V2/get_tour_status?key=kJcAUmzi8AoAngzh&id=0&country=%2&range=all&name=%1" ).arg( QUrl::toPercentEncoding( currentTrack->artist()->prettyName() ), country ) ); 0105 debug() << "getting ontour status: " << ontourUrl; 0106 m_ontourJob = KIO::storedGet( ontourUrl, KIO::NoReload, KIO::HideProgressInfo ); 0107 connect( m_ontourJob, &KJob::result, this, SLOT(ontourResult(KJob*)) ); 0108 0109 QUrl datesUrl( QString( "http://api.songkick.com/api/V2/get_dates_extended?key=kJcAUmzi8AoAngzh&id=0&country=%2&range=all&name=%1" ).arg( QUrl::toPercentEncoding( currentTrack->artist()->prettyName() ), country ) ); 0110 debug() << "getting concert dates: " << datesUrl; 0111 m_datesJob = KIO::storedGet( datesUrl, KIO::NoReload, KIO::HideProgressInfo ); 0112 connect( m_datesJob, &KJob::result, this, SLOT(datesResult(KJob*)) ); 0113 } 0114 0115 void SongkickEngine::datesResult( KJob* job ) 0116 { 0117 DEBUG_BLOCK 0118 if( job != m_datesJob ) 0119 return; 0120 0121 if( !m_datesJob ) 0122 return; 0123 if( !job->error() == 0 && m_datesJob == job) 0124 { 0125 setData( "dates", "error" ); 0126 return; 0127 } 0128 0129 KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( job ); 0130 QString data = QString( storedJob->data() ); 0131 0132 QVariantMap dates; 0133 /*QVariant datesResult = JsonQt::JsonToVariant::parse( data ); 0134 0135 debug() << "got dates: " << dates; 0136 QMapIterator< QString, QVariant > iter( dates ); 0137 while( iter.hasNext() ) 0138 { 0139 iter.next(); 0140 setData( "dates", iter.key(), iter.value() ); 0141 } 0142 */ 0143 setData( "dates", data ); 0144 } 0145 0146 void SongkickEngine::ontourResult( KJob* job ) 0147 { 0148 DEBUG_BLOCK 0149 if( job != m_ontourJob ) 0150 return; 0151 0152 m_ontour = false; 0153 if( !m_ontourJob ) 0154 return; 0155 if( !job->error() == 0 && m_ontourJob == job ) 0156 { 0157 setData( "ontour", "error" ); 0158 return; 0159 } 0160 0161 KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( job ); 0162 QString data = QString( storedJob->data() ); 0163 QVariantMap status; 0164 setData( "ontour", data ); 0165 } 0166