Start moving to ytdl-org
[youtube-dl] / youtube_dl / extractor / once.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8
9 class OnceIE(InfoExtractor):
10     _VALID_URL = r'https?://.+?\.unicornmedia\.com/now/(?:ads/vmap/)?[^/]+/[^/]+/(?P<domain_id>[^/]+)/(?P<application_id>[^/]+)/(?:[^/]+/)?(?P<media_item_id>[^/]+)/content\.(?:once|m3u8|mp4)'
11     ADAPTIVE_URL_TEMPLATE = 'http://once.unicornmedia.com/now/master/playlist/%s/%s/%s/content.m3u8'
12     PROGRESSIVE_URL_TEMPLATE = 'http://once.unicornmedia.com/now/media/progressive/%s/%s/%s/%s/content.mp4'
13
14     def _extract_once_formats(self, url, http_formats_preference=None):
15         domain_id, application_id, media_item_id = re.match(
16             OnceIE._VALID_URL, url).groups()
17         formats = self._extract_m3u8_formats(
18             self.ADAPTIVE_URL_TEMPLATE % (
19                 domain_id, application_id, media_item_id),
20             media_item_id, 'mp4', m3u8_id='hls', fatal=False)
21         progressive_formats = []
22         for adaptive_format in formats:
23             # Prevent advertisement from embedding into m3u8 playlist (see
24             # https://github.com/ytdl-org/youtube-dl/issues/8893#issuecomment-199912684)
25             adaptive_format['url'] = re.sub(
26                 r'\badsegmentlength=\d+', r'adsegmentlength=0', adaptive_format['url'])
27             rendition_id = self._search_regex(
28                 r'/now/media/playlist/[^/]+/[^/]+/([^/]+)',
29                 adaptive_format['url'], 'redition id', default=None)
30             if rendition_id:
31                 progressive_format = adaptive_format.copy()
32                 progressive_format.update({
33                     'url': self.PROGRESSIVE_URL_TEMPLATE % (
34                         domain_id, application_id, rendition_id, media_item_id),
35                     'format_id': adaptive_format['format_id'].replace(
36                         'hls', 'http'),
37                     'protocol': 'http',
38                     'preference': http_formats_preference,
39                 })
40                 progressive_formats.append(progressive_format)
41         self._check_formats(progressive_formats, media_item_id)
42         formats.extend(progressive_formats)
43         return formats