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