streamcz.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import hashlib
  4. import time
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. int_or_none,
  8. sanitized_Request,
  9. )
  10. def _get_api_key(api_path):
  11. if api_path.endswith('?'):
  12. api_path = api_path[:-1]
  13. api_key = 'fb5f58a820353bd7095de526253c14fd'
  14. a = '{0:}{1:}{2:}'.format(api_key, api_path, int(round(time.time() / 24 / 3600)))
  15. return hashlib.md5(a.encode('ascii')).hexdigest()
  16. class StreamCZIE(InfoExtractor):
  17. _VALID_URL = r'https?://(?:www\.)?stream\.cz/.+/(?P<id>[0-9]+)'
  18. _API_URL = 'http://www.stream.cz/API'
  19. _TESTS = [{
  20. 'url': 'http://www.stream.cz/peklonataliri/765767-ecka-pro-deti',
  21. 'md5': '6d3ca61a8d0633c9c542b92fcb936b0c',
  22. 'info_dict': {
  23. 'id': '765767',
  24. 'ext': 'mp4',
  25. 'title': 'Peklo na talíři: Éčka pro děti',
  26. 'description': 'Taška s grónskou pomazánkou a další pekelnosti ZDE',
  27. 'thumbnail': 're:^http://im.stream.cz/episode/52961d7e19d423f8f06f0100',
  28. 'duration': 256,
  29. },
  30. }, {
  31. 'url': 'http://www.stream.cz/blanik/10002447-tri-roky-pro-mazanka',
  32. 'md5': 'e54a254fb8b871968fd8403255f28589',
  33. 'info_dict': {
  34. 'id': '10002447',
  35. 'ext': 'mp4',
  36. 'title': 'Kancelář Blaník: Tři roky pro Mazánka',
  37. 'description': 'md5:3862a00ba7bf0b3e44806b544032c859',
  38. 'thumbnail': 're:^http://im.stream.cz/episode/537f838c50c11f8d21320000',
  39. 'duration': 368,
  40. },
  41. }]
  42. def _real_extract(self, url):
  43. video_id = self._match_id(url)
  44. api_path = '/episode/%s' % video_id
  45. req = sanitized_Request(self._API_URL + api_path)
  46. req.add_header('Api-Password', _get_api_key(api_path))
  47. data = self._download_json(req, video_id)
  48. formats = []
  49. for quality, video in enumerate(data['video_qualities']):
  50. for f in video['formats']:
  51. typ = f['type'].partition('/')[2]
  52. qlabel = video.get('quality_label')
  53. formats.append({
  54. 'format_note': '%s-%s' % (qlabel, typ) if qlabel else typ,
  55. 'format_id': '%s-%s' % (typ, f['quality']),
  56. 'url': f['source'],
  57. 'height': int_or_none(f['quality'].rstrip('p')),
  58. 'quality': quality,
  59. })
  60. self._sort_formats(formats)
  61. image = data.get('image')
  62. if image:
  63. thumbnail = self._proto_relative_url(
  64. image.replace('{width}', '1240').replace('{height}', '697'),
  65. scheme='http:',
  66. )
  67. else:
  68. thumbnail = None
  69. stream = data.get('_embedded', {}).get('stream:show', {}).get('name')
  70. if stream:
  71. title = '%s: %s' % (stream, data['name'])
  72. else:
  73. title = data['name']
  74. return {
  75. 'id': video_id,
  76. 'title': title,
  77. 'thumbnail': thumbnail,
  78. 'formats': formats,
  79. 'description': data.get('web_site_text'),
  80. 'duration': int_or_none(data.get('duration')),
  81. 'view_count': int_or_none(data.get('views')),
  82. }