]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/sonyliv.py
[extractor] Better error message for DRM (#729)
[yt-dlp.git] / yt_dlp / extractor / sonyliv.py
CommitLineData
8991844e
SS
1# coding: utf-8
2from __future__ import unicode_literals
3
29f7c58a 4import time
5import uuid
6
8991844e 7from .common import InfoExtractor
29f7c58a 8from ..compat import compat_HTTPError
9from ..utils import (
10 ExtractorError,
11 int_or_none,
135e6b93 12 try_get,
29f7c58a 13)
8991844e
SS
14
15
16class SonyLIVIE(InfoExtractor):
135e6b93
A
17 _VALID_URL = r'''(?x)
18 (?:
19 sonyliv:|
20 https?://(?:www\.)?sonyliv\.com/(?:s(?:how|port)s/[^/]+|movies|clip|trailer|music-videos)/[^/?#&]+-
21 )
22 (?P<id>\d+)
23 '''
77426a08 24 _TESTS = [{
29f7c58a 25 'url': 'https://www.sonyliv.com/shows/bachelors-delight-1700000113/achaari-cheese-toast-1000022678?watch=true',
8991844e 26 'info_dict': {
c28cfda8 27 'title': 'Achaari Cheese Toast',
29f7c58a 28 'id': '1000022678',
8991844e 29 'ext': 'mp4',
29f7c58a 30 'upload_date': '20200411',
31 'description': 'md5:3957fa31d9309bf336ceb3f37ad5b7cb',
32 'timestamp': 1586632091,
33 'duration': 185,
34 'season_number': 1,
c28cfda8 35 'series': 'Bachelors Delight',
29f7c58a 36 'episode_number': 1,
37 'release_year': 2016,
8991844e
SS
38 },
39 'params': {
40 'skip_download': True,
41 },
77426a08 42 }, {
29f7c58a 43 'url': 'https://www.sonyliv.com/movies/tahalka-1000050121?watch=true',
44 'only_matching': True,
45 }, {
46 'url': 'https://www.sonyliv.com/clip/jigarbaaz-1000098925',
47 'only_matching': True,
48 }, {
49 'url': 'https://www.sonyliv.com/trailer/sandwiched-forever-1000100286?watch=true',
50 'only_matching': True,
51 }, {
52 'url': 'https://www.sonyliv.com/sports/india-tour-of-australia-2020-21-1700000286/cricket-hls-day-3-1st-test-aus-vs-ind-19-dec-2020-1000100959?watch=true',
53 'only_matching': True,
54 }, {
55 'url': 'https://www.sonyliv.com/music-videos/yeh-un-dinon-ki-baat-hai-1000018779',
77426a08
S
56 'only_matching': True,
57 }]
29f7c58a 58 _GEO_COUNTRIES = ['IN']
59 _TOKEN = None
77426a08 60
29f7c58a 61 def _call_api(self, version, path, video_id):
62 headers = {}
63 if self._TOKEN:
64 headers['security_token'] = self._TOKEN
65 try:
66 return self._download_json(
67 'https://apiv2.sonyliv.com/AGL/%s/A/ENG/WEB/%s' % (version, path),
68 video_id, headers=headers)['resultObj']
69 except ExtractorError as e:
70 if isinstance(e.cause, compat_HTTPError) and e.cause.code == 403:
71 message = self._parse_json(
72 e.cause.read().decode(), video_id)['message']
73 if message == 'Geoblocked Country':
74 self.raise_geo_restricted(countries=self._GEO_COUNTRIES)
75 raise ExtractorError(message)
76 raise
77
78 def _real_initialize(self):
79 self._TOKEN = self._call_api('1.4', 'ALL/GETTOKEN', None)
8991844e
SS
80
81 def _real_extract(self, url):
29f7c58a 82 video_id = self._match_id(url)
83 content = self._call_api(
84 '1.5', 'IN/CONTENT/VIDEOURL/VOD/' + video_id, video_id)
a06916d9 85 if not self.get_param('allow_unplayable_formats') and content.get('isEncrypted'):
88acdbc2 86 self.report_drm(video_id)
29f7c58a 87 dash_url = content['videoURL']
88 headers = {
89 'x-playback-session-id': '%s-%d' % (uuid.uuid4().hex, time.time() * 1000)
90 }
91 formats = self._extract_mpd_formats(
92 dash_url, video_id, mpd_id='dash', headers=headers, fatal=False)
93 formats.extend(self._extract_m3u8_formats(
94 dash_url.replace('.mpd', '.m3u8').replace('/DASH/', '/HLS/'),
95 video_id, 'mp4', m3u8_id='hls', headers=headers, fatal=False))
96 for f in formats:
97 f.setdefault('http_headers', {}).update(headers)
98 self._sort_formats(formats)
99
100 metadata = self._call_api(
101 '1.6', 'IN/DETAIL/' + video_id, video_id)['containers'][0]['metadata']
c28cfda8 102 title = metadata['episodeTitle']
09d18ad0
A
103 subtitles = {}
104 for sub in content.get('subtitle', []):
105 sub_url = sub.get('subtitleUrl')
106 if not sub_url:
107 continue
108 subtitles.setdefault(sub.get('subtitleLanguageName', 'ENG'), []).append({
109 'url': sub_url,
110 })
29f7c58a 111 return {
112 'id': video_id,
113 'title': title,
114 'formats': formats,
115 'thumbnail': content.get('posterURL'),
116 'description': metadata.get('longDescription') or metadata.get('shortDescription'),
117 'timestamp': int_or_none(metadata.get('creationDate'), 1000),
118 'duration': int_or_none(metadata.get('duration')),
119 'season_number': int_or_none(metadata.get('season')),
c28cfda8 120 'series': metadata.get('title'),
29f7c58a 121 'episode_number': int_or_none(metadata.get('episodeNumber')),
122 'release_year': int_or_none(metadata.get('year')),
09d18ad0 123 'subtitles': subtitles,
29f7c58a 124 }
135e6b93
A
125
126
127class SonyLIVSeriesIE(InfoExtractor):
128 _VALID_URL = r'https?://(?:www\.)?sonyliv\.com/shows/[^/?#&]+-(?P<id>\d{10})$'
129 _TESTS = [{
130 'url': 'https://www.sonyliv.com/shows/adaalat-1700000091',
131 'playlist_mincount': 456,
132 'info_dict': {
133 'id': '1700000091',
134 },
135 }]
136 _API_SHOW_URL = "https://apiv2.sonyliv.com/AGL/1.9/R/ENG/WEB/IN/DL/DETAIL/{}?kids_safe=false&from=0&to=49"
137 _API_EPISODES_URL = "https://apiv2.sonyliv.com/AGL/1.4/R/ENG/WEB/IN/CONTENT/DETAIL/BUNDLE/{}?from=0&to=1000&orderBy=episodeNumber&sortOrder=asc"
138 _API_SECURITY_URL = 'https://apiv2.sonyliv.com/AGL/1.4/A/ENG/WEB/ALL/GETTOKEN'
139
140 def _entries(self, show_id):
141 headers = {
142 'Accept': 'application/json, text/plain, */*',
143 'Referer': 'https://www.sonyliv.com',
144 }
145 headers['security_token'] = self._download_json(
146 self._API_SECURITY_URL, video_id=show_id, headers=headers,
147 note='Downloading security token')['resultObj']
148 seasons = try_get(
149 self._download_json(self._API_SHOW_URL.format(show_id), video_id=show_id, headers=headers),
150 lambda x: x['resultObj']['containers'][0]['containers'], list)
151 for season in seasons or []:
152 season_id = season['id']
153 episodes = try_get(
154 self._download_json(self._API_EPISODES_URL.format(season_id), video_id=season_id, headers=headers),
155 lambda x: x['resultObj']['containers'][0]['containers'], list)
156 for episode in episodes or []:
157 video_id = episode.get('id')
158 yield self.url_result('sonyliv:%s' % video_id, ie=SonyLIVIE.ie_key(), video_id=video_id)
159
160 def _real_extract(self, url):
161 show_id = self._match_id(url)
162 return self.playlist_result(self._entries(show_id), playlist_id=show_id)