once.py 2.1 KB

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