From 6fa693f5aa77bda0c0ed359711a9c4b5bf4bbb3e Mon Sep 17 00:00:00 2001 From: Bezleputh Date: Wed, 16 Oct 2013 21:04:52 +0200 Subject: [PATCH] [Grooveshark] add user playlist management --- modules/grooveshark/backend.py | 40 ++++++++++++++++---- modules/grooveshark/browser.py | 69 +++++++++++++++++++++++++++------- modules/grooveshark/test.py | 10 +++++ 3 files changed, 99 insertions(+), 20 deletions(-) diff --git a/modules/grooveshark/backend.py b/modules/grooveshark/backend.py index f16cee52..2be2df26 100644 --- a/modules/grooveshark/backend.py +++ b/modules/grooveshark/backend.py @@ -18,10 +18,11 @@ # along with weboob. If not, see . -from weboob.tools.backend import BaseBackend +from weboob.tools.backend import BaseBackend, BackendConfig from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.collection import ICapCollection, Collection, CollectionNotFound from .browser import GroovesharkBrowser +from weboob.tools.value import ValueBackendPassword, Value __all__ = ['GroovesharkBackend'] @@ -35,6 +36,15 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection): LICENSE = 'AGPLv3+' BROWSER = GroovesharkBrowser + CONFIG = BackendConfig(Value('username', label='Login', default=''), + ValueBackendPassword('password', label='Password', default='')) + + def create_default_browser(self): + password = None + username = self.config['username'].get() + if len(username) > 0: + password = self.config['password'].get() + return self.create_browser(username, password) def fill_video(self, video, fields): if 'url' in fields: @@ -58,11 +68,21 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection): collection = self.get_collection(objs, split_path) if collection.path_level == 0: yield Collection([u'albums'], u'Search for Albums') + if self.browser.is_logged: + yield Collection([u'playlists'], u'Grooveshark Playlists') if collection.path_level == 1: - print u'Enter cd [%s\'s name] then ls to launch search' % collection.split_path[0] - if collection.path_level == 2 and collection.split_path[0] == u'albums': - for item in self.browser.search_albums(collection.split_path): - yield item + if collection.split_path[0] == u'playlists': + for item in self.browser.get_all_user_playlists(collection.split_path): + yield item + elif collection.split_path[0] == u'albums': + print u'Enter cd [%s\'s name] then ls to launch search' % collection.split_path[0] + if collection.path_level == 2: + if collection.split_path[0] == u'albums': + for item in self.browser.search_albums(collection.split_path): + yield item + if collection.split_path[0] == u'playlists': + for video in self.browser.get_all_songs_from_playlist(collection.split_path[1]): + yield video if collection.path_level == 3 and collection.split_path[0] == u'albums': for video in self.browser.get_all_songs_from_album(collection.split_path[2]): yield video @@ -71,11 +91,17 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection): if collection.path_level == 0: return - if BaseVideo in objs and collection.split_path == [u'albums']: + if BaseVideo in objs and (collection.split_path == [u'albums'] or collection.split_path == [u'playlists']): return if BaseVideo in objs and collection.path_level == 2 and \ - (collection.split_path[0] == u'albums'): + (collection.split_path[0] == u'albums' or collection.split_path[0] == u'playlists'): + if collection.split_path[0] == u'playlists': + try: + int(collection.split_path[1]) + except ValueError: + raise CollectionNotFound(collection.split_path) + return if BaseVideo in objs and collection.path_level == 3 and \ diff --git a/modules/grooveshark/browser.py b/modules/grooveshark/browser.py index 2daab168..cbad05d3 100644 --- a/modules/grooveshark/browser.py +++ b/modules/grooveshark/browser.py @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with weboob. If not, see . -from weboob.tools.browser import BaseBrowser +from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword from weboob.tools.json import json as simplejson from weboob.capabilities.video import BaseVideo from weboob.capabilities import NotAvailable @@ -47,6 +47,7 @@ class GroovesharkBrowser(BaseBrowser): PROTOCOL = 'http' DOMAIN = 'html5.grooveshark.com' API_URL = 'https://html5.grooveshark.com/more.php' + IS_LOGGED = False #Setting the static header (country, session and uuid) HEADER = {} @@ -65,11 +66,40 @@ class GroovesharkBrowser(BaseBrowser): GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber') COMMUNICATION_TOKEN = None + USER_ID = None VIDEOS_FROM_SONG_RESULTS = None def home(self): + self.login() self.get_communication_token() + def is_logged(self): + return self.USER_ID is not None and self.USER_ID != 0 + + def login(self): + if self.username and self.password: + method = 'authenticateUser' + + parameters = {} + parameters['username'] = self.username + parameters['password'] = self.password + + response = self.API_post(method, parameters, self.create_token(method)) + self.USER_ID = response['result']['userID'] + + if not self.is_logged: + raise BrowserIncorrectPassword() + + def get_all_user_playlists(self, split_path): + if self.is_logged(): + method = 'userGetPlaylists' + + parameters = {} + parameters['userID'] = self.USER_ID + + response = self.API_post(method, parameters, self.create_token(method)) + return self.create_collection_from_playlists_result(response['result']['Playlists'], split_path) + def search_videos(self, pattern): method = 'getResultsFromSearch' @@ -122,27 +152,40 @@ class GroovesharkBrowser(BaseBrowser): self.VIDEOS_FROM_SONG_RESULTS = [] videos = list() for song in songs: + video = self.create_video(song) + if video: + self.VIDEOS_FROM_SONG_RESULTS.append(video) + videos.append(video) + return videos + + def create_video(self, song): + if song['EstimateDuration']: video = GroovesharkVideo(song['SongID']) video.title = u'Song - %s' % song['Name'].encode('ascii', 'replace') video.author = u'%s' % song['ArtistName'].encode('ascii', 'replace') video.description = u'%s - %s' % (video.author, song['AlbumName'].encode('ascii', 'replace')) if song['CoverArtFilename']: video.thumbnail = Thumbnail(u'http://images.gs-cdn.net/static/albums/40_' + song['CoverArtFilename']) - if song['EstimateDuration']: - video.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration']))) + video.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration']))) video.date = NotAvailable - self.VIDEOS_FROM_SONG_RESULTS.append(video) - videos.append(video) - return videos + return video - def create_video_from_playlist_result(self, playlists): - videos = [] + def create_collection_from_playlists_result(self, playlists, split_path): + items = list() for playlist in playlists: - video = GroovesharkVideo(playlist['PlaylistID']) - video.title = u'Playlist - %s' % (playlist['Name']) - video.description = playlist['Artists'] - videos.append(video) - return videos + path = copy.deepcopy(split_path) + path.append(u'%s' % playlist['PlaylistID']) + items.append(Collection(path, u'%s' % (playlist['Name']))) + return items + + def get_all_songs_from_playlist(self, playlistID): + method = 'getPlaylistByID' + + parameters = {} + parameters['playlistID'] = playlistID + + response = self.API_post(method, parameters, self.create_token(method)) + return self.create_video_from_album_result(response['result']['Songs']) def create_collection_from_albums_result(self, albums, split_path): items = list() diff --git a/modules/grooveshark/test.py b/modules/grooveshark/test.py index 1a922615..8595f8cf 100644 --- a/modules/grooveshark/test.py +++ b/modules/grooveshark/test.py @@ -29,6 +29,16 @@ class GroovesharkTest(BackendTest): result = list(self.backend.search_videos("Loic Lantoine")) self.assertTrue(len(result) > 0) + def test_grooveshark_user_playlist(self): + l1 = list(self.backend.iter_resources([BaseVideo], [u'playlists'])) + assert len(l1) + c = l1[0] + l2 = list(self.backend.iter_resources([BaseVideo], c.split_path)) + assert len(l2) + v = l2[0] + self.backend.fillobj(v, ('url',)) + self.assertTrue(v.url is not None, 'URL for video "%s" not found: %s' % (v.id, v.url)) + def test_grooveshark_album_search(self): l1 = list(self.backend.iter_resources([BaseVideo], [u'albums', u'live'])) assert len(l1)