Add another source for direct

And extract artist / title if possible
This commit is contained in:
Laurent Bachelier 2012-02-02 01:08:41 +01:00
commit a1359c2303

View file

@ -23,6 +23,14 @@ from weboob.capabilities.collection import ICapCollection, CollectionNotFound
from weboob.tools.backend import BaseBackend from weboob.tools.backend import BaseBackend
from weboob.tools.browser import BaseBrowser, BasePage from weboob.tools.browser import BaseBrowser, BasePage
from StringIO import StringIO
from time import time
try:
import json
except ImportError:
import simplejson as json
__all__ = ['RadioFranceBackend'] __all__ = ['RadioFranceBackend']
@ -46,6 +54,17 @@ class RadioFranceBrowser(BaseBrowser):
return self.page.get_title() return self.page.get_title()
def get_current_direct(self, id):
json_data = self.openurl('http://www.%s.fr/sites/default/files/direct.json?_=%s' % (id, int(time())))
data = json.load(json_data)
document = self.parser.parse(StringIO(data.get('html')))
artist = document.findtext('//span[@class="artiste"]')
title = document.findtext('//span[@class="titre"]')
artist = unicode(artist) if artist else None
title = unicode(title) if title else None
return (artist, title)
class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection): class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection):
NAME = 'radiofrance' NAME = 'radiofrance'
@ -73,6 +92,8 @@ class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection):
'lemouv', 'lemouv',
) )
_DIRECTJSON_RADIOS = ('lemouv', 'franceinter', )
_SD_RADIOS = ('franceinfo', ) _SD_RADIOS = ('franceinfo', )
def iter_resources(self, splited_path): def iter_resources(self, splited_path):
@ -115,12 +136,19 @@ class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection):
if 'current' in fields: if 'current' in fields:
if not radio.current: if not radio.current:
radio.current = Emission(0) radio.current = Emission(0)
if radio.id in self._PLAYERJS_RADIOS:
radio.current.artist = None
radio.current.title = self.browser.get_current_playerjs(radio.id)
else:
radio.current.artist = None radio.current.artist = None
radio.current.title = None radio.current.title = None
if radio.id in self._PLAYERJS_RADIOS:
radio.current.title = self.browser.get_current_playerjs(radio.id)
if radio.id in self._DIRECTJSON_RADIOS:
artist, title = self.browser.get_current_direct(radio.id)
if artist:
radio.current.artist = artist
if title:
if radio.current.title:
radio.current.title = "%s [%s]" % (title, radio.current.title)
else:
radio.current.title = title
return radio return radio
OBJECTS = {Radio: fill_radio} OBJECTS = {Radio: fill_radio}