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.

92 lines
3.4 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_urllib_request,
  6. compat_urllib_parse,
  7. )
  8. class NFBIE(InfoExtractor):
  9. IE_NAME = 'nfb'
  10. IE_DESC = 'National Film Board of Canada'
  11. _VALID_URL = r'https?://(?:www\.)?(nfb|onf)\.ca/film/(?P<id>[\da-z_-]+)'
  12. _TEST = {
  13. 'url': 'https://www.nfb.ca/film/qallunaat_why_white_people_are_funny',
  14. 'info_dict': {
  15. 'id': 'qallunaat_why_white_people_are_funny',
  16. 'ext': 'mp4',
  17. 'title': 'Qallunaat! Why White People Are Funny ',
  18. 'description': 'md5:836d8aff55e087d04d9f6df554d4e038',
  19. 'duration': 3128,
  20. 'uploader': 'Mark Sandiford',
  21. 'uploader_id': 'mark-sandiford',
  22. },
  23. 'params': {
  24. # rtmp download
  25. 'skip_download': True,
  26. }
  27. }
  28. def _real_extract(self, url):
  29. mobj = re.match(self._VALID_URL, url)
  30. video_id = mobj.group('id')
  31. page = self._download_webpage('https://www.nfb.ca/film/%s' % video_id, video_id, 'Downloading film page')
  32. uploader_id = self._html_search_regex(r'<a class="director-link" href="/explore-all-directors/([^/]+)/"',
  33. page, 'director id', fatal=False)
  34. uploader = self._html_search_regex(r'<em class="director-name" itemprop="name">([^<]+)</em>',
  35. page, 'director name', fatal=False)
  36. request = compat_urllib_request.Request('https://www.nfb.ca/film/%s/player_config' % video_id,
  37. compat_urllib_parse.urlencode({'getConfig': 'true'}).encode('ascii'))
  38. request.add_header('Content-Type', 'application/x-www-form-urlencoded')
  39. request.add_header('X-NFB-Referer', 'http://www.nfb.ca/medias/flash/NFBVideoPlayer.swf')
  40. config = self._download_xml(request, video_id, 'Downloading player config XML')
  41. title = None
  42. description = None
  43. thumbnail = None
  44. duration = None
  45. formats = []
  46. def extract_thumbnail(media):
  47. thumbnails = {}
  48. for asset in media.findall('assets/asset'):
  49. thumbnails[asset.get('quality')] = asset.find('default/url').text
  50. if not thumbnails:
  51. return None
  52. if 'high' in thumbnails:
  53. return thumbnails['high']
  54. return list(thumbnails.values())[0]
  55. for media in config.findall('./player/stream/media'):
  56. if media.get('type') == 'posterImage':
  57. thumbnail = extract_thumbnail(media)
  58. elif media.get('type') == 'video':
  59. duration = int(media.get('duration'))
  60. title = media.find('title').text
  61. description = media.find('description').text
  62. # It seems assets always go from lower to better quality, so no need to sort
  63. formats = [{
  64. 'url': x.find('default/streamerURI').text + '/',
  65. 'play_path': x.find('default/url').text,
  66. 'rtmp_live': False,
  67. 'ext': 'mp4',
  68. 'format_id': x.get('quality'),
  69. } for x in media.findall('assets/asset')]
  70. return {
  71. 'id': video_id,
  72. 'title': title,
  73. 'description': description,
  74. 'thumbnail': thumbnail,
  75. 'duration': duration,
  76. 'uploader': uploader,
  77. 'uploader_id': uploader_id,
  78. 'formats': formats,
  79. }