1
0
mirror of https://github.com/l1ving/youtube-dl synced 2024-11-22 02:02:53 +08:00
youtube-dl/youtube_dl/extractor/streamcz.py

99 lines
3.2 KiB
Python
Raw Normal View History

2014-02-10 01:37:12 +08:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import hashlib
import time
2014-02-10 01:37:12 +08:00
from .common import InfoExtractor
from ..compat import (
compat_urllib_request,
)
2014-05-25 06:30:15 +08:00
from ..utils import (
int_or_none,
)
2014-02-10 01:37:12 +08:00
def _get_api_key(api_path):
if api_path.endswith('?'):
api_path = api_path[:-1]
api_key = 'fb5f58a820353bd7095de526253c14fd'
a = '{0:}{1:}{2:}'.format(api_key, api_path, int(round(time.time() / 24 / 3600)))
return hashlib.md5(a.encode('ascii')).hexdigest()
2014-02-10 01:37:12 +08:00
class StreamCZIE(InfoExtractor):
2014-12-13 19:14:44 +08:00
_VALID_URL = r'https?://(?:www\.)?stream\.cz/.+/(?P<id>[0-9]+)'
_API_URL = 'http://www.stream.cz/API'
2014-02-10 01:37:12 +08:00
2014-05-24 22:01:37 +08:00
_TESTS = [{
2014-02-10 01:37:12 +08:00
'url': 'http://www.stream.cz/peklonataliri/765767-ecka-pro-deti',
'md5': '6d3ca61a8d0633c9c542b92fcb936b0c',
'info_dict': {
'id': '765767',
'ext': 'mp4',
'title': 'Peklo na talíři: Éčka pro děti',
2014-12-13 19:14:44 +08:00
'description': 'Taška s grónskou pomazánkou a další pekelnosti ZDE',
'thumbnail': 're:^http://im.stream.cz/episode/52961d7e19d423f8f06f0100',
2014-02-11 11:19:02 +08:00
'duration': 256,
2014-02-10 01:37:12 +08:00
},
2014-05-24 22:01:37 +08:00
}, {
2014-05-25 06:32:19 +08:00
'url': 'http://www.stream.cz/blanik/10002447-tri-roky-pro-mazanka',
2014-12-13 19:14:44 +08:00
'md5': 'e54a254fb8b871968fd8403255f28589',
2014-05-24 22:01:37 +08:00
'info_dict': {
'id': '10002447',
'ext': 'mp4',
'title': 'Kancelář Blaník: Tři roky pro Mazánka',
2014-12-13 19:14:44 +08:00
'description': 'md5:3862a00ba7bf0b3e44806b544032c859',
'thumbnail': 're:^http://im.stream.cz/episode/537f838c50c11f8d21320000',
2014-05-24 22:01:37 +08:00
'duration': 368,
},
}]
2014-02-10 01:37:12 +08:00
def _real_extract(self, url):
2014-12-13 19:14:44 +08:00
video_id = self._match_id(url)
api_path = '/episode/%s' % video_id
req = compat_urllib_request.Request(self._API_URL + api_path)
req.add_header('Api-Password', _get_api_key(api_path))
data = self._download_json(req, video_id)
2014-02-10 01:37:12 +08:00
formats = []
2014-12-13 19:14:44 +08:00
for quality, video in enumerate(data['video_qualities']):
for f in video['formats']:
typ = f['type'].partition('/')[2]
qlabel = video.get('quality_label')
2014-02-11 11:19:02 +08:00
formats.append({
2014-12-13 19:14:44 +08:00
'format_note': '%s-%s' % (qlabel, typ) if qlabel else typ,
'format_id': '%s-%s' % (typ, f['quality']),
'url': f['source'],
'height': int_or_none(f['quality'].rstrip('p')),
2014-02-11 11:19:02 +08:00
'quality': quality,
})
2014-02-10 01:37:12 +08:00
self._sort_formats(formats)
2014-12-13 19:14:44 +08:00
image = data.get('image')
if image:
thumbnail = self._proto_relative_url(
image.replace('{width}', '1240').replace('{height}', '697'),
scheme='http:',
)
else:
thumbnail = None
stream = data.get('_embedded', {}).get('stream:show', {}).get('name')
if stream:
title = '%s: %s' % (stream, data['name'])
else:
title = data['name']
2014-02-10 01:37:12 +08:00
return {
2014-12-13 19:14:44 +08:00
'id': video_id,
'title': title,
'thumbnail': thumbnail,
2014-02-10 01:37:12 +08:00
'formats': formats,
2014-12-13 19:14:44 +08:00
'description': data.get('web_site_text'),
'duration': int_or_none(data.get('duration')),
'view_count': int_or_none(data.get('views')),
2014-02-10 01:37:12 +08:00
}