[telequebec:emission] Extend _VALID_URL
[youtube-dl] / youtube_dl / extractor / telequebec.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_str
6 from ..utils import (
7     int_or_none,
8     smuggle_url,
9     try_get,
10 )
11
12
13 class TeleQuebecBaseIE(InfoExtractor):
14     @staticmethod
15     def _limelight_result(media_id):
16         return {
17             '_type': 'url_transparent',
18             'url': smuggle_url(
19                 'limelight:media:' + media_id, {'geo_countries': ['CA']}),
20             'ie_key': 'LimelightMedia',
21         }
22
23
24 class TeleQuebecIE(TeleQuebecBaseIE):
25     _VALID_URL = r'https?://zonevideo\.telequebec\.tv/media/(?P<id>\d+)'
26     _TESTS = [{
27         # available till 01.01.2023
28         'url': 'http://zonevideo.telequebec.tv/media/37578/un-petit-choc-et-puis-repart/un-chef-a-la-cabane',
29         'info_dict': {
30             'id': '577116881b4b439084e6b1cf4ef8b1b3',
31             'ext': 'mp4',
32             'title': 'Un petit choc et puis repart!',
33             'description': 'md5:b04a7e6b3f74e32d7b294cffe8658374',
34             'upload_date': '20180222',
35             'timestamp': 1519326631,
36         },
37         'params': {
38             'skip_download': True,
39         },
40     }, {
41         # no description
42         'url': 'http://zonevideo.telequebec.tv/media/30261',
43         'only_matching': True,
44     }]
45
46     def _real_extract(self, url):
47         media_id = self._match_id(url)
48
49         media_data = self._download_json(
50             'https://mnmedias.api.telequebec.tv/api/v2/media/' + media_id,
51             media_id)['media']
52
53         info = self._limelight_result(media_data['streamInfo']['sourceId'])
54         info.update({
55             'title': media_data.get('title'),
56             'description': try_get(
57                 media_data, lambda x: x['descriptions'][0]['text'], compat_str),
58             'duration': int_or_none(
59                 media_data.get('durationInMilliseconds'), 1000),
60         })
61         return info
62
63
64 class TeleQuebecEmissionIE(TeleQuebecBaseIE):
65     _VALID_URL = r'''(?x)
66                     https?://
67                         (?:
68                             [^/]+\.telequebec\.tv/emissions/|
69                             (?:www\.)?telequebec\.tv/
70                         )
71                         (?P<id>[^?#&]+)
72                     '''
73     _TESTS = [{
74         'url': 'http://lindicemcsween.telequebec.tv/emissions/100430013/des-soins-esthetiques-a-377-d-interets-annuels-ca-vous-tente',
75         'info_dict': {
76             'id': '66648a6aef914fe3badda25e81a4d50a',
77             'ext': 'mp4',
78             'title': "Des soins esthétiques à 377 % d'intérêts annuels, ça vous tente?",
79             'description': 'md5:369e0d55d0083f1fc9b71ffb640ea014',
80             'upload_date': '20171024',
81             'timestamp': 1508862118,
82         },
83         'params': {
84             'skip_download': True,
85         },
86     }, {
87         'url': 'http://bancpublic.telequebec.tv/emissions/emission-49/31986/jeunes-meres-sous-pression',
88         'only_matching': True,
89     }, {
90         'url': 'http://www.telequebec.tv/masha-et-michka/epi059masha-et-michka-3-053-078',
91         'only_matching': True,
92     }, {
93         'url': 'http://www.telequebec.tv/documentaire/bebes-sur-mesure/',
94         'only_matching': True,
95     }]
96
97     def _real_extract(self, url):
98         display_id = self._match_id(url)
99
100         webpage = self._download_webpage(url, display_id)
101
102         media_id = self._search_regex(
103             r'mediaUID\s*:\s*["\'][Ll]imelight_(?P<id>[a-z0-9]{32})', webpage,
104             'limelight id')
105
106         info = self._limelight_result(media_id)
107         info.update({
108             'title': self._og_search_title(webpage, default=None),
109             'description': self._og_search_description(webpage, default=None),
110         })
111         return info
112
113
114 class TeleQuebecLiveIE(InfoExtractor):
115     _VALID_URL = r'https?://zonevideo\.telequebec\.tv/(?P<id>endirect)'
116     _TEST = {
117         'url': 'http://zonevideo.telequebec.tv/endirect/',
118         'info_dict': {
119             'id': 'endirect',
120             'ext': 'mp4',
121             'title': 're:^Télé-Québec - En direct [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
122             'is_live': True,
123         },
124         'params': {
125             'skip_download': True,
126         },
127     }
128
129     def _real_extract(self, url):
130         video_id = self._match_id(url)
131
132         m3u8_url = None
133         webpage = self._download_webpage(
134             'https://player.telequebec.tv/Tq_VideoPlayer.js', video_id,
135             fatal=False)
136         if webpage:
137             m3u8_url = self._search_regex(
138                 r'm3U8Url\s*:\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
139                 'm3u8 url', default=None, group='url')
140         if not m3u8_url:
141             m3u8_url = 'https://teleqmmd.mmdlive.lldns.net/teleqmmd/f386e3b206814e1f8c8c1c71c0f8e748/manifest.m3u8'
142         formats = self._extract_m3u8_formats(
143             m3u8_url, video_id, 'mp4', m3u8_id='hls')
144         self._sort_formats(formats)
145
146         return {
147             'id': video_id,
148             'title': self._live_title('Télé-Québec - En direct'),
149             'is_live': True,
150             'formats': formats,
151         }