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.

153 lines
6.0 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import hmac
  3. import hashlib
  4. import base64
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. float_or_none,
  8. int_or_none,
  9. parse_iso8601,
  10. mimetype2ext,
  11. determine_ext,
  12. )
  13. class NYTimesBaseIE(InfoExtractor):
  14. _SECRET = b'pX(2MbU2);4N{7J8)>YwKRJ+/pQ3JkiU2Q^V>mFYv6g6gYvt6v'
  15. def _extract_video_from_id(self, video_id):
  16. # Authorization generation algorithm is reverse engineered from `signer` in
  17. # http://graphics8.nytimes.com/video/vhs/vhs-2.x.min.js
  18. path = '/svc/video/api/v3/video/' + video_id
  19. hm = hmac.new(self._SECRET, (path + ':vhs').encode(), hashlib.sha512).hexdigest()
  20. video_data = self._download_json('http://www.nytimes.com' + path, video_id, 'Downloading video JSON', headers={
  21. 'Authorization': 'NYTV ' + base64.b64encode(hm.encode()).decode(),
  22. 'X-NYTV': 'vhs',
  23. }, fatal=False)
  24. if not video_data:
  25. video_data = self._download_json(
  26. 'http://www.nytimes.com/svc/video/api/v2/video/' + video_id,
  27. video_id, 'Downloading video JSON')
  28. title = video_data['headline']
  29. def get_file_size(file_size):
  30. if isinstance(file_size, int):
  31. return file_size
  32. elif isinstance(file_size, dict):
  33. return int(file_size.get('value', 0))
  34. else:
  35. return None
  36. urls = []
  37. formats = []
  38. for video in video_data.get('renditions', []):
  39. video_url = video.get('url')
  40. format_id = video.get('type')
  41. if not video_url or format_id == 'thumbs' or video_url in urls:
  42. continue
  43. urls.append(video_url)
  44. ext = mimetype2ext(video.get('mimetype')) or determine_ext(video_url)
  45. if ext == 'm3u8':
  46. formats.extend(self._extract_m3u8_formats(
  47. video_url, video_id, 'mp4', 'm3u8_native',
  48. m3u8_id=format_id or 'hls', fatal=False))
  49. elif ext == 'mpd':
  50. continue
  51. # formats.extend(self._extract_mpd_formats(
  52. # video_url, video_id, format_id or 'dash', fatal=False))
  53. else:
  54. formats.append({
  55. 'url': video_url,
  56. 'format_id': format_id,
  57. 'vcodec': video.get('videoencoding') or video.get('video_codec'),
  58. 'width': int_or_none(video.get('width')),
  59. 'height': int_or_none(video.get('height')),
  60. 'filesize': get_file_size(video.get('file_size') or video.get('fileSize')),
  61. 'tbr': int_or_none(video.get('bitrate'), 1000),
  62. 'ext': ext,
  63. })
  64. self._sort_formats(formats)
  65. thumbnails = []
  66. for image in video_data.get('images', []):
  67. image_url = image.get('url')
  68. if not image_url:
  69. continue
  70. thumbnails.append({
  71. 'url': 'http://www.nytimes.com/' + image_url,
  72. 'width': int_or_none(image.get('width')),
  73. 'height': int_or_none(image.get('height')),
  74. })
  75. publication_date = video_data.get('publication_date')
  76. timestamp = parse_iso8601(publication_date[:-8]) if publication_date else None
  77. return {
  78. 'id': video_id,
  79. 'title': title,
  80. 'description': video_data.get('summary'),
  81. 'timestamp': timestamp,
  82. 'uploader': video_data.get('byline'),
  83. 'duration': float_or_none(video_data.get('duration'), 1000),
  84. 'formats': formats,
  85. 'thumbnails': thumbnails,
  86. }
  87. class NYTimesIE(NYTimesBaseIE):
  88. _VALID_URL = r'https?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P<id>\d+)'
  89. _TESTS = [{
  90. 'url': 'http://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263',
  91. 'md5': 'd665342765db043f7e225cff19df0f2d',
  92. 'info_dict': {
  93. 'id': '100000002847155',
  94. 'ext': 'mov',
  95. 'title': 'Verbatim: What Is a Photocopier?',
  96. 'description': 'md5:93603dada88ddbda9395632fdc5da260',
  97. 'timestamp': 1398631707,
  98. 'upload_date': '20140427',
  99. 'uploader': 'Brett Weiner',
  100. 'duration': 419,
  101. }
  102. }, {
  103. 'url': 'http://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.html',
  104. 'only_matching': True,
  105. }]
  106. def _real_extract(self, url):
  107. video_id = self._match_id(url)
  108. return self._extract_video_from_id(video_id)
  109. class NYTimesArticleIE(NYTimesBaseIE):
  110. _VALID_URL = r'https?://(?:www\.)?nytimes\.com/(.(?<!video))*?/(?:[^/]+/)*(?P<id>[^.]+)(?:\.html)?'
  111. _TESTS = [{
  112. 'url': 'http://www.nytimes.com/2015/04/14/business/owner-of-gravity-payments-a-credit-card-processor-is-setting-a-new-minimum-wage-70000-a-year.html?_r=0',
  113. 'md5': 'e2076d58b4da18e6a001d53fd56db3c9',
  114. 'info_dict': {
  115. 'id': '100000003628438',
  116. 'ext': 'mov',
  117. 'title': 'New Minimum Wage: $70,000 a Year',
  118. 'description': 'Dan Price, C.E.O. of Gravity Payments, surprised his 120-person staff by announcing that he planned over the next three years to raise the salary of every employee to $70,000 a year.',
  119. 'timestamp': 1429033037,
  120. 'upload_date': '20150414',
  121. 'uploader': 'Matthew Williams',
  122. }
  123. }, {
  124. 'url': 'http://www.nytimes.com/news/minute/2014/03/17/times-minute-whats-next-in-crimea/?_php=true&_type=blogs&_php=true&_type=blogs&_r=1',
  125. 'only_matching': True,
  126. }]
  127. def _real_extract(self, url):
  128. video_id = self._match_id(url)
  129. webpage = self._download_webpage(url, video_id)
  130. video_id = self._html_search_regex(r'data-videoid="(\d+)"', webpage, 'video id')
  131. return self._extract_video_from_id(video_id)