This commit is contained in:
Romain Bignon 2013-04-27 11:51:06 +02:00
commit 37b94a79f2
3 changed files with 12 additions and 43 deletions

View file

@ -19,7 +19,6 @@
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.capabilities.radio import ICapRadio, Radio, Stream, Emission
from weboob.capabilities.video import ICapVideo, BaseVideo from weboob.capabilities.video import ICapVideo, BaseVideo
from weboob.capabilities.collection import ICapCollection from weboob.capabilities.collection import ICapCollection
from .browser import GroovesharkBrowser from .browser import GroovesharkBrowser

View file

@ -20,22 +20,22 @@
from weboob.tools.browser import BaseBrowser from weboob.tools.browser import BaseBrowser
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.tools.capabilities.thumbnail import Thumbnail from weboob.tools.capabilities.thumbnail import Thumbnail
import urllib2
import hashlib import hashlib
import uuid import uuid
import string import string
import random import random
import datetime import datetime
from .pages import IndexPage
__all__ = ['GroovesharkBrowser'] __all__ = ['GroovesharkBrowser']
class APIError(Exception): class APIError(Exception):
pass pass
class GroovesharkBrowser(BaseBrowser): class GroovesharkBrowser(BaseBrowser):
PROTOCOL = 'http' PROTOCOL = 'http'
DOMAIN = 'html5.grooveshark.com' DOMAIN = 'html5.grooveshark.com'
#SAVE_RESPONSE = True #SAVE_RESPONSE = True
@ -59,14 +59,8 @@ class GroovesharkBrowser(BaseBrowser):
#those values depends on a grooveshark version and may change #those values depends on a grooveshark version and may change
GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber') GROOVESHARK_CONSTANTS = ('mobileshark', '20120830', 'gooeyFlubber')
COMMUNICATION_TOKEN = None COMMUNICATION_TOKEN = None
PAGES = {
'http://html5.grooveshark.com/.*': IndexPage,
}
def home(self): def home(self):
self.location('/');
assert self.is_on_page(IndexPage)
self.get_communication_token() self.get_communication_token()
def search_videos(self, pattern, max_results): def search_videos(self, pattern, max_results):
@ -100,8 +94,11 @@ class GroovesharkBrowser(BaseBrowser):
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'])
video.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration']))) video.duration = datetime.timedelta(seconds=int(float(song['EstimateDuration'])))
video.rating = float(song['AvgRating']) video.rating = float(song['AvgRating'])
video.date = datetime.date(year=int(song['Year']), month=1, day=1) try:
videos.append(video) video.date = datetime.date(year=int(song['Year']), month=1, day=1)
except ValueError:
video.date = NotAvailable
videos.append(video)
return videos return videos
def create_video_from_playlist_result(self, playlists): def create_video_from_playlist_result(self, playlists):
@ -129,9 +126,8 @@ class GroovesharkBrowser(BaseBrowser):
self.COMMUNICATION_TOKEN = result['result'] self.COMMUNICATION_TOKEN = result['result']
def create_token(self, method): def create_token(self, method):
if self.COMMUNICATION_TOKEN is None: if self.COMMUNICATION_TOKEN is None:
self.get_communication_token() self.get_communication_token()
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()
@ -179,8 +175,8 @@ class GroovesharkBrowser(BaseBrowser):
""" """
data = self.create_json_data(method, parameters, token) data = self.create_json_data(method, parameters, token)
req = self.create_request(method) req = self.create_request(method)
response = urllib2.urlopen(req, data) response = self.openurl(req, data)
return self.parse_response(response) return self.parse_response(response)
def create_json_data(self, method, parameters, token): def create_json_data(self, method, parameters, token):
data = {} data = {}
@ -194,7 +190,7 @@ class GroovesharkBrowser(BaseBrowser):
return simplejson.dumps(data) return simplejson.dumps(data)
def create_request(self, method): def create_request(self, method):
req = urllib2.Request('%s?%s' %(self.API_URL, method)) req = self.request_class('%s?%s' %(self.API_URL, method))
req.add_header('Content-Type', 'application/json') req.add_header('Content-Type', 'application/json')
return req return req

View file

@ -1,26 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright(C) 2013 Bezleputh
#
# This file is part of weboob.
#
# weboob is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# weboob is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with weboob. If not, see <http://www.gnu.org/licenses/>.
from weboob.tools.browser import BasePage
__all__ = ['IndexPage']
class IndexPage(BasePage):
pass