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