piksel.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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/(?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. @staticmethod
  42. def _extract_url(webpage):
  43. mobj = re.search(
  44. r'<iframe[^>]+src=["\'](?P<url>(?:https?:)?//player\.piksel\.com/v/[a-z0-9]+)',
  45. webpage)
  46. if mobj:
  47. return mobj.group('url')
  48. def _real_extract(self, url):
  49. video_id = self._match_id(url)
  50. webpage = self._download_webpage(url, video_id)
  51. app_token = self._search_regex([
  52. r'clientAPI\s*:\s*"([^"]+)"',
  53. r'data-de-api-key\s*=\s*"([^"]+)"'
  54. ], webpage, 'app token')
  55. response = self._download_json(
  56. 'http://player.piksel.com/ws/ws_program/api/%s/mode/json/apiv/5' % app_token,
  57. video_id, query={
  58. 'v': video_id
  59. })['response']
  60. failure = response.get('failure')
  61. if failure:
  62. raise ExtractorError(response['failure']['reason'], expected=True)
  63. video_data = response['WsProgramResponse']['program']['asset']
  64. title = video_data['title']
  65. formats = []
  66. m3u8_url = dict_get(video_data, [
  67. 'm3u8iPadURL',
  68. 'ipadM3u8Url',
  69. 'm3u8AndroidURL',
  70. 'm3u8iPhoneURL',
  71. 'iphoneM3u8Url'])
  72. if m3u8_url:
  73. formats.extend(self._extract_m3u8_formats(
  74. m3u8_url, video_id, 'mp4', 'm3u8_native',
  75. m3u8_id='hls', fatal=False))
  76. asset_type = dict_get(video_data, ['assetType', 'asset_type'])
  77. for asset_file in video_data.get('assetFiles', []):
  78. # TODO: extract rtmp formats
  79. http_url = asset_file.get('http_url')
  80. if not http_url:
  81. continue
  82. tbr = None
  83. vbr = int_or_none(asset_file.get('videoBitrate'), 1024)
  84. abr = int_or_none(asset_file.get('audioBitrate'), 1024)
  85. if asset_type == 'video':
  86. tbr = vbr + abr
  87. elif asset_type == 'audio':
  88. tbr = abr
  89. format_id = ['http']
  90. if tbr:
  91. format_id.append(compat_str(tbr))
  92. formats.append({
  93. 'format_id': '-'.join(format_id),
  94. 'url': unescapeHTML(http_url),
  95. 'vbr': vbr,
  96. 'abr': abr,
  97. 'width': int_or_none(asset_file.get('videoWidth')),
  98. 'height': int_or_none(asset_file.get('videoHeight')),
  99. 'filesize': int_or_none(asset_file.get('filesize')),
  100. 'tbr': tbr,
  101. })
  102. self._sort_formats(formats)
  103. subtitles = {}
  104. for caption in video_data.get('captions', []):
  105. caption_url = caption.get('url')
  106. if caption_url:
  107. subtitles.setdefault(caption.get('locale', 'en'), []).append({
  108. 'url': caption_url})
  109. return {
  110. 'id': video_id,
  111. 'title': title,
  112. 'description': video_data.get('description'),
  113. 'thumbnail': video_data.get('thumbnailUrl'),
  114. 'timestamp': parse_iso8601(video_data.get('dateadd')),
  115. 'formats': formats,
  116. 'subtitles': subtitles,
  117. }