File indexing completed on 2024-05-19 04:50:22

0001 /****************************************************************************************
0002  * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@kde.org>                                *
0003  * Copyright (c) 2008 Casey Link <unnamedrambler@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 "Mp3tunesServiceCollection.h"
0019 
0020 #include "Mp3tunesLockerMeta.h"
0021 #include "Mp3tunesMeta.h"
0022 #include "Mp3tunesServiceCollectionLocation.h"
0023 #include "Mp3tunesServiceQueryMaker.h"
0024 #include "Mp3tunesWorkers.h"
0025 #include "core/support/Debug.h"
0026 
0027 #include <ThreadWeaver/ThreadWeaver>
0028 #include <ThreadWeaver/Queue>
0029 #include <ThreadWeaver/Job>
0030 
0031 #include <QRegExp>
0032 
0033 using namespace Collections;
0034 
0035 Mp3tunesServiceCollection::Mp3tunesServiceCollection( ServiceBase * service, const QString
0036 &sessionId, Mp3tunesLocker * locker )
0037  : ServiceCollection( service, "Mp3tunesCollection", "Mp3tunesCollection" )
0038  , m_sessionId( sessionId )
0039  , m_locker( locker )
0040 {
0041 }
0042 
0043 
0044 Mp3tunesServiceCollection::~Mp3tunesServiceCollection()
0045 {
0046 }
0047 
0048 QueryMaker * Mp3tunesServiceCollection::queryMaker()
0049 {
0050     return new Mp3tunesServiceQueryMaker( m_locker,  m_sessionId, this );
0051 }
0052 
0053 QString Mp3tunesServiceCollection::collectionId() const
0054 {
0055     return QLatin1String( "MP3tunesLocker" );
0056 }
0057 
0058 QString Mp3tunesServiceCollection::prettyName() const
0059 {
0060     return i18n( "MP3tunes Locker" );
0061 }
0062 
0063 bool
0064 Mp3tunesServiceCollection::possiblyContainsTrack(const QUrl &url) const
0065 {
0066     QRegExp rx( "http://content.mp3tunes.com/storage/locker(?:get|play)/(.*)\\?(?:sid|partner_token)=.*" ) ;
0067     int matches = rx.indexIn( url.url() );
0068     if( matches == -1 ) {
0069         return false; // not a mp3tunes url
0070     }
0071     return true; // for now: if it's a mp3tunes url.. it's likely the track is in the locker
0072 }
0073 
0074 Meta::TrackPtr
0075 Mp3tunesServiceCollection::trackForUrl( const QUrl &url )
0076 {
0077     DEBUG_BLOCK
0078     if( !m_locker->authenticated() )
0079         m_locker->login();
0080     QRegExp rx( "http://content.mp3tunes.com/storage/locker(?:get|play)/(.*)\\?(?:sid|partner_token)=.*" ) ;
0081     rx.indexIn( url.url() );
0082     QStringList list = rx.capturedTexts();
0083     QString filekey = list[1]; // Because list[0] is the url itself.
0084     if ( filekey.isEmpty() ) {
0085         debug() << "not a track";
0086         return Meta::TrackPtr(); // It's not an mp3tunes track
0087     }
0088     debug() << "filekey: " << filekey;
0089 
0090     Meta::Mp3TunesTrack * serviceTrack = new Meta::Mp3TunesTrack( QString() );
0091     serviceTrack->setUidUrl( url.url() );
0092 
0093     Mp3tunesTrackFromFileKeyFetcher* trackFetcher = new Mp3tunesTrackFromFileKeyFetcher( m_locker, filekey );
0094     m_tracksFetching[filekey] = serviceTrack;
0095     connect( trackFetcher, &Mp3tunesTrackFromFileKeyFetcher::trackFetched, this, &Mp3tunesServiceCollection::trackForUrlComplete );
0096     //debug() << "Connection complete. Enqueueing..";
0097     ThreadWeaver::Queue::instance()->enqueue( QSharedPointer<ThreadWeaver::Job>(trackFetcher) );
0098     //debug() << "m_trackFetcher queue";
0099 
0100     return Meta::TrackPtr( serviceTrack );
0101 }
0102 
0103 void Mp3tunesServiceCollection::trackForUrlComplete( Mp3tunesLockerTrack &track )
0104 {
0105     DEBUG_BLOCK
0106     //Lets get this thing
0107     debug() << "got track: " << track.trackTitle();
0108     QString filekey = track.trackFileKey();
0109     if( !m_tracksFetching.contains( filekey ) ) {
0110         debug() << "track not found in QMap";
0111         return;
0112     }
0113     Meta::Mp3TunesTrack * serviceTrack = m_tracksFetching.take( filekey );
0114 
0115     //Building a Meta::Track
0116     QString title = track.trackTitle().isEmpty() ? "Unknown" :  track.trackTitle();
0117     serviceTrack->setTitle( title );
0118     serviceTrack->setId( track.trackId() );
0119     serviceTrack->setUidUrl( track.playUrl() ); //was: setUrl
0120     serviceTrack->setDownloadableUrl( track.downloadUrl() );
0121     serviceTrack->setLength( track.trackLength() );
0122     serviceTrack->setTrackNumber( track.trackNumber() );
0123     serviceTrack->setYear( track.albumYear() );
0124 
0125     //Building a Meta::Album
0126     title = track.albumTitle().isEmpty() ? "Unknown" :  track.albumTitle();
0127     Meta::Mp3TunesAlbum * serviceAlbum = new Meta::Mp3TunesAlbum( title );
0128     QString albumIdStr = QString::number( track.albumId() );
0129     serviceAlbum->setId( track.albumId() );
0130 
0131     QString coverUrl = "http://content.mp3tunes.com/storage/albumartget/<ALBUM_ID>?alternative=1&partner_token=<PARTNER_TOKEN>&sid=<SESSION_ID>";
0132 
0133     coverUrl.replace( "<SESSION_ID>", m_locker->sessionId() );
0134     coverUrl.replace( "<PARTNER_TOKEN>", m_locker->partnerToken() );
0135     coverUrl.replace( "<ALBUM_ID>", albumIdStr );
0136 
0137     serviceAlbum->setCoverUrl(coverUrl);
0138     Meta::AlbumPtr albumPtr( serviceAlbum );
0139     serviceTrack->setAlbumPtr( albumPtr );
0140 
0141     // Building a Meta::Artist
0142     QString name = track.artistName().isEmpty() ? "Unknown" :  track.artistName();
0143     Meta::ServiceArtist * serviceArtist = new Meta::ServiceArtist( name );
0144     serviceArtist->setId( track.artistId() );
0145     Meta::ArtistPtr artistPtr( serviceArtist );
0146 
0147     serviceTrack->setArtist( artistPtr );
0148     serviceAlbum->setArtistName( name );
0149     serviceAlbum->setAlbumArtist( artistPtr );
0150 //    serviceTrack->update( Meta::TrackPtr( serviceTrack ) );
0151 }
0152 
0153 Mp3tunesLocker* Mp3tunesServiceCollection::locker() const
0154 {
0155     return m_locker;
0156 }
0157 
0158 CollectionLocation*
0159 Mp3tunesServiceCollection::location()
0160 {
0161     return new Mp3tunesServiceCollectionLocation( this );
0162 }
0163 
0164 
0165