]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/theonion.py
b65d8e03f7741a712001099c601ee354830a74a1
[youtubedl] / 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 from ..utils import ExtractorError
8
9
10 class TheOnionIE(InfoExtractor):
11 _VALID_URL = r'(?x)https?://(?:www\.)?theonion\.com/video/[^,]+,(?P<article_id>[0-9]+)/?'
12 _TEST = {
13 'url': 'http://www.theonion.com/video/man-wearing-mm-jacket-gods-image,36918/',
14 'md5': '19eaa9a39cf9b9804d982e654dc791ee',
15 'info_dict': {
16 'id': '2133',
17 'ext': 'mp4',
18 'title': 'Man Wearing M&M Jacket Apparently Made In God\'s Image',
19 'description': 'md5:cc12448686b5600baae9261d3e180910',
20 'thumbnail': 're:^https?://.*\.jpg\?\d+$',
21 }
22 }
23
24 def _real_extract(self, url):
25 mobj = re.match(self._VALID_URL, url)
26 article_id = mobj.group('article_id')
27
28 webpage = self._download_webpage(url, article_id)
29
30 video_id = self._search_regex(
31 r'"videoId":\s(\d+),', webpage, 'video ID')
32 title = self._og_search_title(webpage)
33 description = self._og_search_description(webpage)
34 thumbnail = self._og_search_thumbnail(webpage)
35
36 sources = re.findall(r'<source src="([^"]+)" type="([^"]+)"', webpage)
37 if not sources:
38 raise ExtractorError(
39 'No sources found for video %s' % video_id, expected=True)
40
41 formats = []
42 for src, type_ in sources:
43 if type_ == 'video/mp4':
44 formats.append({
45 'format_id': 'mp4_sd',
46 'preference': 1,
47 'url': src,
48 })
49 elif type_ == 'video/webm':
50 formats.append({
51 'format_id': 'webm_sd',
52 'preference': 0,
53 'url': src,
54 })
55 elif type_ == 'application/x-mpegURL':
56 formats.extend(
57 self._extract_m3u8_formats(src, video_id, preference=-1))
58 else:
59 self.report_warning(
60 'Encountered unexpected format: %s' % type_)
61
62 self._sort_formats(formats)
63
64 return {
65 'id': video_id,
66 'title': title,
67 'formats': formats,
68 'thumbnail': thumbnail,
69 'description': description,
70 }