grooveshark: migrate from ICapVideo to ICapAudio

Signed-off-by: Pierre Mazière <pierre.maziere@gmx.com>
This commit is contained in:
Pierre Mazière 2014-01-02 02:09:34 +01:00
commit 444da30c3a
3 changed files with 75 additions and 75 deletions

View file

@ -19,7 +19,7 @@
from weboob.tools.backend import BaseBackend, BackendConfig from weboob.tools.backend import BaseBackend, BackendConfig
from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.audio import ICapAudio, BaseAudio
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 from weboob.tools.value import ValueBackendPassword, Value
@ -27,7 +27,7 @@ from weboob.tools.value import ValueBackendPassword, Value
__all__ = ['GroovesharkBackend'] __all__ = ['GroovesharkBackend']
class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection): class GroovesharkBackend(BaseBackend, ICapAudio, ICapCollection):
NAME = 'grooveshark' NAME = 'grooveshark'
DESCRIPTION = u'Grooveshark music streaming website' DESCRIPTION = u'Grooveshark music streaming website'
MAINTAINER = u'Bezleputh' MAINTAINER = u'Bezleputh'
@ -46,25 +46,25 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
password = self.config['password'].get() password = self.config['password'].get()
return self.create_browser(username, password) return self.create_browser(username, password)
def fill_video(self, video, fields): def fill_audio(self, audio, fields):
if 'url' in fields: if 'url' in fields:
with self.browser: with self.browser:
video.url = unicode(self.browser.get_stream_url_from_song_id(video.id)) audio.url = unicode(self.browser.get_stream_url_from_song_id(audio.id))
if 'thumbnail' in fields and video.thumbnail: if 'thumbnail' in fields and audio.thumbnail:
with self.browser: with self.browser:
video.thumbnail.data = self.browser.readurl(video.thumbnail.url) audio.thumbnail.data = self.browser.readurl(audio.thumbnail.url)
def search_videos(self, pattern, sortby=ICapVideo.SEARCH_RELEVANCE, nsfw=False): def search_audio(self, pattern, sortby=ICapAudio.SEARCH_RELEVANCE):
with self.browser: with self.browser:
return self.browser.search_videos(pattern) return self.browser.search_audio(pattern)
def get_video(self, _id): def get_audio(self, _id):
with self.browser: with self.browser:
return self.browser.get_video_from_song_id(_id) return self.browser.get_audio_from_song_id(_id)
def iter_resources(self, objs, split_path): def iter_resources(self, objs, split_path):
with self.browser: with self.browser:
if BaseVideo in objs: if BaseAudio in objs:
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')
@ -81,20 +81,20 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
for item in self.browser.search_albums(collection.split_path): for item in self.browser.search_albums(collection.split_path):
yield item yield item
if collection.split_path[0] == u'playlists': if collection.split_path[0] == u'playlists':
for video in self.browser.get_all_songs_from_playlist(collection.split_path[1]): for audio in self.browser.get_all_songs_from_playlist(collection.split_path[1]):
yield video yield audio
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 audio in self.browser.get_all_songs_from_album(collection.split_path[2]):
yield video yield audio
def validate_collection(self, objs, collection): def validate_collection(self, objs, collection):
if collection.path_level == 0: if collection.path_level == 0:
return return
if BaseVideo in objs and (collection.split_path == [u'albums'] or collection.split_path == [u'playlists']): if BaseAudio 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 BaseAudio in objs and collection.path_level == 2 and \
(collection.split_path[0] == u'albums' or collection.split_path[0] == u'playlists'): (collection.split_path[0] == u'albums' or collection.split_path[0] == u'playlists'):
if collection.split_path[0] == u'playlists': if collection.split_path[0] == u'playlists':
try: try:
@ -104,7 +104,7 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
return return
if BaseVideo in objs and collection.path_level == 3 and \ if BaseAudio in objs and collection.path_level == 3 and \
(collection.split_path[0] == u'albums'): (collection.split_path[0] == u'albums'):
try: try:
int(collection.split_path[2]) int(collection.split_path[2])
@ -114,4 +114,4 @@ class GroovesharkBackend(BaseBackend, ICapVideo, ICapCollection):
raise CollectionNotFound(collection.split_path) raise CollectionNotFound(collection.split_path)
OBJECTS = {BaseVideo: fill_video} OBJECTS = {BaseAudio: fill_audio}

View file

@ -19,9 +19,9 @@
from weboob.tools.browser import BaseBrowser, BrowserIncorrectPassword 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 import NotAvailable from weboob.capabilities.audio import BaseAudio
from weboob.capabilities.image import BaseImage from weboob.capabilities.image import BaseImage
from weboob.capabilities.video import BaseVideo from weboob.capabilities import NotAvailable
from weboob.capabilities.collection import Collection from weboob.capabilities.collection import Collection
import hashlib import hashlib
@ -34,9 +34,9 @@ import datetime
__all__ = ['GroovesharkBrowser'] __all__ = ['GroovesharkBrowser']
class GroovesharkVideo(BaseVideo): class GroovesharkAudio(BaseAudio):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
BaseVideo.__init__(self, *args, **kwargs) BaseAudio.__init__(self, *args, **kwargs)
self.ext = u'mp3' self.ext = u'mp3'
@ -66,7 +66,7 @@ class GroovesharkBrowser(BaseBrowser):
GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber') GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber')
COMMUNICATION_TOKEN = None COMMUNICATION_TOKEN = None
VIDEOS_FROM_SONG_RESULTS = None AUDIOS_FROM_SONG_RESULTS = None
def home(self): def home(self):
self.login() self.login()
@ -99,7 +99,7 @@ class GroovesharkBrowser(BaseBrowser):
response = self.API_post(method, parameters, self.create_token(method)) response = self.API_post(method, parameters, self.create_token(method))
return self.create_collection_from_playlists_result(response['result']['Playlists'], split_path) return self.create_collection_from_playlists_result(response['result']['Playlists'], split_path)
def search_videos(self, pattern): def search_audio(self, pattern):
method = 'getResultsFromSearch' method = 'getResultsFromSearch'
parameters = {} parameters = {}
@ -110,7 +110,7 @@ class GroovesharkBrowser(BaseBrowser):
response = self.API_post(method, parameters, self.create_token(method)) response = self.API_post(method, parameters, self.create_token(method))
songs = self.create_video_from_songs_result(response['result']['result']['Songs']) songs = self.create_audio_from_songs_result(response['result']['result']['Songs'])
return songs return songs
@ -129,47 +129,47 @@ class GroovesharkBrowser(BaseBrowser):
return self.create_collection_from_albums_result(response['result']['result']['Albums'], split_path) return self.create_collection_from_albums_result(response['result']['result']['Albums'], split_path)
def create_video_from_songs_result(self, songs): def create_audio_from_songs_result(self, songs):
self.VIDEOS_FROM_SONG_RESULTS = [] self.AUDIOS_FROM_SONG_RESULTS = []
for song in songs: for song in songs:
video = GroovesharkVideo(song['SongID']) audio = GroovesharkAudio(song['SongID'])
video.title = u'Song - %s' % song['SongName'].encode('ascii', 'replace') audio.title = u'Song - %s' % song['SongName'].encode('ascii', 'replace')
video.author = u'%s' % song['ArtistName'].encode('ascii', 'replace') audio.author = u'%s' % song['ArtistName'].encode('ascii', 'replace')
video.description = u'%s - %s - %s' % (video.author, song['AlbumName'].encode('ascii', 'replace'), song['Year'].encode('ascii', 'replace')) audio.description = u'%s - %s - %s' % (audio.author, song['AlbumName'].encode('ascii', 'replace'), song['Year'].encode('ascii', 'replace'))
video.thumbnail = BaseImage(u'http://images.gs-cdn.net/static/albums/40_' + song['CoverArtFilename']) audio.thumbnail = BaseImage(u'http://images.gs-cdn.net/static/albums/40_' + song['CoverArtFilename'])
video.thumbnail.url = video.thumbnail.id audio.thumbnail.url = audio.thumbnail.id
video.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration']))) audio.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration'])))
video.rating = float(song['AvgRating']) audio.rating = float(song['AvgRating'])
try: try:
video.date = datetime.date(year=int(song['Year']), month=1, day=1) audio.date = datetime.date(year=int(song['Year']), month=1, day=1)
except ValueError: except ValueError:
video.date = NotAvailable audio.date = NotAvailable
self.VIDEOS_FROM_SONG_RESULTS.append(video) self.AUDIOS_FROM_SONG_RESULTS.append(audio)
yield video yield audio
def create_video_from_album_result(self, songs): def create_audio_from_album_result(self, songs):
self.VIDEOS_FROM_SONG_RESULTS = [] self.AUDIOS_FROM_SONG_RESULTS = []
videos = list() audios = list()
for song in songs: for song in songs:
video = self.create_video(song) audio = self.create_audio(song)
if video: if audio:
self.VIDEOS_FROM_SONG_RESULTS.append(video) self.AUDIOS_FROM_SONG_RESULTS.append(audio)
videos.append(video) audios.append(audio)
return videos return audios
def create_video(self, song): def create_audio(self, song):
if song['EstimateDuration']: if song['EstimateDuration']:
video = GroovesharkVideo(song['SongID']) audio = GroovesharkAudio(song['SongID'])
video.title = u'Song - %s' % song['Name'].encode('ascii', 'replace') audio.title = u'Song - %s' % song['Name'].encode('ascii', 'replace')
video.author = u'%s' % song['ArtistName'].encode('ascii', 'replace') audio.author = u'%s' % song['ArtistName'].encode('ascii', 'replace')
video.description = u'%s - %s' % (video.author, song['AlbumName'].encode('ascii', 'replace')) audio.description = u'%s - %s' % (audio.author, song['AlbumName'].encode('ascii', 'replace'))
if song['CoverArtFilename']: if song['CoverArtFilename']:
video.thumbnail = BaseImage(u'http://images.gs-cdn.net/static/albums/40_' + song['CoverArtFilename']) audio.thumbnail = BaseImage(u'http://images.gs-cdn.net/static/albums/40_' + song['CoverArtFilename'])
video.thumbnail.url = video.thumbnail.id audio.thumbnail.url = audio.thumbnail.id
video.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration']))) audio.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration'])))
video.date = NotAvailable audio.date = NotAvailable
return video return audio
def create_collection_from_playlists_result(self, playlists, split_path): def create_collection_from_playlists_result(self, playlists, split_path):
items = list() items = list()
@ -186,7 +186,7 @@ class GroovesharkBrowser(BaseBrowser):
parameters['playlistID'] = playlistID parameters['playlistID'] = playlistID
response = self.API_post(method, parameters, self.create_token(method)) response = self.API_post(method, parameters, self.create_token(method))
return self.create_video_from_album_result(response['result']['Songs']) return self.create_audio_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()
@ -206,7 +206,7 @@ class GroovesharkBrowser(BaseBrowser):
parameters['country'] = self.HEADER['country'] parameters['country'] = self.HEADER['country']
response = self.API_post(method, parameters, self.create_token(method)) response = self.API_post(method, parameters, self.create_token(method))
return self.create_video_from_album_result(response['result']) return self.create_audio_from_album_result(response['result'])
def get_communication_token(self): def get_communication_token(self):
parameters = {'secretKey': hashlib.md5(self.HEADER["session"]).hexdigest()} parameters = {'secretKey': hashlib.md5(self.HEADER["session"]).hexdigest()}
@ -220,12 +220,12 @@ class GroovesharkBrowser(BaseBrowser):
rnd = (''.join(random.choice(string.hexdigits) for x in range(6))) rnd = (''.join(random.choice(string.hexdigits) for x in range(6)))
return rnd + hashlib.sha1('%s:%s:%s:%s' % (method, self.COMMUNICATION_TOKEN, self.GROOVESHARK_CONSTANTS[2], rnd)).hexdigest() return rnd + hashlib.sha1('%s:%s:%s:%s' % (method, self.COMMUNICATION_TOKEN, self.GROOVESHARK_CONSTANTS[2], rnd)).hexdigest()
def get_video_from_song_id(self, song_id): def get_audio_from_song_id(self, song_id):
if self.VIDEOS_FROM_SONG_RESULTS: if self.AUDIOS_FROM_SONG_RESULTS:
for video in self.VIDEOS_FROM_SONG_RESULTS: for audio in self.AUDIOS_FROM_SONG_RESULTS:
if video.id == song_id: if audio.id == song_id:
video.url = self.get_stream_url_from_song_id(song_id) audio.url = self.get_stream_url_from_song_id(song_id)
return video return audio
def get_stream_url_from_song_id(self, song_id): def get_stream_url_from_song_id(self, song_id):
method = 'getStreamKeyFromSongIDEx' method = 'getStreamKeyFromSongIDEx'

View file

@ -19,32 +19,32 @@
from weboob.tools.test import BackendTest from weboob.tools.test import BackendTest
from weboob.capabilities.video import BaseVideo from weboob.capabilities.audio import BaseAudio
class GroovesharkTest(BackendTest): class GroovesharkTest(BackendTest):
BACKEND = 'grooveshark' BACKEND = 'grooveshark'
def test_grooveshark_video_search(self): def test_grooveshark_audio_search(self):
result = list(self.backend.search_videos("Loic Lantoine")) result = list(self.backend.search_audios("Loic Lantoine"))
self.assertTrue(len(result) > 0) self.assertTrue(len(result) > 0)
def test_grooveshark_user_playlist(self): def test_grooveshark_user_playlist(self):
l1 = list(self.backend.iter_resources([BaseVideo], [u'playlists'])) l1 = list(self.backend.iter_resources([BaseAudio], [u'playlists']))
assert len(l1) assert len(l1)
c = l1[0] c = l1[0]
l2 = list(self.backend.iter_resources([BaseVideo], c.split_path)) l2 = list(self.backend.iter_resources([BaseAudio], c.split_path))
assert len(l2) assert len(l2)
v = l2[0] v = l2[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, ('url',))
self.assertTrue(v.url is not None, 'URL for video "%s" not found: %s' % (v.id, v.url)) self.assertTrue(v.url is not None, 'URL for audio "%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([BaseAudio], [u'albums', u'live']))
assert len(l1) assert len(l1)
c = l1[0] c = l1[0]
l2 = list(self.backend.iter_resources([BaseVideo], c.split_path)) l2 = list(self.backend.iter_resources([BaseAudio], c.split_path))
assert len(l2) assert len(l2)
v = l2[0] v = l2[0]
self.backend.fillobj(v, ('url',)) self.backend.fillobj(v, ('url',))
self.assertTrue(v.url is not None, 'URL for video "%s" not found: %s' % (v.id, v.url)) self.assertTrue(v.url is not None, 'URL for audio "%s" not found: %s' % (v.id, v.url))