[telequebec] Add support for emissions and refactor (closes #14649, closes #14655)
[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'https?://[^/]+\.telequebec\.tv/emissions/(?P<id>[^?#&]+)'
66     _TESTS = [{
67         'url': 'http://lindicemcsween.telequebec.tv/emissions/100430013/des-soins-esthetiques-a-377-d-interets-annuels-ca-vous-tente',
68         'info_dict': {
69             'id': '66648a6aef914fe3badda25e81a4d50a',
70             'ext': 'mp4',
71             'title': "Des soins esthétiques à 377 % d'intérêts annuels, ça vous tente?",
72             'description': 'md5:369e0d55d0083f1fc9b71ffb640ea014',
73             'upload_date': '20171024',
74             'timestamp': 1508862118,
75         },
76         'params': {
77             'skip_download': True,
78         },
79     }, {
80         'url': 'http://bancpublic.telequebec.tv/emissions/emission-49/31986/jeunes-meres-sous-pression',
81         'only_matching': True,
82     }]
83
84     def _real_extract(self, url):
85         display_id = self._match_id(url)
86
87         webpage = self._download_webpage(url, display_id)
88
89         media_id = self._search_regex(
90             r'mediaUID\s*:\s*["\'][Ll]imelight_(?P<id>[a-z0-9]{32})', webpage,
91             'limelight id')
92
93         info = self._limelight_result(media_id)
94         info.update({
95             'title': self._og_search_title(webpage, default=None),
96             'description': self._og_search_description(webpage, default=None),
97         })
98         return info
99
100
101 class TeleQuebecLiveIE(InfoExtractor):
102     _VALID_URL = r'https?://zonevideo\.telequebec\.tv/(?P<id>endirect)'
103     _TEST = {
104         'url': 'http://zonevideo.telequebec.tv/endirect/',
105         'info_dict': {
106             'id': 'endirect',
107             'ext': 'mp4',
108             'title': 're:^Télé-Québec - En direct [0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}$',
109             'is_live': True,
110         },
111         'params': {
112             'skip_download': True,
113         },
114     }
115
116     def _real_extract(self, url):
117         video_id = self._match_id(url)
118
119         m3u8_url = None
120         webpage = self._download_webpage(
121             'https://player.telequebec.tv/Tq_VideoPlayer.js', video_id,
122             fatal=False)
123         if webpage:
124             m3u8_url = self._search_regex(
125                 r'm3U8Url\s*:\s*(["\'])(?P<url>(?:(?!\1).)+)\1', webpage,
126                 'm3u8 url', default=None, group='url')
127         if not m3u8_url:
128             m3u8_url = 'https://teleqmmd.mmdlive.lldns.net/teleqmmd/f386e3b206814e1f8c8c1c71c0f8e748/manifest.m3u8'
129         formats = self._extract_m3u8_formats(
130             m3u8_url, video_id, 'mp4', m3u8_id='hls')
131         self._sort_formats(formats)
132
133         return {
134             'id': video_id,
135             'title': self._live_title('Télé-Québec - En direct'),
136             'is_live': True,
137             'formats': formats,
138         }