Merge pull request #9110 from remitamine/parse_duration
[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         # Covers https://github.com/rg3/youtube-dl/pull/5983
45         'url': 'http://www.liveleak.com/view?i=801_1409392012',
46         'md5': '0b3bec2d888c20728ca2ad3642f0ef15',
47         'info_dict': {
48             'id': '801_1409392012',
49             'ext': 'mp4',
50             'description': 'Happened on 27.7.2014. \r\nAt 0:53 you can see people still swimming at near beach.',
51             'uploader': 'bony333',
52             'title': 'Crazy Hungarian tourist films close call waterspout in Croatia'
53         }
54     }]
55
56     @staticmethod
57     def _extract_url(webpage):
58         mobj = re.search(
59             r'<iframe[^>]+src="https?://(?:\w+\.)?liveleak\.com/ll_embed\?(?:.*?)i=(?P<id>[\w_]+)(?:.*)',
60             webpage)
61         if mobj:
62             return 'http://www.liveleak.com/view?i=%s' % mobj.group('id')
63
64     def _real_extract(self, url):
65         video_id = self._match_id(url)
66         webpage = self._download_webpage(url, video_id)
67
68         video_title = self._og_search_title(webpage).replace('LiveLeak.com -', '').strip()
69         video_description = self._og_search_description(webpage)
70         video_uploader = self._html_search_regex(
71             r'By:.*?(\w+)</a>', webpage, 'uploader', fatal=False)
72         age_limit = int_or_none(self._search_regex(
73             r'you confirm that you are ([0-9]+) years and over.',
74             webpage, 'age limit', default=None))
75
76         sources_raw = self._search_regex(
77             r'(?s)sources:\s*(\[.*?\]),', webpage, 'video URLs', default=None)
78         if sources_raw is None:
79             alt_source = self._search_regex(
80                 r'(file: ".*?"),', webpage, 'video URL', default=None)
81             if alt_source:
82                 sources_raw = '[{ %s}]' % alt_source
83             else:
84                 # Maybe an embed?
85                 embed_url = self._search_regex(
86                     r'<iframe[^>]+src="(http://www.prochan.com/embed\?[^"]+)"',
87                     webpage, 'embed URL')
88                 return {
89                     '_type': 'url_transparent',
90                     'url': embed_url,
91                     'id': video_id,
92                     'title': video_title,
93                     'description': video_description,
94                     'uploader': video_uploader,
95                     'age_limit': age_limit,
96                 }
97
98         sources_json = re.sub(r'\s([a-z]+):\s', r'"\1": ', sources_raw)
99         sources = json.loads(sources_json)
100
101         formats = [{
102             'format_id': '%s' % i,
103             'format_note': s.get('label'),
104             'url': s['file'],
105         } for i, s in enumerate(sources)]
106         for i, s in enumerate(sources):
107             # Removing '.h264_*.mp4' gives the raw video, which is essentially
108             # the same video without the LiveLeak logo at the top (see
109             # https://github.com/rg3/youtube-dl/pull/4768)
110             orig_url = re.sub(r'\.h264_.+?\.mp4', '', s['file'])
111             if s['file'] != orig_url:
112                 formats.append({
113                     'format_id': 'original-%s' % i,
114                     'format_note': s.get('label'),
115                     'url': orig_url,
116                     'preference': 1,
117                 })
118         self._sort_formats(formats)
119
120         return {
121             'id': video_id,
122             'title': video_title,
123             'description': video_description,
124             'uploader': video_uploader,
125             'formats': formats,
126             'age_limit': age_limit,
127         }