]> git.bitcoin.ninja Git - youtube-dl/blob - youtube_dl/extractor/nfb.py
Merge branch 'ustream-embed-recorded2' of https://github.com/anovicecodemonkey/youtub...
[youtube-dl] / 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         title = None
53         description = None
54         thumbnail = None
55         duration = None
56         formats = []
57
58         def extract_thumbnail(media):
59             thumbnails = {}
60             for asset in media.findall('assets/asset'):
61                 thumbnails[asset.get('quality')] = asset.find('default/url').text
62             if not thumbnails:
63                 return None
64             if 'high' in thumbnails:
65                 return thumbnails['high']
66             return list(thumbnails.values())[0]
67
68         for media in config.findall('./player/stream/media'):
69             if media.get('type') == 'posterImage':
70                 thumbnail = extract_thumbnail(media)
71             elif media.get('type') == 'video':
72                 duration = int(media.get('duration'))
73                 title = media.find('title').text
74                 description = media.find('description').text
75                 # It seems assets always go from lower to better quality, so no need to sort
76                 for asset in media.findall('assets/asset'):
77                     for x in asset:
78                         formats.append({
79                             'url': x.find('streamerURI').text,
80                             'app': x.find('streamerURI').text.split('/', 3)[3],
81                             'play_path': x.find('url').text,
82                             'rtmp_live': False,
83                             'ext': 'mp4',
84                             'format_id': '%s-%s' % (x.tag, asset.get('quality')),
85                         })
86
87         return {
88             'id': video_id,
89             'title': title,
90             'description': description,
91             'thumbnail': thumbnail,
92             'duration': duration,
93             'uploader': uploader,
94             'uploader_id': uploader_id,
95             'formats': formats,
96         }