]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/newstube.py
[TVer] Fix extractor (#3268)
[yt-dlp.git] / yt_dlp / extractor / newstube.py
CommitLineData
dcdb292f 1# coding: utf-8
b8b01bb9
S
2from __future__ import unicode_literals
3
a2b6f946
RA
4import base64
5import hashlib
b8b01bb9
S
6
7from .common import InfoExtractor
1d3586d0 8from ..aes import aes_cbc_decrypt_bytes, unpad_pkcs7
e962ae15 9from ..utils import (
e962ae15 10 int_or_none,
a2b6f946
RA
11 parse_codecs,
12 parse_duration,
e962ae15 13)
b8b01bb9
S
14
15
16class NewstubeIE(InfoExtractor):
17 _VALID_URL = r'https?://(?:www\.)?newstube\.ru/media/(?P<id>.+)'
18 _TEST = {
1fd01551 19 'url': 'http://www.newstube.ru/media/telekanal-cnn-peremestil-gorod-slavyansk-v-krym',
a2b6f946 20 'md5': '9d10320ad473444352f72f746ccb8b8c',
b8b01bb9 21 'info_dict': {
1fd01551 22 'id': '728e0ef2-e187-4012-bac0-5a081fdcb1f6',
e962ae15 23 'ext': 'mp4',
1fd01551
S
24 'title': 'Телеканал CNN переместил город Славянск в Крым',
25 'description': 'md5:419a8c9f03442bc0b0a794d689360335',
26 'duration': 31.05,
b8b01bb9 27 },
b8b01bb9
S
28 }
29
30 def _real_extract(self, url):
a2b6f946 31 video_id = self._match_id(url)
b8b01bb9 32
a2b6f946
RA
33 page = self._download_webpage(url, video_id)
34 title = self._html_search_meta(['og:title', 'twitter:title'], page, fatal=True)
b8b01bb9
S
35
36 video_guid = self._html_search_regex(
a2b6f946 37 r'<meta\s+property="og:video(?::(?:(?:secure_)?url|iframe))?"\s+content="https?://(?:www\.)?newstube\.ru/embed/(?P<guid>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})',
b8b01bb9
S
38 page, 'video GUID')
39
a2b6f946
RA
40 enc_data = base64.b64decode(self._download_webpage(
41 'https://www.newstube.ru/embed/api/player/getsources2',
42 video_guid, query={
43 'guid': video_guid,
44 'ff': 3,
45 }))
46 key = hashlib.pbkdf2_hmac(
47 'sha1', video_guid.replace('-', '').encode(), enc_data[:16], 1)[:16]
1d3586d0 48 dec_data = unpad_pkcs7(aes_cbc_decrypt_bytes(enc_data[32:], key, enc_data[16:32]))
49 sources = self._parse_json(dec_data, video_guid)
b8b01bb9
S
50
51 formats = []
a2b6f946
RA
52 for source in sources:
53 source_url = source.get('Src')
54 if not source_url:
b8b01bb9 55 continue
a2b6f946
RA
56 height = int_or_none(source.get('Height'))
57 f = {
58 'format_id': 'http' + ('-%dp' % height if height else ''),
59 'url': source_url,
60 'width': int_or_none(source.get('Width')),
b8b01bb9 61 'height': height,
a2b6f946
RA
62 }
63 source_type = source.get('Type')
64 if source_type:
65 f.update(parse_codecs(self._search_regex(
66 r'codecs="([^"]+)"', source_type, 'codecs', fatal=False)))
67 formats.append(f)
e962ae15 68
69 self._check_formats(formats, video_guid)
b8b01bb9
S
70 self._sort_formats(formats)
71
72 return {
73 'id': video_guid,
74 'title': title,
a2b6f946
RA
75 'description': self._html_search_meta(['description', 'og:description'], page),
76 'thumbnail': self._html_search_meta(['og:image:secure_url', 'og:image', 'twitter:image'], page),
77 'duration': parse_duration(self._html_search_meta('duration', page)),
b8b01bb9 78 'formats': formats,
5f6a1245 79 }