From d2fa9242ea49efe2ddf0950b3b8b69e0b9eb5b98 Mon Sep 17 00:00:00 2001 From: Bezleputh Date: Tue, 24 Feb 2015 15:42:27 +0100 Subject: [PATCH] [senscritique] remove id lists --- modules/senscritique/browser.py | 16 ++++++- modules/senscritique/module.py | 40 +----------------- modules/senscritique/pages.py | 74 ++++++++++++++------------------- 3 files changed, 48 insertions(+), 82 deletions(-) diff --git a/modules/senscritique/browser.py b/modules/senscritique/browser.py index 336238c1..81cb260c 100644 --- a/modules/senscritique/browser.py +++ b/modules/senscritique/browser.py @@ -19,7 +19,7 @@ from weboob.browser import PagesBrowser, URL from weboob.browser.profiles import Firefox -from .pages import AjaxPage, EventPage, JsonResumePage +from .pages import AjaxPage, EventPage, JsonResumePage, SettingsPage import re from lxml.etree import XMLSyntaxError @@ -50,13 +50,14 @@ class SenscritiqueBrowser(PagesBrowser): }) ENCODING = 'utf-8' - + CHANNELS = None BASEURL = 'http://www.senscritique.com' program_page = URL('/sc/tv_guides') ajax_page = URL('/sc/tv_guides/gridContent.ajax', AjaxPage) event_page = URL('/film/(?P<_id>.*)', EventPage) json_page = URL('/sc/products/storyline/(?P<_id>.*).json', JsonResumePage) + setting_page = URL('/sc/tv_guides/settings.ajax', SettingsPage) LIMIT = 25 # number of results returned for each ajax call (defined in the website). @@ -68,6 +69,17 @@ class SenscritiqueBrowser(PagesBrowser): 'limit': '%d' % LIMIT, } + def get_channels(self): + if not self.CHANNELS: + self.CHANNELS = list(self.setting_page.go().get_channels()) + return self.CHANNELS + + def get_selected_channels(self, package, general=False, cinema=False): + for channel in self.get_channels(): + if (package == 0 or package in channel._networks) and\ + ((general and channel._thema in ('1', '2')) or (cinema and channel._thema == '3')): + yield channel.id + def set_package_settings(self, package, channels): url = 'http://www.senscritique.com/sc/tv_guides/saveSettings.json' # do not use a dict because there are several same keys diff --git a/modules/senscritique/module.py b/modules/senscritique/module.py index 9a21047f..5446b4e1 100644 --- a/modules/senscritique/module.py +++ b/modules/senscritique/module.py @@ -50,36 +50,6 @@ class SenscritiqueModule(Module, CapCalendarEvent): '16': u'Bouygues', }.iteritems())]) - """ - dict that represents ids list of general-interest channels included in a tv package - {'tv package id': ['general-interest channels ids list']} - """ - general = { - 9: [46, 2, 48, 56], - 1: [49, 46, 21, 2, 36, 59, 54, 48, 56, 50, 32, 1, 51, 24, 38, 34, 37, 6, 25, 11, 53, 26, 47], - 2: [49, 46, 21, 2, 36, 59, 54, 48, 56, 50, 32, 1, 51, 24, 38, 34, 37, 6, 25, 11, 53, 26, 47], - 10: [46, 46, 2, 36, 59, 54, 32, 24, 34, 37, 53, 47], - 11: [46, 46, 2, 36, 59, 54, 32, 24, 34, 37, 53, 47], - 12: [49, 46, 2, 36, 59, 54, 32, 24, 34, 37, 53, 47], - 15: [49, 46, 2, 36, 32, 24, 34, 37, 53, 47], - 16: [49, 46, 2, 36, 59, 54, 32, 24, 34, 37, 53, 47], - } - - """ - dict that represents ids list of cinema channels included in a tv package - {'tv package id': ['cinema channels ids list']} - """ - cinema = { - 9: [10, 7], - 1: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 4055, 44, 3, 45, 42, 41, 43, 13, 12], - 2: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 4055, 44, 3, 45, 42, 41, 43, 13, 12], - 10: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 44, 3, 45, 42, 41, 43, 13, 12], - 11: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 4055, 44, 3, 45, 42, 41, 43, 13, 12], - 12: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 44, 3, 45, 42, 41, 43, 13, 12], - 15: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 44, 3, 45, 42, 41, 43, 13, 12], - 16: [10, 7, 9, 8, 52, 19, 18, 17, 16, 20, 15, 14, 4055, 44, 3, 45, 42, 41, 43, 13, 12], - } - CONFIG = BackendConfig(Value('tv_settings', label=u'T.V. package', choices=tv_settings_choices), ValueBool('general', label='General', default=True), ValueBool('cinema', label='Cinema', default=False), @@ -87,14 +57,8 @@ class SenscritiqueModule(Module, CapCalendarEvent): def get_package_and_channels(self): package = int(self.config['tv_settings'].get()) - channels = [] - if package: - if self.config['general'].get(): - channels += self.general[package] - - if self.config['cinema'].get(): - channels += self.cinema[package] - + channels = self.browser.get_selected_channels(package, self.config['general'].get(), + self.config['cinema'].get()) return package, channels def search_events(self, query): diff --git a/modules/senscritique/pages.py b/modules/senscritique/pages.py index 650f3747..d0e3caa1 100644 --- a/modules/senscritique/pages.py +++ b/modules/senscritique/pages.py @@ -20,52 +20,29 @@ from .calendar import SensCritiquenCalendarEvent from datetime import date, datetime, timedelta -from weboob.capabilities.base import empty +from weboob.capabilities.base import empty, BaseObject from weboob.browser.pages import HTMLPage, JsonPage from weboob.browser.elements import ItemElement, ListElement, method from weboob.browser.filters.standard import Filter, CleanText, Regexp, Join, Format, BrowserURL, Env -from weboob.browser.filters.html import Link, Attr +from weboob.browser.filters.html import Link class Channel(Filter): - CHANNELS_PARAM = { - 'einst-3 elgr-data-logo': u'Action', - 'einst-8 elgr-data-logo': u'Canal+ Décalé', - 'einst-9 elgr-data-logo': u'Canal+ Family', - 'einst-12 elgr-data-logo': u'Ciné FX', - 'einst-13 elgr-data-logo': u'Polar', - 'einst-14 elgr-data-logo': u'Ciné+ Classic', - 'einst-15 elgr-data-logo': u'Ciné+ Club', - 'einst-16 elgr-data-logo': u'Ciné+ Emotion', - 'einst-17 elgr-data-logo': u'Ciné+ Famiz', - 'einst-18 elgr-data-logo': u'Ciné+ Frisson', - 'einst-19 elgr-data-logo': u'Ciné+ Premier', - 'einst-21 elgr-data-logo': u'Comédie+', - 'einst-24 elgr-data-logo': u'Disney Channel', - 'einst-25 elgr-data-logo': u'Disney Cinemagic', - 'einst-34 elgr-data-logo': u'Jimmy', - 'einst-36 elgr-data-logo': u'Mangas', - 'einst-37 elgr-data-logo': u'MCM', - 'einst-41 elgr-data-logo': u'OCS Géants', - 'einst-42 elgr-data-logo': u'OCS Choc', - 'einst-44 elgr-data-logo': u'OCS Max', - 'einst-45 elgr-data-logo': u'OCS City', - 'einst-49 elgr-data-logo': u'RTL 9', - 'einst-52 elgr-data-logo': u'TCM Cinéma', - 'einst-54 elgr-data-logo': u'Teva', - 'einst-59 elgr-data-logo': u'TV Breizh', - 'einst-4055 elgr-data-logo': u'Paramount Channel', - } + def __call__(self, item): + channels = item.page.browser.get_channels() + return self.filter(self.select(self.selector, item, key=self._key, obj=self._obj), channels) - def filter(self, el): + def filter(self, el, channels): channel_info = el[0].xpath('div/div[@class="elgr-data-channel"]') if channel_info: - channel = CleanText('.')(channel_info[0]) + return CleanText('.', children=False)(channel_info[0]) else: - channel_info = Attr('div[@class="elgr-product-data"]/span', 'class')(el[0]) - channel = self.CHANNELS_PARAM.get(channel_info) - return channel + channel_id = Regexp(CleanText('div[@class="elgr-product-data"]/span/@class'), + 'einst-(.*) elgr-data-logo')(el[0]) + for channel in channels: + if channel_id == channel.id: + return channel._name class Date(Filter): @@ -131,18 +108,14 @@ class AjaxPage(HTMLPage): return False - class Summary(Filter): - def filter(self, el): - title = Regexp(Attr('div/img', 'alt'), '^Affiche(.*)')(el[0]) - channel = Channel('.')(el[0]) - return u'%s - %s' % (title, channel) - obj_id = Format(u'%s#%s#%s', Regexp(Link('.'), '/film/(.*)'), FormatDate("%Y%m%d%H%M", Date('div/div[@class="elgr-data-diffusion"]')), CleanText(Channel('.'), replace=[(' ', '-')])) obj_start_date = Date('div/div[@class="elgr-data-diffusion"]') - obj_summary = CleanText(Summary('.')) + obj_summary = Format('%s - %s', + Regexp(CleanText('./div/img/@alt'), '^Affiche(.*)'), + Channel('.')) class Description(Filter): @@ -169,3 +142,20 @@ class JsonResumePage(JsonPage): def get_resume(self): if self.doc['json']['success']: return self.doc['json']['data'] + + +class SettingsPage(HTMLPage): + @method + class get_channels(ListElement): + item_xpath = '//li[@class="tse-channels-item hide"]' + + class item(ItemElement): + klass = BaseObject + + obj_id = CleanText('./@data-sc-channel-id') + + def obj__networks(self): + return CleanText('./@data-sc-networks')(self).split(',') + + obj__thema = CleanText('./@data-sc-thema-id') + obj__name = CleanText('./label')