]> Raphaël G. Git Repositories - youtubedl/blob - youtube_dl/extractor/lifenews.py
Imported Upstream version 2016.06.25
[youtubedl] / 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 ExtractorError,
11 int_or_none,
12 parse_iso8601,
13 remove_end,
14 )
15
16
17 class LifeNewsIE(InfoExtractor):
18 IE_NAME = 'life'
19 IE_DESC = 'Life.ru'
20 _VALID_URL = r'https?://life\.ru/t/[^/]+/(?P<id>\d+)'
21
22 _TESTS = [{
23 # single video embedded via video/source
24 'url': 'https://life.ru/t/новости/98736',
25 'md5': '77c95eaefaca216e32a76a343ad89d23',
26 'info_dict': {
27 'id': '98736',
28 'ext': 'mp4',
29 'title': 'Мужчина нашел дома архив оборонного завода',
30 'description': 'md5:3b06b1b39b5e2bea548e403d99b8bf26',
31 'timestamp': 1344154740,
32 'upload_date': '20120805',
33 'view_count': int,
34 }
35 }, {
36 # single video embedded via iframe
37 'url': 'https://life.ru/t/новости/152125',
38 'md5': '77d19a6f0886cd76bdbf44b4d971a273',
39 'info_dict': {
40 'id': '152125',
41 'ext': 'mp4',
42 'title': 'В Сети появилось видео захвата «Правым сектором» колхозных полей ',
43 'description': 'Жители двух поселков Днепропетровской области не простили радикалам угрозу лишения плодородных земель и пошли в лобовую. ',
44 'timestamp': 1427961840,
45 'upload_date': '20150402',
46 'view_count': int,
47 }
48 }, {
49 # two videos embedded via iframe
50 'url': 'https://life.ru/t/новости/153461',
51 'info_dict': {
52 'id': '153461',
53 'title': 'В Москве спасли потерявшегося медвежонка, который спрятался на дереве',
54 'description': 'Маленький хищник не смог найти дорогу домой и обрел временное убежище на тополе недалеко от жилого массива, пока его не нашла соседская собака.',
55 'timestamp': 1430825520,
56 'view_count': int,
57 },
58 'playlist': [{
59 'md5': '9b6ef8bc0ffa25aebc8bdb40d89ab795',
60 'info_dict': {
61 'id': '153461-video1',
62 'ext': 'mp4',
63 'title': 'В Москве спасли потерявшегося медвежонка, который спрятался на дереве (Видео 1)',
64 'description': 'Маленький хищник не смог найти дорогу домой и обрел временное убежище на тополе недалеко от жилого массива, пока его не нашла соседская собака.',
65 'timestamp': 1430825520,
66 'upload_date': '20150505',
67 },
68 }, {
69 'md5': 'ebb3bf3b1ce40e878d0d628e93eb0322',
70 'info_dict': {
71 'id': '153461-video2',
72 'ext': 'mp4',
73 'title': 'В Москве спасли потерявшегося медвежонка, который спрятался на дереве (Видео 2)',
74 'description': 'Маленький хищник не смог найти дорогу домой и обрел временное убежище на тополе недалеко от жилого массива, пока его не нашла соседская собака.',
75 'timestamp': 1430825520,
76 'upload_date': '20150505',
77 },
78 }],
79 }, {
80 'url': 'https://life.ru/t/новости/213035',
81 'only_matching': True,
82 }, {
83 'url': 'https://life.ru/t/%D0%BD%D0%BE%D0%B2%D0%BE%D1%81%D1%82%D0%B8/153461',
84 'only_matching': True,
85 }, {
86 'url': 'https://life.ru/t/новости/411489/manuel_vals_nazval_frantsiiu_tsieliu_nomier_odin_dlia_ighil',
87 'only_matching': True,
88 }]
89
90 def _real_extract(self, url):
91 video_id = self._match_id(url)
92
93 webpage = self._download_webpage(url, video_id)
94
95 video_urls = re.findall(
96 r'<video[^>]+><source[^>]+src=["\'](.+?)["\']', webpage)
97
98 iframe_links = re.findall(
99 r'<iframe[^>]+src=["\']((?:https?:)?//embed\.life\.ru/embed/.+?)["\']',
100 webpage)
101
102 if not video_urls and not iframe_links:
103 raise ExtractorError('No media links available for %s' % video_id)
104
105 title = remove_end(
106 self._og_search_title(webpage),
107 ' - Life.ru')
108
109 description = self._og_search_description(webpage)
110
111 view_count = self._html_search_regex(
112 r'<div[^>]+class=(["\']).*?\bhits-count\b.*?\1[^>]*>\s*(?P<value>\d+)\s*</div>',
113 webpage, 'view count', fatal=False, group='value')
114
115 timestamp = parse_iso8601(self._search_regex(
116 r'<time[^>]+datetime=(["\'])(?P<value>.+?)\1',
117 webpage, 'upload date', fatal=False, group='value'))
118
119 common_info = {
120 'description': description,
121 'view_count': int_or_none(view_count),
122 'timestamp': timestamp,
123 }
124
125 def make_entry(video_id, video_url, index=None):
126 cur_info = dict(common_info)
127 cur_info.update({
128 'id': video_id if not index else '%s-video%s' % (video_id, index),
129 'url': video_url,
130 'title': title if not index else '%s (Видео %s)' % (title, index),
131 })
132 return cur_info
133
134 def make_video_entry(video_id, video_url, index=None):
135 video_url = compat_urlparse.urljoin(url, video_url)
136 return make_entry(video_id, video_url, index)
137
138 def make_iframe_entry(video_id, video_url, index=None):
139 video_url = self._proto_relative_url(video_url, 'http:')
140 cur_info = make_entry(video_id, video_url, index)
141 cur_info['_type'] = 'url_transparent'
142 return cur_info
143
144 if len(video_urls) == 1 and not iframe_links:
145 return make_video_entry(video_id, video_urls[0])
146
147 if len(iframe_links) == 1 and not video_urls:
148 return make_iframe_entry(video_id, iframe_links[0])
149
150 entries = []
151
152 if video_urls:
153 for num, video_url in enumerate(video_urls, 1):
154 entries.append(make_video_entry(video_id, video_url, num))
155
156 if iframe_links:
157 for num, iframe_link in enumerate(iframe_links, len(video_urls) + 1):
158 entries.append(make_iframe_entry(video_id, iframe_link, num))
159
160 playlist = common_info.copy()
161 playlist.update(self.playlist_result(entries, video_id, title, description))
162 return playlist
163
164
165 class LifeEmbedIE(InfoExtractor):
166 IE_NAME = 'life:embed'
167 _VALID_URL = r'https?://embed\.life\.ru/embed/(?P<id>[\da-f]{32})'
168
169 _TEST = {
170 'url': 'http://embed.life.ru/embed/e50c2dec2867350528e2574c899b8291',
171 'md5': 'b889715c9e49cb1981281d0e5458fbbe',
172 'info_dict': {
173 'id': 'e50c2dec2867350528e2574c899b8291',
174 'ext': 'mp4',
175 'title': 'e50c2dec2867350528e2574c899b8291',
176 'thumbnail': 're:http://.*\.jpg',
177 }
178 }
179
180 def _real_extract(self, url):
181 video_id = self._match_id(url)
182
183 webpage = self._download_webpage(url, video_id)
184
185 formats = []
186 for video_url in re.findall(r'"file"\s*:\s*"([^"]+)', webpage):
187 video_url = compat_urlparse.urljoin(url, video_url)
188 ext = determine_ext(video_url)
189 if ext == 'm3u8':
190 formats.extend(self._extract_m3u8_formats(
191 video_url, video_id, 'mp4',
192 entry_protocol='m3u8_native', m3u8_id='m3u8'))
193 else:
194 formats.append({
195 'url': video_url,
196 'format_id': ext,
197 'preference': 1,
198 })
199 self._sort_formats(formats)
200
201 thumbnail = self._search_regex(
202 r'"image"\s*:\s*"([^"]+)', webpage, 'thumbnail', default=None)
203
204 return {
205 'id': video_id,
206 'title': video_id,
207 'thumbnail': thumbnail,
208 'formats': formats,
209 }