nosnl.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from .common import InfoExtractor
  2. from ..utils import parse_duration, parse_iso8601, traverse_obj
  3. class NOSNLArticleIE(InfoExtractor):
  4. _VALID_URL = r'https?://nos\.nl/(?P<type>video|(\w+/)?\w+)/?\d+-(?P<display_id>[\w-]+)'
  5. _TESTS = [
  6. {
  7. # only 1 video
  8. 'url': 'https://nos.nl/nieuwsuur/artikel/2440353-verzakking-door-droogte-dreigt-tot-een-miljoen-kwetsbare-huizen',
  9. 'info_dict': {
  10. 'id': '2440340',
  11. 'ext': 'mp4',
  12. 'description': 'md5:5f83185d902ac97af3af4bed7ece3db5',
  13. 'title': '\'We hebben een huis vol met scheuren\'',
  14. 'duration': 95.0,
  15. 'thumbnail': 'https://cdn.nos.nl/image/2022/08/12/887149/3840x2160a.jpg',
  16. },
  17. }, {
  18. # more than 1 video
  19. 'url': 'https://nos.nl/artikel/2440409-vannacht-sliepen-weer-enkele-honderden-asielzoekers-in-ter-apel-buiten',
  20. 'info_dict': {
  21. 'id': '2440409',
  22. 'title': 'Vannacht sliepen weer enkele honderden asielzoekers in Ter Apel buiten',
  23. 'description': 'md5:72b1e1674d798460e79d78fa37e9f56d',
  24. 'tags': ['aanmeldcentrum', 'Centraal Orgaan opvang asielzoekers', 'COA', 'asielzoekers', 'Ter Apel'],
  25. 'modified_timestamp': 1660452773,
  26. 'modified_date': '20220814',
  27. 'upload_date': '20220813',
  28. 'thumbnail': 'https://cdn.nos.nl/image/2022/07/18/880346/1024x576a.jpg',
  29. 'timestamp': 1660401384,
  30. 'categories': ['Regionaal nieuws', 'Binnenland'],
  31. },
  32. 'playlist_count': 2,
  33. }, {
  34. # audio + video
  35. 'url': 'https://nos.nl/artikel/2440789-wekdienst-16-8-groningse-acties-tien-jaar-na-zware-aardbeving-femke-bol-in-actie-op-ek-atletiek',
  36. 'info_dict': {
  37. 'id': '2440789',
  38. 'title': 'Wekdienst 16/8: Groningse acties tien jaar na zware aardbeving • Femke Bol in actie op EK atletiek ',
  39. 'description': 'md5:0bd277ed7a44fc15cb12a9d27d8f6641',
  40. 'tags': ['wekdienst'],
  41. 'modified_date': '20220816',
  42. 'modified_timestamp': 1660625449,
  43. 'timestamp': 1660625449,
  44. 'upload_date': '20220816',
  45. 'thumbnail': 'https://cdn.nos.nl/image/2022/08/16/888178/1024x576a.jpg',
  46. 'categories': ['Binnenland', 'Buitenland'],
  47. },
  48. 'playlist_count': 2,
  49. }, {
  50. # video url
  51. 'url': 'https://nos.nl/video/2452718-xi-en-trudeau-botsen-voor-de-camera-op-g20-top-je-hebt-gelekt',
  52. 'info_dict': {
  53. 'id': '2452718',
  54. 'title': 'Xi en Trudeau botsen voor de camera op G20-top: \'Je hebt gelekt\'',
  55. 'modified_date': '20221117',
  56. 'description': 'md5:61907dac576f75c11bf8ffffd4a3cc0f',
  57. 'tags': ['Xi', 'Trudeau', 'G20', 'indonesié'],
  58. 'upload_date': '20221117',
  59. 'thumbnail': 'https://cdn.nos.nl/image/2022/11/17/916155/1024x576a.jpg',
  60. 'modified_timestamp': 1668663388,
  61. 'timestamp': 1668663388,
  62. 'categories': ['Buitenland'],
  63. },
  64. 'playlist_mincount': 1,
  65. },
  66. ]
  67. def _entries(self, nextjs_json, display_id):
  68. for item in nextjs_json:
  69. if item.get('type') == 'video':
  70. formats, subtitle = self._extract_m3u8_formats_and_subtitles(
  71. traverse_obj(item, ('source', 'url')), display_id, ext='mp4')
  72. yield {
  73. 'id': str(item['id']),
  74. 'title': item.get('title'),
  75. 'description': item.get('description'),
  76. 'formats': formats,
  77. 'subtitles': subtitle,
  78. 'duration': parse_duration(item.get('duration')),
  79. 'thumbnails': [{
  80. 'url': traverse_obj(image, ('url', ...), get_all=False),
  81. 'width': image.get('width'),
  82. 'height': image.get('height'),
  83. } for image in traverse_obj(item, ('imagesByRatio', ...))[0]],
  84. }
  85. elif item.get('type') == 'audio':
  86. yield {
  87. 'id': str(item['id']),
  88. 'title': item.get('title'),
  89. 'url': traverse_obj(item, ('media', 'src')),
  90. 'ext': 'mp3',
  91. }
  92. def _real_extract(self, url):
  93. site_type, display_id = self._match_valid_url(url).group('type', 'display_id')
  94. webpage = self._download_webpage(url, display_id)
  95. nextjs_json = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['data']
  96. return {
  97. '_type': 'playlist',
  98. 'entries': self._entries(
  99. [nextjs_json['video']] if site_type == 'video' else nextjs_json['items'], display_id),
  100. 'id': str(nextjs_json['id']),
  101. 'title': nextjs_json.get('title') or self._html_search_meta(['title', 'og:title', 'twitter:title'], webpage),
  102. 'description': (nextjs_json.get('description')
  103. or self._html_search_meta(['description', 'twitter:description', 'og:description'], webpage)),
  104. 'tags': nextjs_json.get('keywords'),
  105. 'modified_timestamp': parse_iso8601(nextjs_json.get('modifiedAt')),
  106. 'thumbnail': nextjs_json.get('shareImageSrc') or self._html_search_meta(['og:image', 'twitter:image'], webpage),
  107. 'timestamp': parse_iso8601(nextjs_json.get('publishedAt')),
  108. 'categories': traverse_obj(nextjs_json, ('categories', ..., 'label')),
  109. }