Merge remote-tracking branch 'h-collector/master'
[youtube-dl] / youtube_dl / extractor / liveleak.py
1 from __future__ import unicode_literals
2
3 import json
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import int_or_none
8
9
10 class LiveLeakIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:\w+\.)?liveleak\.com/view\?(?:.*?)i=(?P<id>[\w_]+)(?:.*)'
12     _TESTS = [{
13         'url': 'http://www.liveleak.com/view?i=757_1364311680',
14         'md5': '50f79e05ba149149c1b4ea961223d5b3',
15         'info_dict': {
16             'id': '757_1364311680',
17             'ext': 'flv',
18             'description': 'extremely bad day for this guy..!',
19             'uploader': 'ljfriel2',
20             'title': 'Most unlucky car accident'
21         }
22     }, {
23         'url': 'http://www.liveleak.com/view?i=f93_1390833151',
24         'md5': 'b13a29626183c9d33944e6a04f41aafc',
25         'info_dict': {
26             'id': 'f93_1390833151',
27             'ext': 'mp4',
28             'description': 'German Television Channel NDR does an exclusive interview with Edward Snowden.\r\nUploaded on LiveLeak cause German Television thinks the rest of the world isn\'t intereseted in Edward Snowden.',
29             'uploader': 'ARD_Stinkt',
30             'title': 'German Television does first Edward Snowden Interview (ENGLISH)',
31         }
32     }, {
33         'url': 'http://www.liveleak.com/view?i=4f7_1392687779',
34         'md5': '42c6d97d54f1db107958760788c5f48f',
35         'info_dict': {
36             'id': '4f7_1392687779',
37             'ext': 'mp4',
38             'description': "The guy with the cigarette seems amazingly nonchalant about the whole thing...  I really hope my friends' reactions would be a bit stronger.\r\n\r\nAction-go to 0:55.",
39             'uploader': 'CapObveus',
40             'title': 'Man is Fatally Struck by Reckless Car While Packing up a Moving Truck',
41             'age_limit': 18,
42         }
43     }]
44
45     def _real_extract(self, url):
46         video_id = self._match_id(url)
47         webpage = self._download_webpage(url, video_id)
48
49         video_title = self._og_search_title(webpage).replace('LiveLeak.com -', '').strip()
50         video_description = self._og_search_description(webpage)
51         video_uploader = self._html_search_regex(
52             r'By:.*?(\w+)</a>', webpage, 'uploader', fatal=False)
53         age_limit = int_or_none(self._search_regex(
54             r'you confirm that you are ([0-9]+) years and over.',
55             webpage, 'age limit', default=None))
56
57         sources_raw = self._search_regex(
58             r'(?s)sources:\s*(\[.*?\]),', webpage, 'video URLs', default=None)
59         if sources_raw is None:
60             alt_source = self._search_regex(
61                 r'(file: ".*?"),', webpage, 'video URL', default=None)
62             if alt_source:
63                 sources_raw = '[{ %s}]' % alt_source
64             else:
65                 # Maybe an embed?
66                 embed_url = self._search_regex(
67                     r'<iframe[^>]+src="(http://www.prochan.com/embed\?[^"]+)"',
68                     webpage, 'embed URL')
69                 return {
70                     '_type': 'url_transparent',
71                     'url': embed_url,
72                     'id': video_id,
73                     'title': video_title,
74                     'description': video_description,
75                     'uploader': video_uploader,
76                     'age_limit': age_limit,
77                 }
78
79         sources_json = re.sub(r'\s([a-z]+):\s', r'"\1": ', sources_raw)
80         sources = json.loads(sources_json)
81
82         formats = [{
83             'format_id': '%s' % i,
84             'format_note': s.get('label'),
85             'url': s['file'],
86         } for i, s in enumerate(sources)]
87         for i, s in enumerate(sources):
88             orig_url = s['file'].replace('.h264_base.mp4', '')
89             if s['file'] != orig_url:
90                 formats.append({
91                     'format_id': 'original-%s' % i,
92                     'format_note': s.get('label'),
93                     'url': orig_url,
94                     'preference': 1,
95                 })
96         self._sort_formats(formats)
97
98         return {
99             'id': video_id,
100             'title': video_title,
101             'description': video_description,
102             'uploader': video_uploader,
103             'formats': formats,
104             'age_limit': age_limit,
105         }