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.

117 lines
4.2 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..utils import (
  4. float_or_none,
  5. int_or_none,
  6. parse_iso8601,
  7. )
  8. class NYTimesBaseIE(InfoExtractor):
  9. def _extract_video_from_id(self, video_id):
  10. video_data = self._download_json(
  11. 'http://www.nytimes.com/svc/video/api/v2/video/%s' % video_id,
  12. video_id, 'Downloading video JSON')
  13. title = video_data['headline']
  14. description = video_data.get('summary')
  15. duration = float_or_none(video_data.get('duration'), 1000)
  16. uploader = video_data['byline']
  17. timestamp = parse_iso8601(video_data['publication_date'][:-8])
  18. def get_file_size(file_size):
  19. if isinstance(file_size, int):
  20. return file_size
  21. elif isinstance(file_size, dict):
  22. return int(file_size.get('value', 0))
  23. else:
  24. return 0
  25. formats = [
  26. {
  27. 'url': video['url'],
  28. 'format_id': video.get('type'),
  29. 'vcodec': video.get('video_codec'),
  30. 'width': int_or_none(video.get('width')),
  31. 'height': int_or_none(video.get('height')),
  32. 'filesize': get_file_size(video.get('fileSize')),
  33. } for video in video_data['renditions']
  34. ]
  35. self._sort_formats(formats)
  36. thumbnails = [
  37. {
  38. 'url': 'http://www.nytimes.com/%s' % image['url'],
  39. 'width': int_or_none(image.get('width')),
  40. 'height': int_or_none(image.get('height')),
  41. } for image in video_data['images']
  42. ]
  43. return {
  44. 'id': video_id,
  45. 'title': title,
  46. 'description': description,
  47. 'timestamp': timestamp,
  48. 'uploader': uploader,
  49. 'duration': duration,
  50. 'formats': formats,
  51. 'thumbnails': thumbnails,
  52. }
  53. class NYTimesIE(NYTimesBaseIE):
  54. _VALID_URL = r'https?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P<id>\d+)'
  55. _TESTS = [{
  56. 'url': 'http://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263',
  57. 'md5': '18a525a510f942ada2720db5f31644c0',
  58. 'info_dict': {
  59. 'id': '100000002847155',
  60. 'ext': 'mov',
  61. 'title': 'Verbatim: What Is a Photocopier?',
  62. 'description': 'md5:93603dada88ddbda9395632fdc5da260',
  63. 'timestamp': 1398631707,
  64. 'upload_date': '20140427',
  65. 'uploader': 'Brett Weiner',
  66. 'duration': 419,
  67. }
  68. }, {
  69. 'url': 'http://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.html',
  70. 'only_matching': True,
  71. }]
  72. def _real_extract(self, url):
  73. video_id = self._match_id(url)
  74. return self._extract_video_from_id(video_id)
  75. class NYTimesArticleIE(NYTimesBaseIE):
  76. _VALID_URL = r'https?://(?:www)?\.nytimes\.com/(.(?<!video))*?/(?:[^/]+/)*(?P<id>[^.]+)(?:\.html)?'
  77. _TESTS = [{
  78. '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',
  79. 'md5': 'e2076d58b4da18e6a001d53fd56db3c9',
  80. 'info_dict': {
  81. 'id': '100000003628438',
  82. 'ext': 'mov',
  83. 'title': 'New Minimum Wage: $70,000 a Year',
  84. '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.',
  85. 'timestamp': 1429033037,
  86. 'upload_date': '20150414',
  87. 'uploader': 'Matthew Williams',
  88. }
  89. }, {
  90. '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',
  91. 'only_matching': True,
  92. }]
  93. def _real_extract(self, url):
  94. video_id = self._match_id(url)
  95. webpage = self._download_webpage(url, video_id)
  96. video_id = self._html_search_regex(r'data-videoid="(\d+)"', webpage, 'video id')
  97. return self._extract_video_from_id(video_id)