[Grooveshark] add user playlist management

This commit is contained in:
Bezleputh 2013-10-16 21:04:52 +02:00 committed by Florent Fourcot
commit 6fa693f5aa
3 changed files with 99 additions and 20 deletions

View file

@ -18,10 +18,11 @@
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.video import ICapVideo, BaseVideo
from weboob.capabilities.collection import ICapCollection, Collection, CollectionNotFound from weboob.capabilities.collection import ICapCollection, Collection, CollectionNotFound
from .browser import GroovesharkBrowser from .browser import GroovesharkBrowser
from weboob.tools.value import ValueBackendPassword, Value
__all__ = ['GroovesharkBackend'] __all__ = ['GroovesharkBackend']
@ -35,6 +36,15 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
LICENSE = 'AGPLv3+' LICENSE = 'AGPLv3+'
BROWSER = GroovesharkBrowser 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): def fill_video(self, video, fields):
if 'url' in fields: if 'url' in fields:
@ -58,11 +68,21 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
collection = self.get_collection(objs, split_path) collection = self.get_collection(objs, split_path)
if collection.path_level == 0: if collection.path_level == 0:
yield Collection([u'albums'], u'Search for Albums') 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: if collection.path_level == 1:
print u'Enter cd [%s\'s name] then ls to launch search' % collection.split_path[0] if collection.split_path[0] == u'playlists':
if collection.path_level == 2 and collection.split_path[0] == u'albums': for item in self.browser.get_all_user_playlists(collection.split_path):
for item in self.browser.search_albums(collection.split_path): yield item
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': 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]): for video in self.browser.get_all_songs_from_album(collection.split_path[2]):
yield video yield video
@ -71,11 +91,17 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
if collection.path_level == 0: if collection.path_level == 0:
return 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 return
if BaseVideo in objs and collection.path_level == 2 and \ 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 return
if BaseVideo in objs and collection.path_level == 3 and \ if BaseVideo in objs and collection.path_level == 3 and \

View file

@ -17,7 +17,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>. # along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BaseBrowser from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword
from weboob.tools.json import json as simplejson from weboob.tools.json import json as simplejson
from weboob.capabilities.video import BaseVideo from weboob.capabilities.video import BaseVideo
from weboob.capabilities import NotAvailable from weboob.capabilities import NotAvailable
@ -47,6 +47,7 @@ class GroovesharkBrowser(BaseBrowser):
PROTOCOL = 'http' PROTOCOL = 'http'
DOMAIN = 'html5.grooveshark.com' DOMAIN = 'html5.grooveshark.com'
API_URL = 'https://html5.grooveshark.com/more.php' API_URL = 'https://html5.grooveshark.com/more.php'
IS_LOGGED = False
#Setting the static header (country, session and uuid) #Setting the static header (country, session and uuid)
HEADER = {} HEADER = {}
@ -65,11 +66,40 @@ class GroovesharkBrowser(BaseBrowser):
GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber') GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber')
COMMUNICATION_TOKEN = None COMMUNICATION_TOKEN = None
USER_ID = None
VIDEOS_FROM_SONG_RESULTS = None VIDEOS_FROM_SONG_RESULTS = None
def home(self): def home(self):
self.login()
self.get_communication_token() 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): def search_videos(self, pattern):
method = 'getResultsFromSearch' method = 'getResultsFromSearch'
@ -122,27 +152,40 @@ class GroovesharkBrowser(BaseBrowser):
self.VIDEOS_FROM_SONG_RESULTS = [] self.VIDEOS_FROM_SONG_RESULTS = []
videos = list() videos = list()
for song in songs: 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 = GroovesharkVideo(song['SongID'])
video.title = u'Song - %s' % song['Name'].encode('ascii', 'replace') video.title = u'Song - %s' % song['Name'].encode('ascii', 'replace')
video.author = u'%s' % song['ArtistName'].encode('ascii', 'replace') video.author = u'%s' % song['ArtistName'].encode('ascii', 'replace')
video.description = u'%s - %s' % (video.author, song['AlbumName'].encode('ascii', 'replace')) video.description = u'%s - %s' % (video.author, song['AlbumName'].encode('ascii', 'replace'))
if song['CoverArtFilename']: if song['CoverArtFilename']:
video.thumbnail = Thumbnail(u'http://images.gs-cdn.net/static/albums/40_' + 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 video.date = NotAvailable
self.VIDEOS_FROM_SONG_RESULTS.append(video) return video
videos.append(video)
return videos
def create_video_from_playlist_result(self, playlists): def create_collection_from_playlists_result(self, playlists, split_path):
videos = [] items = list()
for playlist in playlists: for playlist in playlists:
video = GroovesharkVideo(playlist['PlaylistID']) path = copy.deepcopy(split_path)
video.title = u'Playlist - %s' % (playlist['Name']) path.append(u'%s' % playlist['PlaylistID'])
video.description = playlist['Artists'] items.append(Collection(path, u'%s' % (playlist['Name'])))
videos.append(video) return items
return videos
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): def create_collection_from_albums_result(self, albums, split_path):
items = list() items = list()

View file

@ -29,6 +29,16 @@ class GroovesharkTest(BackendTest):
result = list(self.backend.search_videos("Loic Lantoine")) result = list(self.backend.search_videos("Loic Lantoine"))
self.assertTrue(len(result) > 0) 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): def test_grooveshark_album_search(self):
l1 = list(self.backend.iter_resources([BaseVideo], [u'albums', u'live'])) l1 = list(self.backend.iter_resources([BaseVideo], [u'albums', u'live']))
assert len(l1) assert len(l1)