manage albums search in grooveshark

This commit is contained in:
Bezleputh 2013-09-08 18:20:30 +02:00 committed by Florent
commit 9e00f4f9a7
3 changed files with 102 additions and 13 deletions

View file

@ -20,11 +20,13 @@
from weboob.tools.backend import BaseBackend
from weboob.capabilities.video import ICapVideo, BaseVideo
from weboob.capabilities.collection import ICapCollection, Collection, CollectionNotFound
from .browser import GroovesharkBrowser
__all__ = ['GroovesharkBackend']
class GroovesharkBackend(BaseBackend, ICapVideo):
class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
NAME = 'grooveshark'
DESCRIPTION = u'Grooveshark music streaming website'
MAINTAINER = u'Bezleputh'
@ -50,4 +52,40 @@ class GroovesharkBackend(BaseBackend, ICapVideo):
with self.browser:
return self.browser.get_video_from_song_id(_id)
def iter_resources(self, objs, split_path):
with self.browser:
if BaseVideo in objs:
collection = self.get_collection(objs, split_path)
if collection.path_level == 0:
yield Collection([u'albums'], u'Search for Albums')
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.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
def validate_collection(self, objs, collection):
if collection.path_level == 0:
return
if BaseVideo in objs and collection.split_path == [u'albums']:
return
if BaseVideo in objs and collection.path_level == 2 and \
(collection.split_path[0] == u'albums'):
return
if BaseVideo in objs and collection.path_level == 3 and \
(collection.split_path[0] == u'albums'):
try:
int(collection.split_path[2])
except ValueError:
raise CollectionNotFound(collection.split_path)
return
raise CollectionNotFound(collection.split_path)
OBJECTS = {BaseVideo: fill_video}

View file

@ -22,7 +22,9 @@ from weboob.tools.json import json as simplejson
from weboob.capabilities.video import BaseVideo
from weboob.capabilities import NotAvailable
from weboob.tools.capabilities.thumbnail import Thumbnail
from weboob.capabilities.collection import Collection
import hashlib
import copy
import uuid
import string
import random
@ -44,9 +46,6 @@ class APIError(Exception):
class GroovesharkBrowser(BaseBrowser):
PROTOCOL = 'http'
DOMAIN = 'html5.grooveshark.com'
#SAVE_RESPONSE = True
#DEBUG_HTTP = True
#DEBUG_MECHANIZE = True
API_URL = 'https://html5.grooveshark.com/more.php'
#Setting the static header (country, session and uuid)
@ -88,6 +87,21 @@ class GroovesharkBrowser(BaseBrowser):
return songs
def search_albums(self, split_path):
pattern = split_path[1]
method = 'getResultsFromSearch'
parameters = {}
parameters['query'] = pattern.encode(self.ENCODING)
parameters['type'] = ['Albums']
parameters['guts'] = 0
parameters['ppOverr'] = ''
response = self.API_post(method, parameters, self.create_token(method))
return self.create_collection_from_albums_result(response['result']['result']['Albums'], split_path)
def create_video_from_songs_result(self, songs):
self.VIDEOS_FROM_SONG_RESULTS = []
@ -104,9 +118,24 @@ class GroovesharkBrowser(BaseBrowser):
except ValueError:
video.date = NotAvailable
self.VIDEOS_FROM_SONG_RESULTS.append(video)
yield video
def create_video_from_album_result(self, songs):
self.VIDEOS_FROM_SONG_RESULTS = []
videos = list()
for song in songs:
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'))
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.date = NotAvailable
self.VIDEOS_FROM_SONG_RESULTS.append(video)
videos.append(video)
return videos
def create_video_from_playlist_result(self, playlists):
videos = []
for playlist in playlists:
@ -116,14 +145,25 @@ class GroovesharkBrowser(BaseBrowser):
videos.append(video)
return videos
def create_video_from_albums_result(self, albums):
videos = []
def create_collection_from_albums_result(self, albums, split_path):
items = list()
for album in albums:
video = GroovesharkVideo(album['AlbumID'])
video.title = u'Album - %s' % (album['Name'])
video.description = album['Year']
videos.append(video)
return videos
path = copy.deepcopy(split_path)
path.append(u'%s' % album['AlbumID'])
items.append(Collection(path, u'%s - %s' % (album['AlbumName'], album['ArtistName'])))
return items
def get_all_songs_from_album(self, album_id):
method = 'albumGetAllSongs'
parameters = {}
parameters['prefetch'] = False
parameters['mobile'] = True
parameters['albumID'] = int(album_id)
parameters['country'] = self.HEADER['country']
response = self.API_post(method, parameters, self.create_token(method))
return self.create_video_from_album_result(response['result'])
def get_communication_token(self):
parameters = {'secretKey': hashlib.md5(self.HEADER["session"]).hexdigest()}

View file

@ -19,6 +19,7 @@
from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo
class GroovesharkTest(BackendTest):
@ -26,4 +27,14 @@ class GroovesharkTest(BackendTest):
def test_grooveshark_video_search(self):
result = list(self.backend.search_videos("Loic Lantoine"))
self.assertTrue(len(result)>0)
self.assertTrue(len(result) > 0)
def test_grooveshark_album_search(self):
l1 = list(self.backend.iter_resources([BaseVideo], [u'albums', u'live']))
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))