Merge branch 'master' of https://github.com/aurium/youtube-dl into aurium-master
[youtube-dl] / youtube_dl / extractor / lifenews.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urlparse
8 from ..utils import (
9     determine_ext,
10     int_or_none,
11     remove_end,
12     unified_strdate,
13     ExtractorError,
14 )
15
16
17 class LifeNewsIE(InfoExtractor):
18     IE_NAME = 'lifenews'
19     IE_DESC = 'LIFE | NEWS'
20     _VALID_URL = r'http://lifenews\.ru/(?:mobile/)?(?P<section>news|video)/(?P<id>\d+)'
21
22     _TESTS = [{
23         'url': 'http://lifenews.ru/news/126342',
24         'md5': 'e1b50a5c5fb98a6a544250f2e0db570a',
25         'info_dict': {
26             'id': '126342',
27             'ext': 'mp4',
28             'title': 'МВД разыскивает мужчин, оставивших в IKEA сумку с автоматом',
29             'description': 'Камеры наблюдения гипермаркета зафиксировали троих мужчин, спрятавших оружейный арсенал в камере хранения.',
30             'thumbnail': 're:http://.*\.jpg',
31             'upload_date': '20140130',
32         }
33     }, {
34         # video in <iframe>
35         'url': 'http://lifenews.ru/news/152125',
36         'md5': '77d19a6f0886cd76bdbf44b4d971a273',
37         'info_dict': {
38             'id': '152125',
39             'ext': 'mp4',
40             'title': 'В Сети появилось видео захвата «Правым сектором» колхозных полей ',
41             'description': 'Жители двух поселков Днепропетровской области не простили радикалам угрозу лишения плодородных земель и пошли в лобовую. ',
42             'upload_date': '20150402',
43         }
44     }, {
45         'url': 'http://lifenews.ru/news/153461',
46         'md5': '9b6ef8bc0ffa25aebc8bdb40d89ab795',
47         'info_dict': {
48             'id': '153461',
49             'ext': 'mp4',
50             'title': 'В Москве спасли потерявшегося медвежонка, который спрятался на дереве',
51             'description': 'Маленький хищник не смог найти дорогу домой и обрел временное убежище на тополе недалеко от жилого массива, пока его не нашла соседская собака.',
52             'upload_date': '20150505',
53         }
54     }, {
55         'url': 'http://lifenews.ru/video/13035',
56         'only_matching': True,
57     }]
58
59     def _real_extract(self, url):
60         mobj = re.match(self._VALID_URL, url)
61         video_id = mobj.group('id')
62         section = mobj.group('section')
63
64         webpage = self._download_webpage(
65             'http://lifenews.ru/%s/%s' % (section, video_id),
66             video_id, 'Downloading page')
67
68         videos = re.findall(r'<video.*?poster="(?P<poster>[^"]+)".*?src="(?P<video>[^"]+)".*?></video>', webpage)
69         iframe_link = self._html_search_regex(
70             '<iframe[^>]+src=["\']([^"\']+)["\']', webpage, 'iframe link', default=None)
71         if not videos and not iframe_link:
72             raise ExtractorError('No media links available for %s' % video_id)
73
74         title = remove_end(
75             self._og_search_title(webpage),
76             ' - Первый по срочным новостям — LIFE | NEWS')
77
78         description = self._og_search_description(webpage)
79
80         view_count = self._html_search_regex(
81             r'<div class=\'views\'>\s*(\d+)\s*</div>', webpage, 'view count', fatal=False)
82         comment_count = self._html_search_regex(
83             r'=\'commentCount\'[^>]*>\s*(\d+)\s*<',
84             webpage, 'comment count', fatal=False)
85
86         upload_date = self._html_search_regex(
87             r'<time[^>]*datetime=\'([^\']+)\'', webpage, 'upload date', fatal=False)
88         if upload_date is not None:
89             upload_date = unified_strdate(upload_date)
90
91         common_info = {
92             'description': description,
93             'view_count': int_or_none(view_count),
94             'comment_count': int_or_none(comment_count),
95             'upload_date': upload_date,
96         }
97
98         def make_entry(video_id, media, video_number=None):
99             cur_info = dict(common_info)
100             cur_info.update({
101                 'id': video_id,
102                 'url': media[1],
103                 'thumbnail': media[0],
104                 'title': title if video_number is None else '%s-video%s' % (title, video_number),
105             })
106             return cur_info
107
108         if iframe_link:
109             iframe_link = self._proto_relative_url(iframe_link, 'http:')
110             cur_info = dict(common_info)
111             cur_info.update({
112                 '_type': 'url_transparent',
113                 'id': video_id,
114                 'title': title,
115                 'url': iframe_link,
116             })
117             return cur_info
118
119         if len(videos) == 1:
120             return make_entry(video_id, videos[0])
121         else:
122             return [make_entry(video_id, media, video_number + 1) for video_number, media in enumerate(videos)]
123
124
125 class LifeEmbedIE(InfoExtractor):
126     IE_NAME = 'life:embed'
127     _VALID_URL = r'http://embed\.life\.ru/embed/(?P<id>[\da-f]{32})'
128
129     _TEST = {
130         'url': 'http://embed.life.ru/embed/e50c2dec2867350528e2574c899b8291',
131         'md5': 'b889715c9e49cb1981281d0e5458fbbe',
132         'info_dict': {
133             'id': 'e50c2dec2867350528e2574c899b8291',
134             'ext': 'mp4',
135             'title': 'e50c2dec2867350528e2574c899b8291',
136             'thumbnail': 're:http://.*\.jpg',
137         }
138     }
139
140     def _real_extract(self, url):
141         video_id = self._match_id(url)
142
143         webpage = self._download_webpage(url, video_id)
144
145         formats = []
146         for video_url in re.findall(r'"file"\s*:\s*"([^"]+)', webpage):
147             video_url = compat_urlparse.urljoin(url, video_url)
148             ext = determine_ext(video_url)
149             if ext == 'm3u8':
150                 formats.extend(self._extract_m3u8_formats(
151                     video_url, video_id, 'mp4', m3u8_id='m3u8'))
152             else:
153                 formats.append({
154                     'url': video_url,
155                     'format_id': ext,
156                     'preference': 1,
157                 })
158         self._sort_formats(formats)
159
160         thumbnail = self._search_regex(
161             r'"image"\s*:\s*"([^"]+)', webpage, 'thumbnail', default=None)
162
163         return {
164             'id': video_id,
165             'title': video_id,
166             'thumbnail': thumbnail,
167             'formats': formats,
168         }