[radiofrance] site changed / backend rewritten using browser2

This commit is contained in:
Bezleputh 2015-06-25 13:57:25 +02:00
commit cfb603c6ad
4 changed files with 170 additions and 329 deletions

View file

@ -17,172 +17,20 @@
# 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.deprecated.browser import Browser, Page
from weboob.tools.json import json
from weboob.capabilities.video import BaseVideo
from weboob.deprecated.browser.decorators import id2url
from weboob.browser import PagesBrowser, URL
from .pages import PlayerPage, TimelinePage
from time import time
import re
from urlparse import parse_qs
__all__ = ['RadioFranceBrowser']
__all__ = ['RadioFranceBrowser', 'RadioFranceVideo']
class RadioFranceBrowser(PagesBrowser):
timeline = URL('sites/default/files/(?P<json_url>.*).json', TimelinePage)
player_page = URL('(?P<player>.*)', PlayerPage)
def get_radio_url(self, radio, player):
self.BASEURL = 'http://www.%s.fr/' % radio
return self.player_page.go(player=player).get_url()
class RadioFranceVideo(BaseVideo):
RADIO_DOMAINS = ('franceinter', 'franceculture', 'fipradio', 'franceinfo')
def __init__(self, *args, **kwargs):
BaseVideo.__init__(self, *args, **kwargs)
self.ext = u'mp3'
@classmethod
def id2url(cls, _id):
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' % \
(radio_domain, replay_id)
class PlayerPage(Page):
URL = r'^http://www\.(?P<rdomain>%s)\.fr/player/reecouter\?play=(?P<replay_id>\d+)$' \
% '|'.join(RadioFranceVideo.RADIO_DOMAINS)
MP3_REGEXP = re.compile(r'sites%2Fdefault.+.(?:MP3|mp3)')
def get_url(self):
radio_domain = self.groups[0]
player = self.parser.select(self.document.getroot(), '#rfPlayer embed', 1)
urlparams = parse_qs(player.attrib['src'])
return 'http://www.%s.fr/%s' % (radio_domain, urlparams['urlAOD'][0])
class ReplayPage(Page):
URL = r'^http://www\.(?P<rdomain>%s)\.fr/(?:emission|diffusion)-.+$' \
% '|'.join(RadioFranceVideo.RADIO_DOMAINS)
# the url does not always end with id-yyy-mm-dd, sometimes no mm or dd
URL2 = r'^http://www\.(?P<rdomain>%s)\.fr/[a-z\-]+/[0-9a-z\-]+/[0-9a-z\-]+-[0-9\-]+' \
% 'franceinfo'
def get_id(self):
radio_domain = self.groups[0]
for node in self.parser.select(self.document.getroot(), 'div.node-rf_diffusion'):
match = re.match(r'^node-(\d+)$', node.attrib.get('id', ''))
if match:
player_id = match.groups()[0]
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)
# at least for franceinfo
for node in self.parser.select(self.document.getroot(), '#article .emission-player a.play'):
match = re.match(r'^song-(\d+)$', node.attrib.get('rel', ''))
if match:
player_id = match.groups()[0]
return (radio_domain, player_id)
class DataPage(Page):
def get_current(self):
document = self.document
title = ''
for metas in self.parser.select(document.getroot(), 'div.metas'):
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
else:
artist = ''
return (artist, title)
class RssPage(Page):
def get_title(self):
titles = []
for heading in self.parser.select(self.document.getroot(), 'h1, h2, h3, h4'):
# Remove newlines/multiple spaces
words = heading.text_content()
if words:
for word in unicode(words).split():
titles.append(word)
if len(titles):
return ' '.join(titles)
class RadioFranceBrowser(Browser):
DOMAIN = None
ENCODING = 'UTF-8'
PAGES = {r'http://.*/player/direct': DataPage,
r'http://players.tv-radio.com/radiofrance/metadatas/([a-z]+)RSS.html': RssPage,
PlayerPage.URL: PlayerPage,
ReplayPage.URL: ReplayPage,
ReplayPage.URL2: 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):
self.location('http://%s/player/direct' % self.id2domain(_id))
assert self.is_on_page(DataPage)
return self.page.get_current()
def get_current_rss(self, _id):
self.location('http://players.tv-radio.com/radiofrance/metadatas/%sRSS.html' % _id)
assert self.is_on_page(RssPage)
return self.page.get_title()
def get_current_direct(self, _id):
json_data = self.openurl('http://%s/sites/default/files/direct.json?_=%s' % (self.id2domain(_id), int(time())))
data = json.load(json_data)
title = unicode(data['rf_titre_antenne']['titre'])
artist = unicode(data['rf_titre_antenne']['interprete'])
return (artist, title)
def get_current_direct_large(self, _id):
json_data = self.openurl('http://%s/sites/default/files/import_si/si_titre_antenne/FIP_player_current.json'
% self.id2domain(_id))
data = json.load(json_data)
artist = unicode(data['current']['song']['interpreteMorceau'])
title = unicode(data['current']['song']['titre'])
return (artist, title)
@id2url(RadioFranceVideo.id2url)
def get_video(self, url):
radio_domain = replay_id = None
match = re.match(PlayerPage.URL, url)
if match:
radio_domain, replay_id = match.groups()
elif re.match(ReplayPage.URL, url) or re.match(ReplayPage.URL2, url):
self.location(url)
assert self.is_on_page(ReplayPage)
radio_domain, replay_id = self.page.get_id()
if radio_domain and replay_id:
radio_id = 'fip' if radio_domain == 'fipradio' else radio_domain
_id = '%s-%s' % (radio_id, replay_id)
return RadioFranceVideo(_id)
@id2url(RadioFranceVideo.id2url)
def get_url(self, url):
self.location(url)
assert self.is_on_page(PlayerPage)
return self.page.get_url()
def get_current(self, radio, json_url):
self.BASEURL = 'http://www.%s.fr/' % radio
return self.timeline.go(json_url=json_url).get_current()