Merge remote-tracking branch 'duncankl/airmozilla'
[youtube-dl] / youtube_dl / extractor / airmozilla.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import parse_iso8601
8
9
10 class AirMozillaIE(InfoExtractor):
11     _VALID_URL = r'https?://air\.mozilla\.org/(?P<id>[0-9a-z-]+)/?'
12     _TEST = {
13         'url': 'https://air.mozilla.org/privacy-lab-a-meetup-for-privacy-minded-people-in-san-francisco/',
14         'md5': '2e3e7486ba5d180e829d453875b9b8bf',
15         'info_dict': {
16             'id': '6x4q2w',
17             'ext': 'mp4',
18             'title': 'Privacy Lab - a meetup for privacy minded people in San Francisco',
19             'thumbnail': 're:https://\w+\.cloudfront\.net/6x4q2w/poster\.jpg\?t=\d+',
20             'description': 'Brings together privacy professionals and others interested in privacy at for-profits, non-profits, and NGOs in an effort to contribute to the state of the ecosystem...',
21             'timestamp': 1422487800,
22             'upload_date': '20150128',
23             'location': 'SFO Commons',
24             'duration': 3780,
25             'view_count': int,
26             'categories': ['Main'],
27         }
28     }
29
30     _QUALITY_MAP = {
31         '360p': 0,
32         '576p': 1,
33         '640p': 2,
34         '720p': 3,
35     }
36
37     def _real_extract(self, url):
38         display_id = self._match_id(url)
39         webpage = self._download_webpage(url, display_id)
40         video_id = self._html_search_regex(r'//vid.ly/(.*?)/embed', webpage, 'id')
41
42         embed_script = self._download_webpage('https://vid.ly/{0}/embed'.format(video_id), video_id)
43         jwconfig = self._search_regex(r'\svar jwconfig = (\{.*?\});\s', embed_script, 'metadata')
44         metadata = self._parse_json(jwconfig, video_id)
45
46         formats = []
47         for source in metadata['playlist'][0]['sources']:
48             fmt = {
49                 'url': source['file'],
50                 'ext': source['type'],
51                 'format_id': self._search_regex(r'&format=(.*)$', source['file'], 'video format'),
52                 'resolution': source['label'],
53                 'quality': self._QUALITY_MAP.get(source['label'], -1),
54             }
55             formats.append(fmt)
56         self._sort_formats(formats)
57
58         duration_match = re.search(r'Duration:(?: (?P<H>\d+) hours?)?(?: (?P<M>\d+) minutes?)?', webpage)
59
60         return {
61             'id': video_id,
62             'title': self._og_search_title(webpage),
63             'formats': formats,
64             'url': self._og_search_url(webpage),
65             'display_id': display_id,
66             'thumbnail': metadata['playlist'][0]['image'],
67             'description': self._og_search_description(webpage),
68             'timestamp': parse_iso8601(self._html_search_regex(r'<time datetime="(.*?)"', webpage, 'timestamp')),
69             'location': self._html_search_regex(r'Location: (.*)', webpage, 'location', default=None),
70             'duration': int(duration_match.groupdict()['H'] or 0) * 3600 + int(duration_match.groupdict()['M'] or 0) * 60,
71             'view_count': int(self._html_search_regex(r'Views since archived: ([0-9]+)', webpage, 'view count')),
72             'categories': re.findall(r'<a href=".*?" class="channel">(.*?)</a>', webpage),
73         }