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.

94 lines
3.7 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_urllib_request,
  5. compat_urllib_parse,
  6. )
  7. class NFBIE(InfoExtractor):
  8. IE_NAME = 'nfb'
  9. IE_DESC = 'National Film Board of Canada'
  10. _VALID_URL = r'https?://(?:www\.)?(?:nfb|onf)\.ca/film/(?P<id>[\da-z_-]+)'
  11. _TEST = {
  12. 'url': 'https://www.nfb.ca/film/qallunaat_why_white_people_are_funny',
  13. 'info_dict': {
  14. 'id': 'qallunaat_why_white_people_are_funny',
  15. 'ext': 'mp4',
  16. 'title': 'Qallunaat! Why White People Are Funny ',
  17. 'description': 'md5:836d8aff55e087d04d9f6df554d4e038',
  18. 'duration': 3128,
  19. 'uploader': 'Mark Sandiford',
  20. 'uploader_id': 'mark-sandiford',
  21. },
  22. 'params': {
  23. # rtmp download
  24. 'skip_download': True,
  25. }
  26. }
  27. def _real_extract(self, url):
  28. video_id = self._match_id(url)
  29. page = self._download_webpage(
  30. 'https://www.nfb.ca/film/%s' % video_id, video_id,
  31. '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. for asset in media.findall('assets/asset'):
  64. for x in asset:
  65. formats.append({
  66. 'url': x.find('streamerURI').text,
  67. 'app': x.find('streamerURI').text.split('/', 3)[3],
  68. 'play_path': x.find('url').text,
  69. 'rtmp_live': False,
  70. 'ext': 'mp4',
  71. 'format_id': '%s-%s' % (x.tag, asset.get('quality')),
  72. })
  73. return {
  74. 'id': video_id,
  75. 'title': title,
  76. 'description': description,
  77. 'thumbnail': thumbnail,
  78. 'duration': duration,
  79. 'uploader': uploader,
  80. 'uploader_id': uploader_id,
  81. 'formats': formats,
  82. }