amazonminitv.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import json
  2. from .common import InfoExtractor
  3. from ..utils import ExtractorError, int_or_none, traverse_obj, try_get
  4. class AmazonMiniTVBaseIE(InfoExtractor):
  5. def _real_initialize(self):
  6. self._download_webpage(
  7. 'https://www.amazon.in/minitv', None,
  8. note='Fetching guest session cookies')
  9. AmazonMiniTVBaseIE.session_id = self._get_cookies('https://www.amazon.in')['session-id'].value
  10. def _call_api(self, asin, data=None, note=None):
  11. device = {'clientId': 'ATVIN', 'deviceLocale': 'en_GB'}
  12. if data:
  13. data['variables'].update({
  14. 'contentType': 'VOD',
  15. 'sessionIdToken': self.session_id,
  16. **device,
  17. })
  18. resp = self._download_json(
  19. f'https://www.amazon.in/minitv/api/web/{"graphql" if data else "prs"}',
  20. asin, note=note, headers={'Content-Type': 'application/json'},
  21. data=json.dumps(data).encode() if data else None,
  22. query=None if data else {
  23. 'deviceType': 'A1WMMUXPCUJL4N',
  24. 'contentId': asin,
  25. **device,
  26. })
  27. if resp.get('errors'):
  28. raise ExtractorError(f'MiniTV said: {resp["errors"][0]["message"]}')
  29. elif not data:
  30. return resp
  31. return resp['data'][data['operationName']]
  32. class AmazonMiniTVIE(AmazonMiniTVBaseIE):
  33. _VALID_URL = r'(?:https?://(?:www\.)?amazon\.in/minitv/tp/|amazonminitv:(?:amzn1\.dv\.gti\.)?)(?P<id>[a-f0-9-]+)'
  34. _TESTS = [{
  35. 'url': 'https://www.amazon.in/minitv/tp/75fe3a75-b8fe-4499-8100-5c9424344840?referrer=https%3A%2F%2Fwww.amazon.in%2Fminitv',
  36. 'info_dict': {
  37. 'id': 'amzn1.dv.gti.75fe3a75-b8fe-4499-8100-5c9424344840',
  38. 'ext': 'mp4',
  39. 'title': 'May I Kiss You?',
  40. 'language': 'Hindi',
  41. 'thumbnail': r're:^https?://.*\.jpg$',
  42. 'description': 'md5:a549bfc747973e04feb707833474e59d',
  43. 'release_timestamp': 1644710400,
  44. 'release_date': '20220213',
  45. 'duration': 846,
  46. 'chapters': 'count:2',
  47. 'series': 'Couple Goals',
  48. 'series_id': 'amzn1.dv.gti.56521d46-b040-4fd5-872e-3e70476a04b0',
  49. 'season': 'Season 3',
  50. 'season_number': 3,
  51. 'season_id': 'amzn1.dv.gti.20331016-d9b9-4968-b991-c89fa4927a36',
  52. 'episode': 'May I Kiss You?',
  53. 'episode_number': 2,
  54. 'episode_id': 'amzn1.dv.gti.75fe3a75-b8fe-4499-8100-5c9424344840',
  55. },
  56. }, {
  57. 'url': 'https://www.amazon.in/minitv/tp/280d2564-584f-452f-9c98-7baf906e01ab?referrer=https%3A%2F%2Fwww.amazon.in%2Fminitv',
  58. 'info_dict': {
  59. 'id': 'amzn1.dv.gti.280d2564-584f-452f-9c98-7baf906e01ab',
  60. 'ext': 'mp4',
  61. 'title': 'Jahaan',
  62. 'language': 'Hindi',
  63. 'thumbnail': r're:^https?://.*\.jpg',
  64. 'description': 'md5:05eb765a77bf703f322f120ec6867339',
  65. 'release_timestamp': 1647475200,
  66. 'release_date': '20220317',
  67. 'duration': 783,
  68. 'chapters': [],
  69. },
  70. }, {
  71. 'url': 'https://www.amazon.in/minitv/tp/280d2564-584f-452f-9c98-7baf906e01ab',
  72. 'only_matching': True,
  73. }, {
  74. 'url': 'amazonminitv:amzn1.dv.gti.280d2564-584f-452f-9c98-7baf906e01ab',
  75. 'only_matching': True,
  76. }, {
  77. 'url': 'amazonminitv:280d2564-584f-452f-9c98-7baf906e01ab',
  78. 'only_matching': True,
  79. }]
  80. _GRAPHQL_QUERY_CONTENT = '''
  81. query content($sessionIdToken: String!, $deviceLocale: String, $contentId: ID!, $contentType: ContentType!, $clientId: String) {
  82. content(
  83. applicationContextInput: {deviceLocale: $deviceLocale, sessionIdToken: $sessionIdToken, clientId: $clientId}
  84. contentId: $contentId
  85. contentType: $contentType
  86. ) {
  87. contentId
  88. name
  89. ... on Episode {
  90. contentId
  91. vodType
  92. name
  93. images
  94. description {
  95. synopsis
  96. contentLengthInSeconds
  97. }
  98. publicReleaseDateUTC
  99. audioTracks
  100. seasonId
  101. seriesId
  102. seriesName
  103. seasonNumber
  104. episodeNumber
  105. timecode {
  106. endCreditsTime
  107. }
  108. }
  109. ... on MovieContent {
  110. contentId
  111. vodType
  112. name
  113. description {
  114. synopsis
  115. contentLengthInSeconds
  116. }
  117. images
  118. publicReleaseDateUTC
  119. audioTracks
  120. }
  121. }
  122. }'''
  123. def _real_extract(self, url):
  124. asin = f'amzn1.dv.gti.{self._match_id(url)}'
  125. prs = self._call_api(asin, note='Downloading playback info')
  126. formats, subtitles = [], {}
  127. for type_, asset in prs['playbackAssets'].items():
  128. if not traverse_obj(asset, 'manifestUrl'):
  129. continue
  130. if type_ == 'hls':
  131. m3u8_fmts, m3u8_subs = self._extract_m3u8_formats_and_subtitles(
  132. asset['manifestUrl'], asin, ext='mp4', entry_protocol='m3u8_native',
  133. m3u8_id=type_, fatal=False)
  134. formats.extend(m3u8_fmts)
  135. subtitles = self._merge_subtitles(subtitles, m3u8_subs)
  136. elif type_ == 'dash':
  137. mpd_fmts, mpd_subs = self._extract_mpd_formats_and_subtitles(
  138. asset['manifestUrl'], asin, mpd_id=type_, fatal=False)
  139. formats.extend(mpd_fmts)
  140. subtitles = self._merge_subtitles(subtitles, mpd_subs)
  141. else:
  142. self.report_warning(f'Unknown asset type: {type_}')
  143. title_info = self._call_api(
  144. asin, note='Downloading title info', data={
  145. 'operationName': 'content',
  146. 'variables': {'contentId': asin},
  147. 'query': self._GRAPHQL_QUERY_CONTENT,
  148. })
  149. credits_time = try_get(title_info, lambda x: x['timecode']['endCreditsTime'] / 1000)
  150. is_episode = title_info.get('vodType') == 'EPISODE'
  151. return {
  152. 'id': asin,
  153. 'title': title_info.get('name'),
  154. 'formats': formats,
  155. 'subtitles': subtitles,
  156. 'language': traverse_obj(title_info, ('audioTracks', 0)),
  157. 'thumbnails': [{
  158. 'id': type_,
  159. 'url': url,
  160. } for type_, url in (title_info.get('images') or {}).items()],
  161. 'description': traverse_obj(title_info, ('description', 'synopsis')),
  162. 'release_timestamp': int_or_none(try_get(title_info, lambda x: x['publicReleaseDateUTC'] / 1000)),
  163. 'duration': traverse_obj(title_info, ('description', 'contentLengthInSeconds')),
  164. 'chapters': [{
  165. 'start_time': credits_time,
  166. 'title': 'End Credits',
  167. }] if credits_time else [],
  168. 'series': title_info.get('seriesName'),
  169. 'series_id': title_info.get('seriesId'),
  170. 'season_number': title_info.get('seasonNumber'),
  171. 'season_id': title_info.get('seasonId'),
  172. 'episode': title_info.get('name') if is_episode else None,
  173. 'episode_number': title_info.get('episodeNumber'),
  174. 'episode_id': asin if is_episode else None,
  175. }
  176. class AmazonMiniTVSeasonIE(AmazonMiniTVBaseIE):
  177. IE_NAME = 'amazonminitv:season'
  178. _VALID_URL = r'amazonminitv:season:(?:amzn1\.dv\.gti\.)?(?P<id>[a-f0-9-]+)'
  179. IE_DESC = 'Amazon MiniTV Series, "minitv:season:" prefix'
  180. _TESTS = [{
  181. 'url': 'amazonminitv:season:amzn1.dv.gti.0aa996eb-6a1b-4886-a342-387fbd2f1db0',
  182. 'playlist_mincount': 6,
  183. 'info_dict': {
  184. 'id': 'amzn1.dv.gti.0aa996eb-6a1b-4886-a342-387fbd2f1db0',
  185. },
  186. }, {
  187. 'url': 'amazonminitv:season:0aa996eb-6a1b-4886-a342-387fbd2f1db0',
  188. 'only_matching': True,
  189. }]
  190. _GRAPHQL_QUERY = '''
  191. query getEpisodes($sessionIdToken: String!, $clientId: String, $episodeOrSeasonId: ID!, $deviceLocale: String) {
  192. getEpisodes(
  193. applicationContextInput: {sessionIdToken: $sessionIdToken, deviceLocale: $deviceLocale, clientId: $clientId}
  194. episodeOrSeasonId: $episodeOrSeasonId
  195. ) {
  196. episodes {
  197. ... on Episode {
  198. contentId
  199. name
  200. images
  201. seriesName
  202. seasonId
  203. seriesId
  204. seasonNumber
  205. episodeNumber
  206. description {
  207. synopsis
  208. contentLengthInSeconds
  209. }
  210. publicReleaseDateUTC
  211. }
  212. }
  213. }
  214. }
  215. '''
  216. def _entries(self, asin):
  217. season_info = self._call_api(
  218. asin, note='Downloading season info', data={
  219. 'operationName': 'getEpisodes',
  220. 'variables': {'episodeOrSeasonId': asin},
  221. 'query': self._GRAPHQL_QUERY,
  222. })
  223. for episode in season_info['episodes']:
  224. yield self.url_result(
  225. f'amazonminitv:{episode["contentId"]}', AmazonMiniTVIE, episode['contentId'])
  226. def _real_extract(self, url):
  227. asin = f'amzn1.dv.gti.{self._match_id(url)}'
  228. return self.playlist_result(self._entries(asin), asin)
  229. class AmazonMiniTVSeriesIE(AmazonMiniTVBaseIE):
  230. IE_NAME = 'amazonminitv:series'
  231. _VALID_URL = r'amazonminitv:series:(?:amzn1\.dv\.gti\.)?(?P<id>[a-f0-9-]+)'
  232. _TESTS = [{
  233. 'url': 'amazonminitv:series:amzn1.dv.gti.56521d46-b040-4fd5-872e-3e70476a04b0',
  234. 'playlist_mincount': 3,
  235. 'info_dict': {
  236. 'id': 'amzn1.dv.gti.56521d46-b040-4fd5-872e-3e70476a04b0',
  237. },
  238. }, {
  239. 'url': 'amazonminitv:series:56521d46-b040-4fd5-872e-3e70476a04b0',
  240. 'only_matching': True,
  241. }]
  242. _GRAPHQL_QUERY = '''
  243. query getSeasons($sessionIdToken: String!, $deviceLocale: String, $episodeOrSeasonOrSeriesId: ID!, $clientId: String) {
  244. getSeasons(
  245. applicationContextInput: {deviceLocale: $deviceLocale, sessionIdToken: $sessionIdToken, clientId: $clientId}
  246. episodeOrSeasonOrSeriesId: $episodeOrSeasonOrSeriesId
  247. ) {
  248. seasons {
  249. seasonId
  250. }
  251. }
  252. }
  253. '''
  254. def _entries(self, asin):
  255. season_info = self._call_api(
  256. asin, note='Downloading series info', data={
  257. 'operationName': 'getSeasons',
  258. 'variables': {'episodeOrSeasonOrSeriesId': asin},
  259. 'query': self._GRAPHQL_QUERY,
  260. })
  261. for season in season_info['seasons']:
  262. yield self.url_result(f'amazonminitv:season:{season["seasonId"]}', AmazonMiniTVSeasonIE, season['seasonId'])
  263. def _real_extract(self, url):
  264. asin = f'amzn1.dv.gti.{self._match_id(url)}'
  265. return self.playlist_result(self._entries(asin), asin)