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.

83 lines
2.8 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 NYTimesIE(InfoExtractor):
  9. _VALID_URL = r'https?://(?:(?:www\.)?nytimes\.com/video/(?:[^/]+/)+?|graphics8\.nytimes\.com/bcvideo/\d+(?:\.\d+)?/iframe/embed\.html\?videoId=)(?P<id>\d+)'
  10. _TESTS = [{
  11. 'url': 'http://www.nytimes.com/video/opinion/100000002847155/verbatim-what-is-a-photocopier.html?playlistId=100000001150263',
  12. 'md5': '18a525a510f942ada2720db5f31644c0',
  13. 'info_dict': {
  14. 'id': '100000002847155',
  15. 'ext': 'mov',
  16. 'title': 'Verbatim: What Is a Photocopier?',
  17. 'description': 'md5:93603dada88ddbda9395632fdc5da260',
  18. 'timestamp': 1398631707,
  19. 'upload_date': '20140427',
  20. 'uploader': 'Brett Weiner',
  21. 'duration': 419,
  22. }
  23. }, {
  24. 'url': 'http://www.nytimes.com/video/travel/100000003550828/36-hours-in-dubai.html',
  25. 'only_matching': True,
  26. }]
  27. def _real_extract(self, url):
  28. video_id = self._match_id(url)
  29. video_data = self._download_json(
  30. 'http://www.nytimes.com/svc/video/api/v2/video/%s' % video_id,
  31. video_id, 'Downloading video JSON')
  32. title = video_data['headline']
  33. description = video_data.get('summary')
  34. duration = float_or_none(video_data.get('duration'), 1000)
  35. uploader = video_data['byline']
  36. timestamp = parse_iso8601(video_data['publication_date'][:-8])
  37. def get_file_size(file_size):
  38. if isinstance(file_size, int):
  39. return file_size
  40. elif isinstance(file_size, dict):
  41. return int(file_size.get('value', 0))
  42. else:
  43. return 0
  44. formats = [
  45. {
  46. 'url': video['url'],
  47. 'format_id': video.get('type'),
  48. 'vcodec': video.get('video_codec'),
  49. 'width': int_or_none(video.get('width')),
  50. 'height': int_or_none(video.get('height')),
  51. 'filesize': get_file_size(video.get('fileSize')),
  52. } for video in video_data['renditions']
  53. ]
  54. self._sort_formats(formats)
  55. thumbnails = [
  56. {
  57. 'url': 'http://www.nytimes.com/%s' % image['url'],
  58. 'width': int_or_none(image.get('width')),
  59. 'height': int_or_none(image.get('height')),
  60. } for image in video_data['images']
  61. ]
  62. return {
  63. 'id': video_id,
  64. 'title': title,
  65. 'description': description,
  66. 'timestamp': timestamp,
  67. 'uploader': uploader,
  68. 'duration': duration,
  69. 'formats': formats,
  70. 'thumbnails': thumbnails,
  71. }