Merge pull request #7691 from ryandesign/use-PYTHON-env-var
[youtube-dl] / youtube_dl / extractor / theonion.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8
9 class TheOnionIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?theonion\.com/video/[^,]+,(?P<id>[0-9]+)/?'
11     _TEST = {
12         'url': 'http://www.theonion.com/video/man-wearing-mm-jacket-gods-image,36918/',
13         'md5': '19eaa9a39cf9b9804d982e654dc791ee',
14         'info_dict': {
15             'id': '2133',
16             'ext': 'mp4',
17             'title': 'Man Wearing M&M Jacket Apparently Made In God\'s Image',
18             'description': 'md5:cc12448686b5600baae9261d3e180910',
19             'thumbnail': 're:^https?://.*\.jpg\?\d+$',
20         }
21     }
22
23     def _real_extract(self, url):
24         display_id = self._match_id(url)
25         webpage = self._download_webpage(url, display_id)
26
27         video_id = self._search_regex(
28             r'"videoId":\s(\d+),', webpage, 'video ID')
29         title = self._og_search_title(webpage)
30         description = self._og_search_description(webpage)
31         thumbnail = self._og_search_thumbnail(webpage)
32
33         sources = re.findall(r'<source src="([^"]+)" type="([^"]+)"', webpage)
34         formats = []
35         for src, type_ in sources:
36             if type_ == 'video/mp4':
37                 formats.append({
38                     'format_id': 'mp4_sd',
39                     'preference': 1,
40                     'url': src,
41                 })
42             elif type_ == 'video/webm':
43                 formats.append({
44                     'format_id': 'webm_sd',
45                     'preference': 0,
46                     'url': src,
47                 })
48             elif type_ == 'application/x-mpegURL':
49                 formats.extend(
50                     self._extract_m3u8_formats(src, display_id, preference=-1))
51             else:
52                 self.report_warning(
53                     'Encountered unexpected format: %s' % type_)
54         self._sort_formats(formats)
55
56         return {
57             'id': video_id,
58             'display_id': display_id,
59             'title': title,
60             'formats': formats,
61             'thumbnail': thumbnail,
62             'description': description,
63         }