[byutv] Add support for DVR videos (closes #20574)
[youtube-dl] / youtube_dl / extractor / byutv.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     url_basename,
8     parse_duration,
9 )
10
11
12 class BYUtvIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?byutv\.org/(?:watch|player)/(?!event/)(?P<id>[0-9a-f-]+)(?:/(?P<display_id>[^/?#&]+))?'
14     _TESTS = [{
15         'url': 'http://www.byutv.org/watch/6587b9a3-89d2-42a6-a7f7-fd2f81840a7d/studio-c-season-5-episode-5',
16         'info_dict': {
17             'id': 'ZvanRocTpW-G5_yZFeltTAMv6jxOU9KH',
18             'display_id': 'studio-c-season-5-episode-5',
19             'ext': 'mp4',
20             'title': 'Season 5 Episode 5',
21             'description': 'md5:1d31dc18ef4f075b28f6a65937d22c65',
22             'thumbnail': r're:^https?://.*',
23             'duration': 1486.486,
24         },
25         'params': {
26             'skip_download': True,
27         },
28         'add_ie': ['Ooyala'],
29     }, {
30         'url': 'https://www.byutv.org/player/a5467e14-c7f2-46f9-b3c2-cb31a56749c6/byu-soccer-w-argentina-vs-byu-4419',
31         'info_dict': {
32             'id': 'a5467e14-c7f2-46f9-b3c2-cb31a56749c6',
33             'display_id': 'byu-soccer-w-argentina-vs-byu-4419',
34             'ext': 'mp4',
35             'title': 'Argentina vs. BYU (4/4/19)',
36             'duration': 7543.0,
37         },
38         'params': {
39             'skip_download': True
40         },
41     }, {
42         'url': 'http://www.byutv.org/watch/6587b9a3-89d2-42a6-a7f7-fd2f81840a7d',
43         'only_matching': True,
44     }, {
45         'url': 'https://www.byutv.org/player/27741493-dc83-40b0-8420-e7ae38a2ae98/byu-football-toledo-vs-byu-93016?listid=4fe0fee5-0d3c-4a29-b725-e4948627f472&listindex=0&q=toledo',
46         'only_matching': True,
47     }]
48
49     def _real_extract(self, url):
50         mobj = re.match(self._VALID_URL, url)
51         video_id = mobj.group('id')
52
53         info = self._download_json(
54             'https://api.byutv.org/api3/catalog/getvideosforcontent', video_id,
55             query={
56                 'contentid': video_id,
57                 'channel': 'byutv',
58                 'x-byutv-context': 'web$US',
59             }, headers={
60                 'x-byutv-context': 'web$US',
61                 'x-byutv-platformkey': 'xsaaw9c7y5',
62             })
63
64         ep = info.get('ooyalaVOD')
65         if ep:
66             return {
67                 '_type': 'url_transparent',
68                 'ie_key': 'Ooyala',
69                 'url': 'ooyala:%s' % ep['providerId'],
70                 'id': video_id,
71                 'display_id': mobj.group('display_id') or video_id,
72                 'title': ep.get('title'),
73                 'description': ep.get('description'),
74                 'thumbnail': ep.get('imageThumbnail'),
75             }
76         else:
77             ep = info['dvr']
78             formats = self._extract_m3u8_formats(
79                 ep['videoUrl'], video_id, 'mp4', entry_protocol='m3u8_native'
80             )
81             self._sort_formats(formats)
82             return {
83                 'formats': formats,
84                 'id': video_id,
85                 'display_id': url_basename(url),
86                 'title': ep['title'],
87                 'description': ep.get('description'),
88                 'thumbnail': ep.get('imageThumbnail'),
89                 'duration': parse_duration(ep.get('length')),
90             }