radiofrance: Fix fetching FIP current, support replay

FIP now uses the same website as most Radio France radios.
As a consequence, it is able to support CapVideo.

There are a few differences between FIP and the other radios, but this
still allowed for less code, and the code should be more robust for
every radio.
This commit is contained in:
Laurent Bachelier 2012-03-14 16:25:02 +01:00
commit 184cabf345
3 changed files with 69 additions and 52 deletions

View file

@ -96,11 +96,11 @@ class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection, ICapVideo):
'franceculture', 'franceculture',
'franceinfo', 'franceinfo',
'lemouv', 'lemouv',
'fip',
) )
_DIRECTJSON_RADIOS = ('lemouv', 'franceinter', ) _DIRECTJSON_RADIOS = ('lemouv', 'franceinter', )
_RSS_RADIOS = ('francemusique', ) _RSS_RADIOS = ('francemusique', )
_ANTENNA_RADIOS = ('fip', )
def iter_resources(self, objs, split_path): def iter_resources(self, objs, split_path):
if Radio in objs: if Radio in objs:
@ -152,9 +152,11 @@ class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection, ICapVideo):
artist = None artist = None
title = None title = None
if radio.id in self._PLAYERJS_RADIOS: if radio.id in self._PLAYERJS_RADIOS:
title = self.browser.get_current_playerjs(radio.id) artist, title = self.browser.get_current_playerjs(radio.id)
if radio.id in self._DIRECTJSON_RADIOS: if radio.id in self._DIRECTJSON_RADIOS:
artist, dtitle = self.browser.get_current_direct(radio.id) dartist, dtitle = self.browser.get_current_direct(radio.id)
if dartist:
artist = dartist
if dtitle: if dtitle:
if title: if title:
title = "%s [%s]" % (dtitle, title) title = "%s [%s]" % (dtitle, title)
@ -162,8 +164,6 @@ class RadioFranceBackend(BaseBackend, ICapRadio, ICapCollection, ICapVideo):
title = dtitle title = dtitle
if radio.id in self._RSS_RADIOS: if radio.id in self._RSS_RADIOS:
title = self.browser.get_current_rss(radio.id) title = self.browser.get_current_rss(radio.id)
if radio.id in self._ANTENNA_RADIOS:
artist, title = self.browser.get_current_antenna(radio.id)
if title: if title:
if not radio.current or radio.current is NotLoaded: if not radio.current or radio.current is NotLoaded:
radio.current = Emission(0) radio.current = Emission(0)

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, BasePage, BrokenPageError from weboob.tools.browser import BaseBrowser, BasePage
from weboob.capabilities.video import BaseVideo from weboob.capabilities.video import BaseVideo
from weboob.tools.browser.decorators import id2url from weboob.tools.browser.decorators import id2url
@ -40,7 +40,7 @@ __all__ = ['RadioFranceBrowser', 'RadioFranceVideo']
class RadioFranceVideo(BaseVideo): class RadioFranceVideo(BaseVideo):
RADIOS = ('franceinter', 'franceculture') RADIO_DOMAINS = ('franceinter', 'franceculture', 'fipradio', )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
BaseVideo.__init__(self, *args, **kwargs) BaseVideo.__init__(self, *args, **kwargs)
@ -49,41 +49,61 @@ class RadioFranceVideo(BaseVideo):
@classmethod @classmethod
def id2url(cls, _id): def id2url(cls, _id):
radio_id, replay_id = _id.split('-', 2) radio_id, replay_id = _id.split('-', 2)
radio_domain = 'fipradio' if radio_id == 'fip' else radio_id
return 'http://www.%s.fr/player/reecouter?play=%s' % \ return 'http://www.%s.fr/player/reecouter?play=%s' % \
(radio_id, replay_id) (radio_domain, replay_id)
class PlayerPage(BasePage): class PlayerPage(BasePage):
URL = r'^http://www\.(?P<radio_id>%s)\.fr/player/reecouter\?play=(?P<replay_id>\d+)$' \ URL = r'^http://www\.(?P<rdomain>%s)\.fr/player/reecouter\?play=(?P<replay_id>\d+)$' \
% '|'.join(RadioFranceVideo.RADIOS) % '|'.join(RadioFranceVideo.RADIO_DOMAINS)
MP3_REGEXP = re.compile(r'sites%2Fdefault.+.(?:MP3|mp3)') MP3_REGEXP = re.compile(r'sites%2Fdefault.+.(?:MP3|mp3)')
def get_url(self): def get_url(self):
radio_id = self.groups[0] radio_domain = self.groups[0]
player = self.parser.select(self.document.getroot(), '#rfPlayer embed', 1) player = self.parser.select(self.document.getroot(), '#rfPlayer embed', 1)
urlparams = parse_qs(player.attrib['src']) urlparams = parse_qs(player.attrib['src'])
return 'http://www.%s.fr/%s' % (radio_id, urlparams['urlAOD'][0]) return 'http://www.%s.fr/%s' % (radio_domain, urlparams['urlAOD'][0])
class ReplayPage(BasePage): class ReplayPage(BasePage):
URL = r'^http://www\.(?P<radio_id>%s)\.fr/emission-.+$' \ URL = r'^http://www\.(?P<rdomain>%s)\.fr/(?:emission|diffusion)-.+$' \
% '|'.join(RadioFranceVideo.RADIOS) % '|'.join(RadioFranceVideo.RADIO_DOMAINS)
def get_id(self): def get_id(self):
radio_id = self.groups[0] radio_domain = self.groups[0]
for node in self.parser.select(self.document.getroot(), 'div.node-rf_diffusion'): for node in self.parser.select(self.document.getroot(), 'div.node-rf_diffusion'):
match = re.match(r'^node-(\d+)$', node.attrib.get('id', '')) match = re.match(r'^node-(\d+)$', node.attrib.get('id', ''))
if match: if match:
player_id = match.groups()[0] player_id = match.groups()[0]
return (radio_id, player_id) return (radio_domain, player_id)
# if we failed, try another way (used in FIP)
# but it might not be as accurate for others
# (some pages have more than one of these)
# so it's only used as a fallback
for node in self.parser.select(self.document.getroot(), 'a.rf-player-open'):
match = re.match(r'^/player/reecouter\?play=(\d+)$', node.attrib.get('href', ''))
if match:
player_id = match.groups()[0]
return (radio_domain, player_id)
class DataPage(BasePage): class DataPage(BasePage):
def get_title(self): def get_current(self):
for metas in self.parser.select(self.document.getroot(), 'div.metas'): document = self.document
title = unicode(metas.text_content()).strip() title = None
if len(title): for metas in self.parser.select(document.getroot(), 'div.metas'):
return title ftitle = unicode(metas.text_content()).strip()
if ftitle:
title = ftitle
# Another format (used by FIP)
artist = document.findtext('//div[@class="metas"]//span[@class="author"]')
if artist:
artist = unicode(artist).strip()
ftitle = document.findtext('//div[@class="subtitle"]')
title = unicode(ftitle).strip() if ftitle else title
return (artist, title)
class RssPage(BasePage): class RssPage(BasePage):
@ -99,35 +119,28 @@ class RssPage(BasePage):
return ' '.join(titles) return ' '.join(titles)
class RssAntennaPage(BasePage):
ENCODING = 'ISO-8859-1'
def get_track(self):
# This information is not always available
try:
marquee = self.parser.select(self.document.getroot(), 'marquee', 1)
track = self.parser.select(marquee, 'font b', 2)
artist = unicode(track[0].text).strip() or None
title = unicode(track[1].text).strip() or None
return (artist, title)
except BrokenPageError:
return (None, None)
class RadioFranceBrowser(BaseBrowser): class RadioFranceBrowser(BaseBrowser):
DOMAIN = None DOMAIN = None
ENCODING = 'UTF-8' ENCODING = 'UTF-8'
PAGES = {r'/playerjs/direct/donneesassociees/html\?guid=$': DataPage, PAGES = {r'/playerjs/direct/donneesassociees/html\?guid=$': DataPage,
r'http://players.tv-radio.com/radiofrance/metadatas/([a-z]+)RSS.html': RssPage, r'http://players.tv-radio.com/radiofrance/metadatas/([a-z]+)RSS.html': RssPage,
r'http://players.tv-radio.com/radiofrance/metadatas/([a-z]+)RSS_a_lantenne.html': RssAntennaPage,
PlayerPage.URL: PlayerPage, PlayerPage.URL: PlayerPage,
ReplayPage.URL: ReplayPage} ReplayPage.URL: ReplayPage}
def id2domain(self, _id):
"""
Get the main website domain for a Radio ID.
"""
# FIP is the only one to use "fip" but "fipradio" for the domain.
if _id == 'fip':
_id = 'fipradio'
return 'www.%s.fr' % _id
def get_current_playerjs(self, _id): def get_current_playerjs(self, _id):
self.location('http://www.%s.fr/playerjs/direct/donneesassociees/html?guid=' % _id) self.location('http://%s/playerjs/direct/donneesassociees/html?guid=' % self.id2domain(_id))
assert self.is_on_page(DataPage) assert self.is_on_page(DataPage)
return self.page.get_title() return self.page.get_current()
def get_current_rss(self, _id): def get_current_rss(self, _id):
self.location('http://players.tv-radio.com/radiofrance/metadatas/%sRSS.html' % _id) self.location('http://players.tv-radio.com/radiofrance/metadatas/%sRSS.html' % _id)
@ -136,7 +149,7 @@ class RadioFranceBrowser(BaseBrowser):
return self.page.get_title() return self.page.get_title()
def get_current_direct(self, _id): def get_current_direct(self, _id):
json_data = self.openurl('http://www.%s.fr/sites/default/files/direct.json?_=%s' % (_id, int(time()))) json_data = self.openurl('http://%s/sites/default/files/direct.json?_=%s' % (self.id2domain(_id), int(time())))
data = json.load(json_data) data = json.load(json_data)
document = self.parser.parse(StringIO(data.get('html'))) document = self.parser.parse(StringIO(data.get('html')))
@ -146,25 +159,18 @@ class RadioFranceBrowser(BaseBrowser):
title = unicode(title) if title else None title = unicode(title) if title else None
return (artist, title) return (artist, title)
def get_current_antenna(self, _id):
self.ENCODING = RssAntennaPage.ENCODING
self.location('http://players.tv-radio.com/radiofrance/metadatas/%sRSS_a_lantenne.html' % _id)
assert self.is_on_page(RssAntennaPage)
result = self.page.get_track()
self.ENCODING = RadioFranceBrowser.ENCODING
return result
@id2url(RadioFranceVideo.id2url) @id2url(RadioFranceVideo.id2url)
def get_video(self, url): def get_video(self, url):
radio_id = replay_id = None radio_domain = replay_id = None
match = re.match(PlayerPage.URL, url) match = re.match(PlayerPage.URL, url)
if match: if match:
radio_id, replay_id = match.groups() radio_domain, replay_id = match.groups()
elif re.match(ReplayPage.URL, url): elif re.match(ReplayPage.URL, url):
self.location(url) self.location(url)
assert self.is_on_page(ReplayPage) assert self.is_on_page(ReplayPage)
radio_id, replay_id = self.page.get_id() radio_domain, replay_id = self.page.get_id()
if radio_id and replay_id: if radio_domain and replay_id:
radio_id = 'fip' if radio_domain == 'fipradio' else radio_domain
_id = '%s-%s' % (radio_id, replay_id) _id = '%s-%s' % (radio_id, replay_id)
return RadioFranceVideo(_id) return RadioFranceVideo(_id)

View file

@ -50,3 +50,14 @@ class RadioFranceTest(BackendTest):
assert vid.id assert vid.id
self.backend.fillobj(vid, ['url']) self.backend.fillobj(vid, ['url'])
assert vid.url.lower().endswith('.mp3') assert vid.url.lower().endswith('.mp3')
# fip (no expiration known)
# getting the proper ID is hard, hence the tests with many urls for the same content
urls = ('http://www.fipradio.fr/diffusion-club-jazzafip-du-13-mars',
'http://www.fipradio.fr/player/reecouter?play=20686',
'fip-20686')
for url in urls:
vid = self.backend.get_video(url)
assert vid.id == urls[-1]
self.backend.fillobj(vid, ['url'])
assert vid.url.lower().endswith('.mp3')