nosnl.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/((?!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': 'Er werd wel geprobeerd om kwetsbare migranten onderdak te bieden, zegt het COA.',
  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. },
  31. 'playlist_count': 2,
  32. }, {
  33. # audio + video
  34. 'url': 'https://nos.nl/artikel/2440789-wekdienst-16-8-groningse-acties-tien-jaar-na-zware-aardbeving-femke-bol-in-actie-op-ek-atletiek',
  35. 'info_dict': {
  36. 'id': '2440789',
  37. 'title': 'Wekdienst 16/8: Groningse acties tien jaar na zware aardbeving • Femke Bol in actie op EK atletiek ',
  38. 'description': 'Nieuws, weer, verkeer: met dit overzicht begin je geïnformeerd aan de dag.',
  39. 'tags': ['wekdienst'],
  40. 'modified_date': '20220816',
  41. 'modified_timestamp': 1660625449,
  42. 'timestamp': 1660625449,
  43. 'upload_date': '20220816',
  44. 'thumbnail': 'https://cdn.nos.nl/image/2022/08/16/888178/1024x576a.jpg',
  45. },
  46. 'playlist_count': 2,
  47. }
  48. ]
  49. def _entries(self, nextjs_json, display_id):
  50. for item in nextjs_json['items']:
  51. if item.get('type') == 'video':
  52. formats, subtitle = self._extract_m3u8_formats_and_subtitles(
  53. traverse_obj(item, ('source', 'url')), display_id, ext='mp4')
  54. yield {
  55. 'id': str(item['id']),
  56. 'title': item.get('title'),
  57. 'description': item.get('description'),
  58. 'formats': formats,
  59. 'subtitles': subtitle,
  60. 'duration': parse_duration(item.get('duration')),
  61. 'thumbnails': [{
  62. 'url': traverse_obj(image, ('url', ...), get_all=False),
  63. 'width': image.get('width'),
  64. 'height': image.get('height')
  65. } for image in traverse_obj(item, ('imagesByRatio', ...))[0]],
  66. }
  67. elif item.get('type') == 'audio':
  68. yield {
  69. 'id': str(item['id']),
  70. 'title': item.get('title'),
  71. 'url': traverse_obj(item, ('media', 'src')),
  72. 'ext': 'mp3',
  73. }
  74. def _real_extract(self, url):
  75. display_id = self._match_valid_url(url).group('display_id')
  76. webpage = self._download_webpage(url, display_id)
  77. nextjs_json = self._search_nextjs_data(webpage, display_id)['props']['pageProps']['data']
  78. return {
  79. '_type': 'playlist',
  80. 'entries': self._entries(nextjs_json, display_id),
  81. 'id': str(nextjs_json['id']),
  82. 'title': nextjs_json.get('title') or self._html_search_meta(['title', 'og:title', 'twitter:title'], webpage),
  83. 'description': (nextjs_json.get('description')
  84. or self._html_search_meta(['description', 'twitter:description', 'og:description'], webpage)),
  85. 'tags': nextjs_json.get('keywords'),
  86. 'modified_timestamp': parse_iso8601(nextjs_json.get('modifiedAt')),
  87. 'thumbnail': nextjs_json.get('shareImageSrc') or self._html_search_meta(['og:image', 'twitter:image'], webpage),
  88. 'timestamp': parse_iso8601(nextjs_json.get('publishedAt'))
  89. }