piksel.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. dict_get,
  5. ExtractorError,
  6. int_or_none,
  7. join_nonempty,
  8. parse_iso8601,
  9. try_get,
  10. unescapeHTML,
  11. )
  12. class PikselIE(InfoExtractor):
  13. _VALID_URL = r'''(?x)https?://
  14. (?:
  15. (?:
  16. player\.
  17. (?:
  18. olympusattelecom|
  19. vibebyvista
  20. )|
  21. (?:api|player)\.multicastmedia|
  22. (?:api-ovp|player)\.piksel
  23. )\.com|
  24. (?:
  25. mz-edge\.stream\.co|
  26. movie-s\.nhk\.or
  27. )\.jp|
  28. vidego\.baltimorecity\.gov
  29. )/v/(?:refid/(?P<refid>[^/]+)/prefid/)?(?P<id>[\w-]+)'''
  30. _EMBED_REGEX = [r'<iframe[^>]+src=["\'](?P<url>(?:https?:)?//player\.piksel\.com/v/[a-z0-9]+)']
  31. _TESTS = [
  32. {
  33. 'url': 'http://player.piksel.com/v/ums2867l',
  34. 'md5': '34e34c8d89dc2559976a6079db531e85',
  35. 'info_dict': {
  36. 'id': 'ums2867l',
  37. 'ext': 'mp4',
  38. 'title': 'GX-005 with Caption',
  39. 'timestamp': 1481335659,
  40. 'upload_date': '20161210'
  41. }
  42. },
  43. {
  44. # Original source: http://www.uscourts.gov/cameras-courts/state-washington-vs-donald-j-trump-et-al
  45. 'url': 'https://player.piksel.com/v/v80kqp41',
  46. 'md5': '753ddcd8cc8e4fa2dda4b7be0e77744d',
  47. 'info_dict': {
  48. 'id': 'v80kqp41',
  49. 'ext': 'mp4',
  50. 'title': 'WAW- State of Washington vs. Donald J. Trump, et al',
  51. 'description': 'State of Washington vs. Donald J. Trump, et al, Case Number 17-CV-00141-JLR, TRO Hearing, Civil Rights Case, 02/3/2017, 1:00 PM (PST), Seattle Federal Courthouse, Seattle, WA, Judge James L. Robart presiding.',
  52. 'timestamp': 1486171129,
  53. 'upload_date': '20170204'
  54. }
  55. },
  56. {
  57. # https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2019240/
  58. 'url': 'http://player.piksel.com/v/refid/nhkworld/prefid/nw_vod_v_en_2019_240_20190823233000_02_1566873477',
  59. 'only_matching': True,
  60. }
  61. ]
  62. def _call_api(self, app_token, resource, display_id, query, fatal=True):
  63. response = (self._download_json(
  64. 'http://player.piksel.com/ws/ws_%s/api/%s/mode/json/apiv/5' % (resource, app_token),
  65. display_id, query=query, fatal=fatal) or {}).get('response')
  66. failure = try_get(response, lambda x: x['failure']['reason'])
  67. if failure:
  68. if fatal:
  69. raise ExtractorError(failure, expected=True)
  70. self.report_warning(failure)
  71. return response
  72. def _real_extract(self, url):
  73. ref_id, display_id = self._match_valid_url(url).groups()
  74. webpage = self._download_webpage(url, display_id)
  75. app_token = self._search_regex([
  76. r'clientAPI\s*:\s*"([^"]+)"',
  77. r'data-de-api-key\s*=\s*"([^"]+)"'
  78. ], webpage, 'app token')
  79. query = {'refid': ref_id, 'prefid': display_id} if ref_id else {'v': display_id}
  80. program = self._call_api(
  81. app_token, 'program', display_id, query)['WsProgramResponse']['program']
  82. video_id = program['uuid']
  83. video_data = program['asset']
  84. title = video_data['title']
  85. asset_type = dict_get(video_data, ['assetType', 'asset_type'])
  86. formats = []
  87. def process_asset_file(asset_file):
  88. if not asset_file:
  89. return
  90. # TODO: extract rtmp formats
  91. http_url = asset_file.get('http_url')
  92. if not http_url:
  93. return
  94. tbr = None
  95. vbr = int_or_none(asset_file.get('videoBitrate'), 1024)
  96. abr = int_or_none(asset_file.get('audioBitrate'), 1024)
  97. if asset_type == 'video':
  98. tbr = vbr + abr
  99. elif asset_type == 'audio':
  100. tbr = abr
  101. formats.append({
  102. 'format_id': join_nonempty('http', tbr),
  103. 'url': unescapeHTML(http_url),
  104. 'vbr': vbr,
  105. 'abr': abr,
  106. 'width': int_or_none(asset_file.get('videoWidth')),
  107. 'height': int_or_none(asset_file.get('videoHeight')),
  108. 'filesize': int_or_none(asset_file.get('filesize')),
  109. 'tbr': tbr,
  110. })
  111. def process_asset_files(asset_files):
  112. for asset_file in (asset_files or []):
  113. process_asset_file(asset_file)
  114. process_asset_files(video_data.get('assetFiles'))
  115. process_asset_file(video_data.get('referenceFile'))
  116. if not formats:
  117. asset_id = video_data.get('assetid') or program.get('assetid')
  118. if asset_id:
  119. process_asset_files(try_get(self._call_api(
  120. app_token, 'asset_file', display_id, {
  121. 'assetid': asset_id,
  122. }, False), lambda x: x['WsAssetFileResponse']['AssetFiles']))
  123. m3u8_url = dict_get(video_data, [
  124. 'm3u8iPadURL',
  125. 'ipadM3u8Url',
  126. 'm3u8AndroidURL',
  127. 'm3u8iPhoneURL',
  128. 'iphoneM3u8Url'])
  129. if m3u8_url:
  130. formats.extend(self._extract_m3u8_formats(
  131. m3u8_url, video_id, 'mp4', 'm3u8_native',
  132. m3u8_id='hls', fatal=False))
  133. smil_url = dict_get(video_data, ['httpSmil', 'hdSmil', 'rtmpSmil'])
  134. if smil_url:
  135. transform_source = None
  136. if ref_id == 'nhkworld':
  137. # TODO: figure out if this is something to be fixed in urljoin,
  138. # _parse_smil_formats or keep it here
  139. transform_source = lambda x: x.replace('src="/', 'src="').replace('/media"', '/media/"')
  140. formats.extend(self._extract_smil_formats(
  141. re.sub(r'/od/[^/]+/', '/od/http/', smil_url), video_id,
  142. transform_source=transform_source, fatal=False))
  143. subtitles = {}
  144. for caption in video_data.get('captions', []):
  145. caption_url = caption.get('url')
  146. if caption_url:
  147. subtitles.setdefault(caption.get('locale', 'en'), []).append({
  148. 'url': caption_url})
  149. return {
  150. 'id': video_id,
  151. 'title': title,
  152. 'description': video_data.get('description'),
  153. 'thumbnail': video_data.get('thumbnailUrl'),
  154. 'timestamp': parse_iso8601(video_data.get('dateadd')),
  155. 'formats': formats,
  156. 'subtitles': subtitles,
  157. '_format_sort_fields': ('tbr', ), # Incomplete resolution information
  158. }