45201332d18ccbdf72949e19192b948d64aac105
[youtube-dl] / youtube_dl / extractor / ultimedia.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class UltimediaIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?ultimedia\.com/deliver/(?P<type>generic|musique)(?:/[^/]+)*/(?:src|article)/(?P<id>[\d+a-z]+)'
12     _TESTS = [{
13         # news
14         'url': 'https://www.ultimedia.com/deliver/generic/iframe/mdtk/01601930/zone/1/src/s8uk0r/autoplay/yes/ad/no/width/714/height/435',
15         'md5': '276a0e49de58c7e85d32b057837952a2',
16         'info_dict': {
17             'id': 's8uk0r',
18             'ext': 'mp4',
19             'title': 'Loi sur la fin de vie: le texte prĂ©voit un renforcement des directives anticipĂ©es',
20             'thumbnail': 're:^https?://.*\.jpg',
21             'duration': 74,
22             'upload_date': '20150317',
23             'timestamp': 1426604939,
24             'uploader_id': '3fszv',
25         },
26     }, {
27         # music
28         'url': 'https://www.ultimedia.com/deliver/musique/iframe/mdtk/01601930/zone/1/article/xvpfp8/autoplay/yes/ad/no/width/714/height/435',
29         'md5': '2ea3513813cf230605c7e2ffe7eca61c',
30         'info_dict': {
31             'id': 'xvpfp8',
32             'ext': 'mp4',
33             'title': 'Two - C\'est La Vie (clip)',
34             'thumbnail': 're:^https?://.*\.jpg',
35             'duration': 233,
36             'upload_date': '20150224',
37             'timestamp': 1424760500,
38             'uploader_id': '3rfzk',
39         },
40     }]
41
42     @staticmethod
43     def _extract_url(webpage):
44         mobj = re.search(
45             r'<(?:iframe|script)[^>]+src=["\'](?P<url>(?:https?:)?//(?:www\.)?ultimedia\.com/deliver/(?:generic|musique)(?:/[^/]+)*/(?:src|article)/[\d+a-z]+)',
46             webpage)
47         if mobj:
48             return mobj.group('url')
49
50     def _real_extract(self, url):
51         video_type, video_id = re.match(self._VALID_URL, url).groups()
52
53         deliver_info = self._download_json(
54             'http://www.ultimedia.com/deliver/video?video=%s&topic=%s' % (video_id, video_type),
55             video_id)
56
57         yt_id = deliver_info.get('yt_id')
58         if yt_id:
59             return self.url_result(yt_id, 'Youtube')
60
61         jwconf = deliver_info['jwconf']
62
63         formats = []
64         for source in jwconf['playlist'][0]['sources']:
65             formats.append({
66                 'url': source['file'],
67                 'format_id': source.get('label'),
68             })
69
70         self._sort_formats(formats)
71
72         title = deliver_info['title']
73         thumbnail = jwconf.get('image')
74         duration = int_or_none(deliver_info.get('duration'))
75         timestamp = int_or_none(deliver_info.get('release_time'))
76         uploader_id = deliver_info.get('owner_id')
77
78         return {
79             'id': video_id,
80             'title': title,
81             'thumbnail': thumbnail,
82             'duration': duration,
83             'timestamp': timestamp,
84             'uploader_id': uploader_id,
85             'formats': formats,
86         }