You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.0 KiB

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