[ultimedia] Capture and output unavailable video message
[youtube-dl] / youtube_dl / extractor / ultimedia.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     qualities,
8     unified_strdate,
9     clean_html,
10 )
11
12
13 class UltimediaIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?ultimedia\.com/default/index/video[^/]+/id/(?P<id>[\d+a-z]+)'
15     _TESTS = [{
16         # news
17         'url': 'https://www.ultimedia.com/default/index/videogeneric/id/s8uk0r',
18         'md5': '276a0e49de58c7e85d32b057837952a2',
19         'info_dict': {
20             'id': 's8uk0r',
21             'ext': 'mp4',
22             'title': 'Loi sur la fin de vie: le texte prévoit un renforcement des directives anticipées',
23             'description': 'md5:3e5c8fd65791487333dda5db8aed32af',
24             'thumbnail': 're:^https?://.*\.jpg',
25             'upload_date': '20150317',
26         },
27     }, {
28         # music
29         'url': 'https://www.ultimedia.com/default/index/videomusic/id/xvpfp8',
30         'md5': '2ea3513813cf230605c7e2ffe7eca61c',
31         'info_dict': {
32             'id': 'xvpfp8',
33             'ext': 'mp4',
34             'title': "Two - C'est la vie (Clip)",
35             'description': 'Two',
36             'thumbnail': 're:^https?://.*\.jpg',
37             'upload_date': '20150224',
38         },
39     }]
40
41     def _real_extract(self, url):
42         video_id = self._match_id(url)
43
44         webpage = self._download_webpage(url, video_id)
45
46         deliver_url = self._search_regex(
47             r'<iframe[^>]+src="(https?://(?:www\.)?ultimedia\.com/deliver/[^"]+)"',
48             webpage, 'deliver URL')
49
50         deliver_page = self._download_webpage(
51             deliver_url, video_id, 'Downloading iframe page')
52
53         if '>This video is currently not available' in deliver_page:
54             raise ExtractorError(
55                 'Video %s is currently not available' % video_id, expected=True)
56
57         player = self._parse_json(
58             self._search_regex(
59                 r"jwplayer\('player(?:_temp)?'\)\.setup\(({.+?})\)\.on", deliver_page, 'player'),
60             video_id)
61
62         quality = qualities(['flash', 'html5'])
63
64         formats = [{
65             'url': mode['config']['file'],
66             'format_id': mode.get('type'),
67             'quality': quality(mode.get('type')),
68         } for mode in player['modes']]
69         self._sort_formats(formats)
70
71         thumbnail = player.get('image')
72
73         title = clean_html((
74             self._html_search_regex(
75                 r'(?s)<div\s+id="catArticle">.+?</div>(.+?)</h1>',
76                 webpage, 'title', default=None)
77             or self._search_regex(
78                 r"var\s+nameVideo\s*=\s*'([^']+)'",
79                 deliver_page, 'title')))
80
81         description = clean_html(self._html_search_regex(
82             r'(?s)<span>Description</span>(.+?)</p>', webpage,
83             'description', fatal=False))
84
85         upload_date = unified_strdate(self._search_regex(
86             r'Ajouté le\s*<span>([^<]+)', webpage,
87             'upload date', fatal=False))
88
89         return {
90             'id': video_id,
91             'title': title,
92             'description': description,
93             'thumbnail': thumbnail,
94             'upload_date': upload_date,
95             'formats': formats,
96         }