You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.9 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_str
  6. from ..utils import (
  7. ExtractorError,
  8. dict_get,
  9. int_or_none,
  10. unescapeHTML,
  11. parse_iso8601,
  12. )
  13. class PikselIE(InfoExtractor):
  14. _VALID_URL = r'https?://player\.piksel\.com/v/(?:refid/[^/]+/prefid/)?(?P<id>[a-z0-9_]+)'
  15. _TESTS = [
  16. {
  17. 'url': 'http://player.piksel.com/v/ums2867l',
  18. 'md5': '34e34c8d89dc2559976a6079db531e85',
  19. 'info_dict': {
  20. 'id': 'ums2867l',
  21. 'ext': 'mp4',
  22. 'title': 'GX-005 with Caption',
  23. 'timestamp': 1481335659,
  24. 'upload_date': '20161210'
  25. }
  26. },
  27. {
  28. # Original source: http://www.uscourts.gov/cameras-courts/state-washington-vs-donald-j-trump-et-al
  29. 'url': 'https://player.piksel.com/v/v80kqp41',
  30. 'md5': '753ddcd8cc8e4fa2dda4b7be0e77744d',
  31. 'info_dict': {
  32. 'id': 'v80kqp41',
  33. 'ext': 'mp4',
  34. 'title': 'WAW- State of Washington vs. Donald J. Trump, et al',
  35. '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.',
  36. 'timestamp': 1486171129,
  37. 'upload_date': '20170204'
  38. }
  39. },
  40. {
  41. # https://www3.nhk.or.jp/nhkworld/en/ondemand/video/2019240/
  42. 'url': 'http://player.piksel.com/v/refid/nhkworld/prefid/nw_vod_v_en_2019_240_20190823233000_02_1566873477',
  43. 'only_matching': True,
  44. }
  45. ]
  46. @staticmethod
  47. def _extract_url(webpage):
  48. mobj = re.search(
  49. r'<iframe[^>]+src=["\'](?P<url>(?:https?:)?//player\.piksel\.com/v/[a-z0-9]+)',
  50. webpage)
  51. if mobj:
  52. return mobj.group('url')
  53. def _real_extract(self, url):
  54. display_id = self._match_id(url)
  55. webpage = self._download_webpage(url, display_id)
  56. video_id = self._search_regex(
  57. r'data-de-program-uuid=[\'"]([a-z0-9]+)',
  58. webpage, 'program uuid', default=display_id)
  59. app_token = self._search_regex([
  60. r'clientAPI\s*:\s*"([^"]+)"',
  61. r'data-de-api-key\s*=\s*"([^"]+)"'
  62. ], webpage, 'app token')
  63. response = self._download_json(
  64. 'http://player.piksel.com/ws/ws_program/api/%s/mode/json/apiv/5' % app_token,
  65. video_id, query={
  66. 'v': video_id
  67. })['response']
  68. failure = response.get('failure')
  69. if failure:
  70. raise ExtractorError(response['failure']['reason'], expected=True)
  71. video_data = response['WsProgramResponse']['program']['asset']
  72. title = video_data['title']
  73. formats = []
  74. m3u8_url = dict_get(video_data, [
  75. 'm3u8iPadURL',
  76. 'ipadM3u8Url',
  77. 'm3u8AndroidURL',
  78. 'm3u8iPhoneURL',
  79. 'iphoneM3u8Url'])
  80. if m3u8_url:
  81. formats.extend(self._extract_m3u8_formats(
  82. m3u8_url, video_id, 'mp4', 'm3u8_native',
  83. m3u8_id='hls', fatal=False))
  84. asset_type = dict_get(video_data, ['assetType', 'asset_type'])
  85. for asset_file in video_data.get('assetFiles', []):
  86. # TODO: extract rtmp formats
  87. http_url = asset_file.get('http_url')
  88. if not http_url:
  89. continue
  90. tbr = None
  91. vbr = int_or_none(asset_file.get('videoBitrate'), 1024)
  92. abr = int_or_none(asset_file.get('audioBitrate'), 1024)
  93. if asset_type == 'video':
  94. tbr = vbr + abr
  95. elif asset_type == 'audio':
  96. tbr = abr
  97. format_id = ['http']
  98. if tbr:
  99. format_id.append(compat_str(tbr))
  100. formats.append({
  101. 'format_id': '-'.join(format_id),
  102. 'url': unescapeHTML(http_url),
  103. 'vbr': vbr,
  104. 'abr': abr,
  105. 'width': int_or_none(asset_file.get('videoWidth')),
  106. 'height': int_or_none(asset_file.get('videoHeight')),
  107. 'filesize': int_or_none(asset_file.get('filesize')),
  108. 'tbr': tbr,
  109. })
  110. self._sort_formats(formats)
  111. subtitles = {}
  112. for caption in video_data.get('captions', []):
  113. caption_url = caption.get('url')
  114. if caption_url:
  115. subtitles.setdefault(caption.get('locale', 'en'), []).append({
  116. 'url': caption_url})
  117. return {
  118. 'id': video_id,
  119. 'title': title,
  120. 'description': video_data.get('description'),
  121. 'thumbnail': video_data.get('thumbnailUrl'),
  122. 'timestamp': parse_iso8601(video_data.get('dateadd')),
  123. 'formats': formats,
  124. 'subtitles': subtitles,
  125. }