1 /****************************************************************************************
2  * Copyright (c) 2010-2012 Leo Franchi <lfranchi@kde.org>                               *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #include "Track.h"
18 
19 #include "Track_p.h"
20 #include "Config.h"
21 #include "Song.h"
22 #include "Parsing_p.h"
23 
24 #include <QNetworkReply>
25 #include <QFileInfo>
26 
Track()27 Echonest::Track::Track()
28   :  d( new TrackData )
29 {
30 }
31 
Track(const Echonest::Track & other)32 Echonest::Track::Track(const Echonest::Track& other)
33     : d( other.d )
34 {}
35 
Track(const QByteArray & id)36 Echonest::Track::Track(const QByteArray& id)
37 :  d( new TrackData )
38 {
39     d->id = id;
40 }
41 
42 
~Track()43 Echonest::Track::~Track()
44 {
45 }
46 
47 
operator =(const Echonest::Track & track)48 Echonest::Track& Echonest::Track::operator=(const Echonest::Track& track)
49 {
50     d = track.d;
51     return *this;
52 }
53 
54 
artist() const55 QString Echonest::Track::artist() const
56 {
57   return d->artist;
58 }
59 
setArtist(const QString & artist)60 void Echonest::Track::setArtist(const QString& artist)
61 {
62     d->artist = artist;
63 }
64 
65 
title() const66 QString Echonest::Track::title() const
67 {
68   return d->title;
69 }
70 
setTitle(const QString & title)71 void Echonest::Track::setTitle(const QString& title)
72 {
73     d->title = title;
74 }
75 
id() const76 QByteArray Echonest::Track::id() const
77 {
78     return d->id;
79 }
80 
setId(const QByteArray & id)81 void Echonest::Track::setId(const QByteArray& id)
82 {
83     d->id = id;
84 }
85 
md5() const86 QByteArray Echonest::Track::md5() const
87 {
88     return d->md5;
89 }
90 
setMD5(const QByteArray & md5)91 void Echonest::Track::setMD5(const QByteArray& md5)
92 {
93     d->md5 = md5;
94 }
95 
96 
release() const97 QString Echonest::Track::release() const
98 {
99     return d->release;
100 }
101 
setRelease(const QString & release)102 void Echonest::Track::setRelease(const QString& release)
103 {
104     d->release = release;
105 }
106 
analyzerVersion() const107 QString Echonest::Track::analyzerVersion() const
108 {
109     return d->analyzer_version;
110 }
111 
setAnalyzerVersion(const QString & analyzerVersion)112 void Echonest::Track::setAnalyzerVersion(const QString& analyzerVersion)
113 {
114     d->analyzer_version = analyzerVersion;
115 }
116 
bitrate() const117 int Echonest::Track::bitrate() const
118 {
119     return d->bitrate;
120 }
121 
setBitrate(int bitrate)122 void Echonest::Track::setBitrate(int bitrate)
123 {
124     d->bitrate = bitrate;
125 }
126 
samplerate() const127 int Echonest::Track::samplerate() const
128 {
129     return d->samplerate;
130 }
131 
setSamplerate(int samplerate)132 void Echonest::Track::setSamplerate(int samplerate)
133 {
134     d->samplerate = samplerate;
135 }
136 
audioMD5() const137 QByteArray Echonest::Track::audioMD5() const
138 {
139     return d->audio_md5;
140 }
141 
setAudioMD5(const QByteArray & md5)142 void Echonest::Track::setAudioMD5(const QByteArray& md5)
143 {
144     d->audio_md5 = md5;
145 }
146 
status() const147 Echonest::Analysis::AnalysisStatus Echonest::Track::status() const
148 {
149     return Echonest::statusToEnum( d->status );
150 }
151 
setStatus(Echonest::Analysis::AnalysisStatus status)152 void Echonest::Track::setStatus( Echonest::Analysis::AnalysisStatus status )
153 {
154     d->status = Echonest::statusToString( status );
155 }
156 
audioSummary() const157 Echonest::AudioSummary Echonest::Track::audioSummary() const
158 {
159     return d->audio_summary;
160 }
161 
setAudioSummary(const Echonest::AudioSummary & summary)162 void Echonest::Track::setAudioSummary( const Echonest::AudioSummary& summary )
163 {
164     d->audio_summary = summary;
165 }
166 
catalog() const167 QString Echonest::Track::catalog() const
168 {
169     return d->catalog;
170 }
171 
setCatalog(const QString & catalog)172 void Echonest::Track::setCatalog(const QString& catalog)
173 {
174     d->catalog = catalog;
175 }
176 
foreignId() const177 QByteArray Echonest::Track::foreignId() const
178 {
179     return d->foreign_id;
180 }
181 
setForeignId(const QByteArray & id)182 void Echonest::Track::setForeignId(const QByteArray& id)
183 {
184     d->foreign_id = id;
185 }
186 
previewUrl() const187 QUrl Echonest::Track::previewUrl() const
188 {
189     return d->preview_url;
190 }
191 
setPreviewUrl(const QUrl & preview)192 void Echonest::Track::setPreviewUrl(const QUrl& preview)
193 {
194     d->preview_url = preview;
195 }
196 
releaseImage() const197 QUrl Echonest::Track::releaseImage() const
198 {
199     return d->release_image;
200 }
201 
setReleaseImage(const QUrl & imgUrl)202 void Echonest::Track::setReleaseImage(const QUrl& imgUrl)
203 {
204     d->release_image = imgUrl;
205 }
206 
song() const207 Echonest::Song Echonest::Track::song() const
208 {
209     return d->song;
210 }
211 
setSong(const Echonest::Song & song)212 void Echonest::Track::setSong(const Echonest::Song& song)
213 {
214     d->song = song;
215 }
216 
profileFromTrackId(const QByteArray & id)217 QNetworkReply* Echonest::Track::profileFromTrackId( const QByteArray& id )
218 {
219     QUrl url = Echonest::baseGetQuery( "track", "profile" );
220 
221     urlAddQueryItem( url, QLatin1String( "id" ), QString::fromLatin1( id ) );
222     urlAddQueryItem( url, QLatin1String("bucket"), QLatin1String( "audio_summary" ) );
223 
224     qDebug() << "Creating profileFromTrackId URL" << url;
225     return Echonest::Config::instance()->nam()->get( QNetworkRequest( url ) );
226 }
227 
profileFromMD5(const QByteArray & md5)228 QNetworkReply* Echonest::Track::profileFromMD5( const QByteArray& md5 )
229 {
230     QUrl url = Echonest::baseGetQuery( "track", "profile" );
231 
232     urlAddQueryItem( url, QLatin1String( "md5" ), QString::fromLatin1( md5 ) );
233     urlAddQueryItem( url, QLatin1String( "bucket" ), QLatin1String( "audio_summary" ) );
234 
235     qDebug() << "Creating profileFromMD5 URL" << url;
236     return Echonest::Config::instance()->nam()->get( QNetworkRequest( url ) );
237 }
238 
uploadLocalFile(const QUrl & localFile,const QByteArray & data,bool waitForResult)239 QNetworkReply* Echonest::Track::uploadLocalFile( const QUrl& localFile, const QByteArray& data, bool waitForResult )
240 {
241     QUrl url = Echonest::baseGetQuery( "track", "upload" );
242     QFileInfo info( localFile.path() );
243 
244     urlAddQueryItem( url, QLatin1String( "filetype" ), info.suffix() );
245     urlAddQueryItem( url, QLatin1String( "bucket" ), QLatin1String( "audio_summary" ) );
246     urlAddQueryItem( url, QLatin1String( "wait" ), QLatin1String( waitForResult ? "true" : "false" ) );
247 
248     QNetworkRequest request( url );
249 
250     request.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1String( "application/octet-stream" ) );
251 //     qDebug() << "Uploading local file to" << url;
252     return Echonest::Config::instance()->nam()->post( request, data );
253 }
254 
uploadURL(const QUrl & remoteURL,bool waitForResult)255 QNetworkReply* Echonest::Track::uploadURL( const QUrl& remoteURL, bool waitForResult )
256 {
257     QUrl url = Echonest::baseGetQuery( "track", "upload" );
258 
259     urlAddQueryItem( url, QLatin1String( "url" ), remoteURL.toString() );
260     urlAddQueryItem( url, QLatin1String( "bucket" ), QLatin1String( "audio_summary" ) );
261     urlAddQueryItem( url, QLatin1String( "wait" ), QLatin1String( waitForResult ? "true" : "false" ) );
262 
263     qDebug() << "Uploading URL:" << url;
264     QNetworkRequest req( url );
265     req.setHeader( QNetworkRequest::ContentTypeHeader, QLatin1String( "application/x-www-form-urlencoded" ) );
266     return Echonest::Config::instance()->nam()->post( req, QByteArray() );
267 }
268 
analyzeTrackId(const QByteArray & id,bool wait)269 QNetworkReply* Echonest::Track::analyzeTrackId( const QByteArray& id, bool wait )
270 {
271     QUrl url = Echonest::baseGetQuery( "track", "analyze" );
272 
273     urlAddQueryItem( url, QLatin1String( "id" ), QString::fromLatin1( id ) );
274     urlAddQueryItem( url, QLatin1String( "bucket" ), QLatin1String( "audio_summary" ) );
275     urlAddQueryItem( url, QLatin1String( "wait" ), QLatin1String( wait ? "true" : "false" ) );
276 
277     qDebug() << "Creating analyzeTrackId URL" << url;
278     return Echonest::doPost( url );
279 //     return Echonest::Config::instance()->nam()->post( QNetworkRequest( url ), QByteArray() );
280 }
281 
analyzeTrackMD5(const QByteArray & md5,bool wait)282 QNetworkReply* Echonest::Track::analyzeTrackMD5( const QByteArray& md5, bool wait )
283 {
284     QUrl url = Echonest::baseGetQuery( "track", "analyze" );
285 
286     urlAddQueryItem( url, QLatin1String( "id" ), QString::fromLatin1( md5 ) );
287     urlAddQueryItem( url, QLatin1String( "bucket" ), QLatin1String( "audio_summary" ) );
288     urlAddQueryItem( url, QLatin1String( "wait" ), QLatin1String( wait ? "true" : "false" ) );
289 
290     qDebug() << "Creating analyzeTrackMD5 URL" << url;
291     return Echonest::doPost( url );
292 //     return Echonest::Config::instance()->nam()->post( QNetworkRequest( url ), QByteArray() );
293 }
294 
parseProfile(QNetworkReply * finishedReply)295 Echonest::Track Echonest::Track::parseProfile( QNetworkReply* finishedReply ) throw( Echonest::ParseError )
296 {
297     QByteArray data = finishedReply->readAll();
298     qDebug() << data;
299     Echonest::Parser::checkForErrors( finishedReply );
300 
301     QXmlStreamReader xml( data );
302 
303     Echonest::Parser::readStatus( xml );
304     Echonest::Track track = Echonest::Parser::parseTrack( xml );
305 
306     finishedReply->deleteLater();
307     return track;
308 }
309 
310 
311 
operator <<(QDebug d,const Echonest::Track & track)312 QDebug Echonest::operator<<(QDebug d, const Echonest::Track& track)
313 {
314     d << QString::fromLatin1( "Track(%1, %2, %3, %4)" ).arg( QLatin1String( track.id() ) ).arg( track.title() ).arg( track.artist() ).arg( track.release() );
315     return d.maybeSpace();
316 }
317 
318