1 /****************************************************************************************
2  * Copyright (c) 2008 Jeff Mitchell <mitchell@kde.org>                                  *
3  * Copyright (c) 2007-2008 Leo Franchi <lfranchi@gmail.com>                             *
4  *                                                                                      *
5  * This program is free software; you can redistribute it and/or modify it under        *
6  * the terms of the GNU General Public License as published by the Free Software        *
7  * Foundation; either version 2 of the License, or (at your option) any later           *
8  * version.                                                                             *
9  *                                                                                      *
10  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
12  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
13  *                                                                                      *
14  * You should have received a copy of the GNU General Public License along with         *
15  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
16  ****************************************************************************************/
17 
18 #include "SongkickEngine.h"
19 
20 #include "JsonQt/lib/JsonToVariant.h"
21 #include "JsonQt/lib/ParseException.h"
22 
23 #include "core/support/Amarok.h"
24 #include "core/support/Debug.h"
25 #include "ContextObserver.h"
26 #include "ContextView.h"
27 #include "EngineController.h"
28 
29 #include <KIO/Job>
30 
31 #include <QFile>
32 #include <QLocale>
33 #include <QUrl>
34 
35 using namespace Context;
36 
SongkickEngine(QObject * parent,const QList<QVariant> & args)37 SongkickEngine::SongkickEngine( QObject* parent, const QList<QVariant>& args )
38     : DataEngine( parent )
39     , ContextObserver( ContextView::self() )
40     , m_datesJob( )
41     , m_currentTrack( 0 )
42     , m_ontour( true )
43     , m_dates( true )
44 {
45     Q_UNUSED( args )
46     DEBUG_BLOCK
47 
48     m_sources << I18N_NOOP( "ontour" ) << I18N_NOOP( "dates" );
49 }
50 
sources() const51 QStringList SongkickEngine::sources() const
52 {
53     DEBUG_BLOCK
54     return m_sources;
55 }
56 
sourceRequestEvent(const QString & name)57 bool SongkickEngine::sourceRequestEvent( const QString& name )
58 {
59     DEBUG_BLOCK
60     debug() << "sourceRequested with name " << name;
61 
62     removeAllData( name );
63     setData( name, "fetching" );
64     update();
65     return true;
66 }
67 
message(const ContextState & state)68 void SongkickEngine::message( const ContextState& state )
69 {
70     DEBUG_BLOCK
71     if( state == Current )
72         update();
73 }
74 
metadataChanged(Meta::TrackPtr track)75 void SongkickEngine::metadataChanged( Meta::TrackPtr track )
76 {
77     Q_UNUSED( track )
78     DEBUG_BLOCK
79 
80     update();
81 }
82 
update()83 void SongkickEngine::update()
84 {
85     DEBUG_BLOCK
86 
87     unsubscribeFrom( m_currentTrack );
88     Meta::TrackPtr currentTrack = The::engineController()->currentTrack();
89     m_currentTrack = currentTrack;
90     subscribeTo( currentTrack );
91 
92     if ( !currentTrack )
93     {
94         debug() << "No current track!";
95         return;
96     }
97     else if ( !currentTrack->artist() )
98     {
99         debug() << "No artist found!";
100         return;
101     }
102 
103     QString country = QLocale::system().name().right( 2 ).toLower();
104     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 ) );
105     debug() << "getting ontour status: " << ontourUrl;
106     m_ontourJob = KIO::storedGet( ontourUrl, KIO::NoReload, KIO::HideProgressInfo );
107     connect( m_ontourJob, &KJob::result, this, SLOT(ontourResult(KJob*)) );
108 
109     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 ) );
110     debug() << "getting concert dates: " << datesUrl;
111     m_datesJob = KIO::storedGet( datesUrl, KIO::NoReload, KIO::HideProgressInfo );
112     connect( m_datesJob, &KJob::result, this, SLOT(datesResult(KJob*)) );
113 }
114 
datesResult(KJob * job)115 void SongkickEngine::datesResult( KJob* job )
116 {
117     DEBUG_BLOCK
118     if( job != m_datesJob )
119         return;
120 
121     if( !m_datesJob )
122         return;
123     if( !job->error() == 0 && m_datesJob == job)
124     {
125         setData( "dates", "error" );
126         return;
127     }
128 
129     KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( job );
130     QString data = QString( storedJob->data() );
131 
132     QVariantMap dates;
133     /*QVariant datesResult = JsonQt::JsonToVariant::parse( data );
134 
135     debug() << "got dates: " << dates;
136     QMapIterator< QString, QVariant > iter( dates );
137     while( iter.hasNext() )
138     {
139         iter.next();
140         setData( "dates", iter.key(), iter.value() );
141     }
142     */
143     setData( "dates", data );
144 }
145 
ontourResult(KJob * job)146 void SongkickEngine::ontourResult( KJob* job )
147 {
148     DEBUG_BLOCK
149     if( job != m_ontourJob )
150         return;
151 
152     m_ontour = false;
153     if( !m_ontourJob )
154         return;
155     if( !job->error() == 0 && m_ontourJob == job )
156     {
157         setData( "ontour", "error" );
158         return;
159     }
160 
161     KIO::StoredTransferJob* const storedJob = static_cast<KIO::StoredTransferJob*>( job );
162     QString data = QString( storedJob->data() );
163     QVariantMap status;
164     setData( "ontour", data );
165 }
166 
167