summaryrefslogtreecommitdiff
path: root/plugin.video.catchuptvandmore/resources/lib/channels/fr/publicsenat.py
blob: 8f8cf6a5d55213e540318131c034ab97f9b7e07d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
# -*- coding: utf-8 -*-
"""
    Catch-up TV & More
    Original work (C) JUL1EN094, SPM, SylvainCecchetto
    Copyright (C) 2016  SylvainCecchetto

    This file is part of Catch-up TV & More.

    Catch-up TV & More is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Catch-up TV & More 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 General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with Catch-up TV & More; if not, write to the Free Software Foundation,
    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import re
import ast
from bs4 import BeautifulSoup as bs
from resources.lib import utils
from resources.lib import common

# TO DO
# Add info LIVE TV

# Initialize GNU gettext emulation in addon
# This allows to use UI strings from addon’s English
# strings.po file instead of numeric codes
_ = common.ADDON.initialize_gettext()

URL_ROOT = 'https://www.publicsenat.fr'

URL_LIVE_SITE = 'https://www.publicsenat.fr/direct'

URL_DAILYMOTION_EMBED = 'http://www.dailymotion.com/embed/video/%s'


def channel_entry(params):
    """Entry function of the module"""
    if 'root' in params.next:
        return root(params)
    elif 'list_shows' in params.next:
        return list_shows(params)
    elif 'list_videos' in params.next:
        return list_videos(params)
    elif 'live' in params.next:
        return list_live(params)
    elif 'play' in params.next:
        return get_video_url(params)


CATEGORIES = {
    'https://www.publicsenat.fr/recherche/type/episode/'
    'field_theme/politique-4127?'
    'sort_by=pse_search_date_publication': 'Politique',
    'https://www.publicsenat.fr/recherche/type/episode/'
    'field_theme/societe-4126?sort_by=pse_search_date_publication': 'Société',
    'https://www.publicsenat.fr/recherche/type/episode/'
    'field_theme/debat-4128?sort_by=pse_search_date_publication': 'Débat'
}

CORRECT_MONTH = {
    'janvier': '01',
    'février': '02',
    'mars': '03',
    'avril': '04',
    'mai': '05',
    'juin': '06',
    'juillet': '07',
    'août': '08',
    'septembre': '09',
    'octobre': '10',
    'novembre': '11',
    'décembre': '12'
}


@common.PLUGIN.mem_cached(common.CACHE_TIME)
def root(params):
    """Add Replay and Live in the listing"""
    modes = []

    # Add Replay
    modes.append({
        'label': 'Replay',
        'url': common.PLUGIN.get_url(
            action='channel_entry',
            next='list_shows_1',
            category='%s Replay' % params.channel_name.upper(),
            window_title='%s Replay' % params.channel_name.upper()
        ),
    })

    # Add Live
    modes.append({
        'label': 'Live TV',
        'url': common.PLUGIN.get_url(
            action='channel_entry',
            next='live_cat',
            category='%s Live TV' % params.channel_name.upper(),
            window_title='%s Live TV' % params.channel_name.upper()
        ),
    })

    return common.PLUGIN.create_listing(
        modes,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL
        ),
    )


@common.PLUGIN.mem_cached(common.CACHE_TIME)
def list_shows(params):
    """Build shows listing"""
    shows = []

    if params.next == 'list_shows_1':

        for category_url, category_name in CATEGORIES.iteritems():

            shows.append({
                'label': category_name,
                'url': common.PLUGIN.get_url(
                    action='channel_entry',
                    category_url=category_url,
                    category_name=category_name,
                    page='0',
                    next='list_videos_1',
                    window_title=category_name
                )
            })

    return common.PLUGIN.create_listing(
        shows,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL
        )
    )


@common.PLUGIN.mem_cached(common.CACHE_TIME)
def list_videos(params):
    """Build videos listing"""
    videos = []

    if 'previous_listing' in params:
        videos = ast.literal_eval(params['previous_listing'])

    url = params.category_url + '&page=' + str(params.page)

    file_path = utils.download_catalog(
        url,
        '%s_%s_%s.html' % (
            params.channel_name,
            params.category_name,
            params.page))
    root_html = open(file_path).read()
    root_soup = bs(root_html, 'html.parser')

    if params.category_name == 'Politique':
        video_soup = root_soup.find_all(
            'article',
            class_="node node-episode node-episode-pse-search-result"
                   " theme-4127 clearfix")
    elif params.category_name == 'Société':
        video_soup = root_soup.find_all(
            'article',
            class_="node node-episode node-episode-pse-search-result "
                   "theme-4126 clearfix")
    elif params.category_name == 'Débat':
        video_soup = root_soup.find_all(
            'article',
            class_="node node-episode node-episode-pse-search-result "
                   "theme-4128 clearfix")

    for video in video_soup:

        # Test Existing Video
        if video.find('div', class_="content").find(
                'div', class_="right").find('div', class_="wrapper-duree"):

            title = ''
            if video.find('div', class_="content").find(
                    'div',
                    class_="field field-name-title-field field-type-text "
                           "field-label-hidden"):
                title = video.find(
                    'div',
                    class_="content"
                ).find(
                    'div',
                    class_="field field-name-field-ref-emission"
                           " field-type-entityreference "
                           "field-label-hidden"
                ).find(
                    'div',
                    class_="field-items"
                ).find(
                    'div',
                    class_="field-item even"
                ).get_text().encode('utf-8') + ' - ' \
                    + video.find(
                        'div',
                        class_="content").find(
                            'div',
                        class_="field field-name-title-field "
                               "field-type-text field-label-hidden").find(
                        'div',
                        class_="field-items").find(
                        'div',
                        class_="field-item even").get_text().encode('utf-8')
            else:
                title = video.find(
                    'div',
                    class_="content").find(
                    'div',
                    class_="field field-name-field-ref-emission"
                           " field-type-entityreference "
                           "field-label-hidden").find(
                    'div',
                    class_="field-items"
                ).find(
                    'div',
                    class_="field-item even"
                ).get_text().encode('utf-8')

            img = ''
            if video.find(
                'div', class_="content").find(
                'div', class_="wrapper-visuel").find(
                'div', class_="scald-atom video").find(
                'div', class_="field field-name-scald-thumbnail"
                              " field-type-image field-label-hidden"):
                img = video.find(
                    'div',
                    class_="content"
                ).find(
                    'div',
                    class_="wrapper-visuel"
                ).find(
                    'div',
                    class_="scald-atom video"
                ).find(
                    'div',
                    class_="field field-name-scald-thumbnail"
                           " field-type-image field-label-hidden"
                ).find(
                    'div',
                    class_="field-items"
                ).find(
                    'div',
                    class_="field-item even"
                ).find('img').get('src')

            plot = ''
            if video.find(
                'div', class_="content").find(
                'div', class_="field field-name-field-contenu"
                              " field-type-text-long field-label-hidden"):
                plot = video.find(
                    'div',
                    class_="content"
                ).find(
                    'div',
                    class_="field field-name-field-contenu "
                           "field-type-text-long field-label-hidden"
                ).find(
                    'div',
                    class_="field-items"
                ).find(
                    'div',
                    class_="field-item even"
                ).get_text().encode('utf-8')

            value_date = video.find(
                'div',
                class_="content"
            ).find('div', class_="first-diffusion").get_text().encode('utf-8')
            date = value_date.split(' ')
            day = date[2]
            try:
                mounth = CORRECT_MONTH[date[3]]
            except Exception:
                mounth = '00'
            year = date[4]

            date = '.'.join((day, mounth, year))
            aired = '-'.join((year, mounth, day))

            duration = 0
            duration = int(video.find('div', class_="content").find(
                'div', class_="right").find(
                    'div',
                    class_="wrapper-duree").get_text().encode(
                    'utf-8')[:-3]) * 60

            url_video = URL_ROOT + video.find(
                'div', class_="content").find('a').get('href').encode('utf-8')

            info = {
                'video': {
                    'title': title,
                    'aired': aired,
                    'date': date,
                    'duration': duration,
                    'year': year,
                    'plot': plot,
                    'mediatype': 'tvshow'
                }
            }

            context_menu = []
            download_video = (
                _('Download'),
                'XBMC.RunPlugin(' + common.PLUGIN.get_url(
                    action='download_video',
                    url_video=url_video) + ')'
            )
            context_menu.append(download_video)

            videos.append({
                'label': title,
                'thumb': img,
                'fanart': img,
                'url': common.PLUGIN.get_url(
                    action='channel_entry',
                    next='play_r',
                    url_video=url_video
                ),
                'is_playable': True,
                'info': info,
                'context_menu': context_menu
            })

    # More videos...
    videos.append({
        'label': common.ADDON.get_localized_string(30100),
        'url': common.PLUGIN.get_url(
            action='channel_entry',
            category_url=params.category_url,
            category_name=params.category_name,
            next='list_videos_1',
            page=str(int(params.page) + 1),
            update_listing=True,
            previous_listing=str(videos)
        ),
    })

    return common.PLUGIN.create_listing(
        videos,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_PLAYCOUNT,
            common.sp.xbmcplugin.SORT_METHOD_DATE,
            common.sp.xbmcplugin.SORT_METHOD_DURATION,
            common.sp.xbmcplugin.SORT_METHOD_LABEL_IGNORE_THE
        ),
        content='tvshows',
        update_listing='update_listing' in params,
    )


@common.PLUGIN.mem_cached(common.CACHE_TIME)
def list_live(params):
    """Build live listing"""
    lives = []

    title = ''
    plot = ''
    duration = 0
    img = ''
    url_live = ''

    html_live = utils.get_webcontent(URL_LIVE_SITE)
    root_soup = bs(html_live, 'html.parser')
    live_soup = root_soup.find(
        'div',
        class_='iframe-responsive')

    url_live = live_soup.find('iframe').get('src')

    title = '%s Live' % params.channel_name.upper()

    info = {
        'video': {
            'title': title,
            'plot': plot,
            'duration': duration
        }
    }

    lives.append({
        'label': title,
        'fanart': img,
        'thumb': img,
        'url': common.PLUGIN.get_url(
            action='channel_entry',
            next='play_l',
            url=url_live,
        ),
        'is_playable': True,
        'info': info
    })

    return common.PLUGIN.create_listing(
        lives,
        sort_methods=(
            common.sp.xbmcplugin.SORT_METHOD_UNSORTED,
            common.sp.xbmcplugin.SORT_METHOD_LABEL
        )
    )


@common.PLUGIN.mem_cached(common.CACHE_TIME)
def get_video_url(params):
    """Get video URL and start video player"""
    if params.next == 'play_r' or params.next == 'download_video':

        url = ''

        html_video = utils.get_webcontent(params.url_video)
        ur_video_soup = bs(html_video, 'html.parser')
        urlembeded_videos_soup = ur_video_soup.find_all('iframe')

        for url in urlembeded_videos_soup:
            url_video_embed = url.get('src').encode('utf-8')
            break  # get first video hard to find by another method

        url_video_embed_http = url_video_embed
        if params.next == 'download_video':
            return url_video_embed_http
        html_video = utils.get_webcontent(url_video_embed_http)
        html_video = html_video.replace('\\', '')

        all_url_video = re.compile(
            r'"type":"video/mp4","url":"(.*?)"').findall(html_video)
        for datas in all_url_video:
            url = datas

        return url

    elif params.next == 'play_l':

        html_live = utils.get_webcontent(params.url)
        html_live = html_live.replace('\\', '')

        url_live = re.compile(
            r'{"type":"application/x-mpegURL","url":"(.*?)"}]}'
        ).findall(html_live)

        # Just one flux no quality to choose
        return url_live[0]