1 /****************************************************************************************
2 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org> *
3 * Copyright (c) 2008 Casey Link <unnamedrambler@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 "Mp3tunesServiceCollection.h"
19
20 #include "Mp3tunesLockerMeta.h"
21 #include "Mp3tunesMeta.h"
22 #include "Mp3tunesServiceCollectionLocation.h"
23 #include "Mp3tunesServiceQueryMaker.h"
24 #include "Mp3tunesWorkers.h"
25 #include "core/support/Debug.h"
26
27 #include <ThreadWeaver/ThreadWeaver>
28 #include <ThreadWeaver/Queue>
29 #include <ThreadWeaver/Job>
30
31 #include <QRegExp>
32
33 using namespace Collections;
34
Mp3tunesServiceCollection(ServiceBase * service,const QString & sessionId,Mp3tunesLocker * locker)35 Mp3tunesServiceCollection::Mp3tunesServiceCollection( ServiceBase * service, const QString
36 &sessionId, Mp3tunesLocker * locker )
37 : ServiceCollection( service, "Mp3tunesCollection", "Mp3tunesCollection" )
38 , m_sessionId( sessionId )
39 , m_locker( locker )
40 {
41 }
42
43
~Mp3tunesServiceCollection()44 Mp3tunesServiceCollection::~Mp3tunesServiceCollection()
45 {
46 }
47
queryMaker()48 QueryMaker * Mp3tunesServiceCollection::queryMaker()
49 {
50 return new Mp3tunesServiceQueryMaker( m_locker, m_sessionId, this );
51 }
52
collectionId() const53 QString Mp3tunesServiceCollection::collectionId() const
54 {
55 return QLatin1String( "MP3tunesLocker" );
56 }
57
prettyName() const58 QString Mp3tunesServiceCollection::prettyName() const
59 {
60 return i18n( "MP3tunes Locker" );
61 }
62
63 bool
possiblyContainsTrack(const QUrl & url) const64 Mp3tunesServiceCollection::possiblyContainsTrack(const QUrl &url) const
65 {
66 QRegExp rx( "http://content.mp3tunes.com/storage/locker(?:get|play)/(.*)\\?(?:sid|partner_token)=.*" ) ;
67 int matches = rx.indexIn( url.url() );
68 if( matches == -1 ) {
69 return false; // not a mp3tunes url
70 }
71 return true; // for now: if it's a mp3tunes url.. it's likely the track is in the locker
72 }
73
74 Meta::TrackPtr
trackForUrl(const QUrl & url)75 Mp3tunesServiceCollection::trackForUrl( const QUrl &url )
76 {
77 DEBUG_BLOCK
78 if( !m_locker->authenticated() )
79 m_locker->login();
80 QRegExp rx( "http://content.mp3tunes.com/storage/locker(?:get|play)/(.*)\\?(?:sid|partner_token)=.*" ) ;
81 rx.indexIn( url.url() );
82 QStringList list = rx.capturedTexts();
83 QString filekey = list[1]; // Because list[0] is the url itself.
84 if ( filekey.isEmpty() ) {
85 debug() << "not a track";
86 return Meta::TrackPtr(); // It's not an mp3tunes track
87 }
88 debug() << "filekey: " << filekey;
89
90 Meta::Mp3TunesTrack * serviceTrack = new Meta::Mp3TunesTrack( QString() );
91 serviceTrack->setUidUrl( url.url() );
92
93 Mp3tunesTrackFromFileKeyFetcher* trackFetcher = new Mp3tunesTrackFromFileKeyFetcher( m_locker, filekey );
94 m_tracksFetching[filekey] = serviceTrack;
95 connect( trackFetcher, &Mp3tunesTrackFromFileKeyFetcher::trackFetched, this, &Mp3tunesServiceCollection::trackForUrlComplete );
96 //debug() << "Connection complete. Enqueueing..";
97 ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(trackFetcher) );
98 //debug() << "m_trackFetcher queue";
99
100 return Meta::TrackPtr( serviceTrack );
101 }
102
trackForUrlComplete(Mp3tunesLockerTrack & track)103 void Mp3tunesServiceCollection::trackForUrlComplete( Mp3tunesLockerTrack &track )
104 {
105 DEBUG_BLOCK
106 //Lets get this thing
107 debug() << "got track: " << track.trackTitle();
108 QString filekey = track.trackFileKey();
109 if( !m_tracksFetching.contains( filekey ) ) {
110 debug() << "track not found in QMap";
111 return;
112 }
113 Meta::Mp3TunesTrack * serviceTrack = m_tracksFetching.take( filekey );
114
115 //Building a Meta::Track
116 QString title = track.trackTitle().isEmpty() ? "Unknown" : track.trackTitle();
117 serviceTrack->setTitle( title );
118 serviceTrack->setId( track.trackId() );
119 serviceTrack->setUidUrl( track.playUrl() ); //was: setUrl
120 serviceTrack->setDownloadableUrl( track.downloadUrl() );
121 serviceTrack->setLength( track.trackLength() );
122 serviceTrack->setTrackNumber( track.trackNumber() );
123 serviceTrack->setYear( track.albumYear() );
124
125 //Building a Meta::Album
126 title = track.albumTitle().isEmpty() ? "Unknown" : track.albumTitle();
127 Meta::Mp3TunesAlbum * serviceAlbum = new Meta::Mp3TunesAlbum( title );
128 QString albumIdStr = QString::number( track.albumId() );
129 serviceAlbum->setId( track.albumId() );
130
131 QString coverUrl = "http://content.mp3tunes.com/storage/albumartget/<ALBUM_ID>?alternative=1&partner_token=<PARTNER_TOKEN>&sid=<SESSION_ID>";
132
133 coverUrl.replace( "<SESSION_ID>", m_locker->sessionId() );
134 coverUrl.replace( "<PARTNER_TOKEN>", m_locker->partnerToken() );
135 coverUrl.replace( "<ALBUM_ID>", albumIdStr );
136
137 serviceAlbum->setCoverUrl(coverUrl);
138 Meta::AlbumPtr albumPtr( serviceAlbum );
139 serviceTrack->setAlbumPtr( albumPtr );
140
141 // Building a Meta::Artist
142 QString name = track.artistName().isEmpty() ? "Unknown" : track.artistName();
143 Meta::ServiceArtist * serviceArtist = new Meta::ServiceArtist( name );
144 serviceArtist->setId( track.artistId() );
145 Meta::ArtistPtr artistPtr( serviceArtist );
146
147 serviceTrack->setArtist( artistPtr );
148 serviceAlbum->setArtistName( name );
149 serviceAlbum->setAlbumArtist( artistPtr );
150 // serviceTrack->update( Meta::TrackPtr( serviceTrack ) );
151 }
152
locker() const153 Mp3tunesLocker* Mp3tunesServiceCollection::locker() const
154 {
155 return m_locker;
156 }
157
158 CollectionLocation*
location()159 Mp3tunesServiceCollection::location()
160 {
161 return new Mp3tunesServiceCollectionLocation( this );
162 }
163
164
165
166