[hotstar] improve extraction(closes #12096)
[youtube-dl] / youtube_dl / extractor / hotstar.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     ExtractorError,
7     determine_ext,
8     int_or_none,
9 )
10
11
12 class HotStarIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?hotstar\.com/(?:.+?[/-])?(?P<id>\d{10})'
14     _TESTS = [{
15         'url': 'http://www.hotstar.com/on-air-with-aib--english-1000076273',
16         'info_dict': {
17             'id': '1000076273',
18             'ext': 'mp4',
19             'title': 'On Air With AIB - English',
20             'description': 'md5:c957d8868e9bc793ccb813691cc4c434',
21             'timestamp': 1447227000,
22             'upload_date': '20151111',
23             'duration': 381,
24         },
25         'params': {
26             # m3u8 download
27             'skip_download': True,
28         }
29     }, {
30         'url': 'http://www.hotstar.com/sports/cricket/rajitha-sizzles-on-debut-with-329/2001477583',
31         'only_matching': True,
32     }, {
33         'url': 'http://www.hotstar.com/1000000515',
34         'only_matching': True,
35     }]
36
37     def _download_json(self, url_or_request, video_id, note='Downloading JSON metadata', fatal=True, query=None):
38         json_data = super(HotStarIE, self)._download_json(
39             url_or_request, video_id, note, fatal=fatal, query=query)
40         if json_data['resultCode'] != 'OK':
41             if fatal:
42                 raise ExtractorError(json_data['errorDescription'])
43             return None
44         return json_data['resultObj']
45
46     def _real_extract(self, url):
47         video_id = self._match_id(url)
48         video_data = self._download_json(
49             'http://account.hotstar.com/AVS/besc', video_id, query={
50                 'action': 'GetAggregatedContentDetails',
51                 'channel': 'PCTV',
52                 'contentId': video_id,
53             })['contentInfo'][0]
54         title = video_data['episodeTitle']
55
56         if video_data.get('encrypted') == 'Y':
57             raise ExtractorError('This video is DRM protected.', expected=True)
58
59         formats = []
60         for f in ('JIO',):
61             format_data = self._download_json(
62                 'http://getcdn.hotstar.com/AVS/besc',
63                 video_id, 'Downloading %s JSON metadata' % f,
64                 fatal=False, query={
65                     'action': 'GetCDN',
66                     'asJson': 'Y',
67                     'channel': f,
68                     'id': video_id,
69                     'type': 'VOD',
70                 })
71             if format_data:
72                 format_url = format_data.get('src')
73                 if not format_url:
74                     continue
75                 ext = determine_ext(format_url)
76                 if ext == 'm3u8':
77                     formats.extend(self._extract_m3u8_formats(
78                         format_url, video_id, 'mp4',
79                         m3u8_id='hls', fatal=False))
80                 elif ext == 'f4m':
81                     # produce broken files
82                     continue
83                 else:
84                     formats.append({
85                         'url': format_url,
86                         'width': int_or_none(format_data.get('width')),
87                         'height': int_or_none(format_data.get('height')),
88                     })
89         self._sort_formats(formats)
90
91         return {
92             'id': video_id,
93             'title': title,
94             'description': video_data.get('description'),
95             'duration': int_or_none(video_data.get('duration')),
96             'timestamp': int_or_none(video_data.get('broadcastDate')),
97             'formats': formats,
98             'episode': title,
99             'episode_number': int_or_none(video_data.get('episodeNumber')),
100             'series': video_data.get('contentTitle'),
101         }