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.

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