[tf1] Fix wat id extraction (closes #21365)
[youtube-dl] / youtube_dl / extractor / tf1.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5
6 from ..utils import js_to_json
7
8
9 class TF1IE(InfoExtractor):
10     """TF1 uses the wat.tv player."""
11     _VALID_URL = r'https?://(?:(?:videos|www|lci)\.tf1|(?:www\.)?(?:tfou|ushuaiatv|histoire|tvbreizh))\.fr/(?:[^/]+/)*(?P<id>[^/?#.]+)'
12     _TESTS = [{
13         'url': 'http://videos.tf1.fr/auto-moto/citroen-grand-c4-picasso-2013-presentation-officielle-8062060.html',
14         'info_dict': {
15             'id': '10635995',
16             'ext': 'mp4',
17             'title': 'Citroën Grand C4 Picasso 2013 : présentation officielle',
18             'description': 'Vidéo officielle du nouveau Citroën Grand C4 Picasso, lancé à l\'automne 2013.',
19         },
20         'params': {
21             # Sometimes wat serves the whole file with the --test option
22             'skip_download': True,
23         },
24         'expected_warnings': ['HTTP Error 404'],
25     }, {
26         'url': 'http://www.tfou.fr/chuggington/videos/le-grand-mysterioso-chuggington-7085291-739.html',
27         'info_dict': {
28             'id': 'le-grand-mysterioso-chuggington-7085291-739',
29             'ext': 'mp4',
30             'title': 'Le grand Mystérioso - Chuggington',
31             'description': 'Le grand Mystérioso - Emery rêve qu\'un article lui soit consacré dans le journal.',
32             'upload_date': '20150103',
33         },
34         'params': {
35             # Sometimes wat serves the whole file with the --test option
36             'skip_download': True,
37         },
38         'skip': 'HTTP Error 410: Gone',
39     }, {
40         'url': 'http://www.tf1.fr/tf1/koh-lanta/videos/replay-koh-lanta-22-mai-2015.html',
41         'only_matching': True,
42     }, {
43         'url': 'http://lci.tf1.fr/sept-a-huit/videos/sept-a-huit-du-24-mai-2015-8611550.html',
44         'only_matching': True,
45     }, {
46         'url': 'http://www.tf1.fr/hd1/documentaire/videos/mylene-farmer-d-une-icone.html',
47         'only_matching': True,
48     }, {
49         'url': 'https://www.tf1.fr/tmc/quotidien-avec-yann-barthes/videos/quotidien-premiere-partie-11-juin-2019.html',
50         'info_dict': {
51             'id': '13641379',
52             'ext': 'mp4',
53             'title': 'md5:f392bc52245dc5ad43771650c96fb620',
54             'description': 'md5:44bc54f0a21322f5b91d68e76a544eae',
55             'upload_date': '20190611',
56         },
57         'params': {
58             # Sometimes wat serves the whole file with the --test option
59             'skip_download': True,
60         },
61     }]
62
63     def _real_extract(self, url):
64         video_id = self._match_id(url)
65         webpage = self._download_webpage(url, video_id)
66         vids_data_string = self._html_search_regex(
67             r'<script>\s*window\.__APOLLO_STATE__\s*=\s*(?P<vids_data_string>\{.*?\})\s*;?\s*</script>',
68             webpage, 'videos data string', group='vids_data_string', default=None)
69         wat_id = None
70         if vids_data_string is not None:
71             vids_data = self._parse_json(
72                 vids_data_string, video_id,
73                 transform_source=js_to_json)
74             video_data = [v for v in vids_data.values()
75                           if 'slug' in v and v['slug'] == video_id]
76             if len(video_data) > 0 and 'streamId' in video_data[0]:
77                 wat_id = video_data[0]['streamId']
78         if wat_id is None:
79             wat_id = self._html_search_regex(
80                 [r'(["\'])(?:https?:)?//www\.wat\.tv/embedframe/.*?(?P<id>\d{8})\1',
81                  r'(["\']?)streamId\1\s*:\s*(["\']?)(?P<id>\d+)\2'
82                  ],
83                 webpage, 'wat id', group='id')
84         return self.url_result('wat:%s' % wat_id, 'Wat')