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.

122 lines
4.0 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. from .common import InfoExtractor
  5. from ..compat import compat_chr
  6. from ..utils import (
  7. determine_ext,
  8. ExtractorError,
  9. int_or_none,
  10. js_to_json,
  11. )
  12. class StreamangoIE(InfoExtractor):
  13. _VALID_URL = r'https?://(?:www\.)?streamango\.com/(?:f|embed)/(?P<id>[^/?#&]+)'
  14. _TESTS = [{
  15. 'url': 'https://streamango.com/f/clapasobsptpkdfe/20170315_150006_mp4',
  16. 'md5': 'e992787515a182f55e38fc97588d802a',
  17. 'info_dict': {
  18. 'id': 'clapasobsptpkdfe',
  19. 'ext': 'mp4',
  20. 'title': '20170315_150006.mp4',
  21. }
  22. }, {
  23. # no og:title
  24. 'url': 'https://streamango.com/embed/foqebrpftarclpob/asdf_asd_2_mp4',
  25. 'info_dict': {
  26. 'id': 'foqebrpftarclpob',
  27. 'ext': 'mp4',
  28. 'title': 'foqebrpftarclpob',
  29. },
  30. 'params': {
  31. 'skip_download': True,
  32. },
  33. 'skip': 'gone',
  34. }, {
  35. 'url': 'https://streamango.com/embed/clapasobsptpkdfe/20170315_150006_mp4',
  36. 'only_matching': True,
  37. }]
  38. def _real_extract(self, url):
  39. def decrypt_src(encoded, val):
  40. ALPHABET = '=/+9876543210zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA'
  41. encoded = re.sub(r'[^A-Za-z0-9+/=]', '', encoded)
  42. decoded = ''
  43. sm = [None] * 4
  44. i = 0
  45. str_len = len(encoded)
  46. while i < str_len:
  47. for j in range(4):
  48. sm[j % 4] = ALPHABET.index(encoded[i])
  49. i += 1
  50. char_code = ((sm[0] << 0x2) | (sm[1] >> 0x4)) ^ val
  51. decoded += compat_chr(char_code)
  52. if sm[2] != 0x40:
  53. char_code = ((sm[1] & 0xf) << 0x4) | (sm[2] >> 0x2)
  54. decoded += compat_chr(char_code)
  55. if sm[3] != 0x40:
  56. char_code = ((sm[2] & 0x3) << 0x6) | sm[3]
  57. decoded += compat_chr(char_code)
  58. return decoded
  59. video_id = self._match_id(url)
  60. webpage = self._download_webpage(url, video_id)
  61. title = self._og_search_title(webpage, default=video_id)
  62. formats = []
  63. for format_ in re.findall(r'({[^}]*\bsrc\s*:\s*[^}]*})', webpage):
  64. mobj = re.search(r'(src\s*:\s*[^(]+\(([^)]*)\)[\s,]*)', format_)
  65. if mobj is None:
  66. continue
  67. format_ = format_.replace(mobj.group(0), '')
  68. video = self._parse_json(
  69. format_, video_id, transform_source=js_to_json,
  70. fatal=False) or {}
  71. mobj = re.search(
  72. r'([\'"])(?P<src>(?:(?!\1).)+)\1\s*,\s*(?P<val>\d+)',
  73. mobj.group(1))
  74. if mobj is None:
  75. continue
  76. src = decrypt_src(mobj.group('src'), int_or_none(mobj.group('val')))
  77. if not src:
  78. continue
  79. ext = determine_ext(src, default_ext=None)
  80. if video.get('type') == 'application/dash+xml' or ext == 'mpd':
  81. formats.extend(self._extract_mpd_formats(
  82. src, video_id, mpd_id='dash', fatal=False))
  83. else:
  84. formats.append({
  85. 'url': src,
  86. 'ext': ext or 'mp4',
  87. 'width': int_or_none(video.get('width')),
  88. 'height': int_or_none(video.get('height')),
  89. 'tbr': int_or_none(video.get('bitrate')),
  90. })
  91. if not formats:
  92. error = self._search_regex(
  93. r'<p[^>]+\bclass=["\']lead[^>]+>(.+?)</p>', webpage,
  94. 'error', default=None)
  95. if not error and '>Sorry' in webpage:
  96. error = 'Video %s is not available' % video_id
  97. if error:
  98. raise ExtractorError(error, expected=True)
  99. self._sort_formats(formats)
  100. return {
  101. 'id': video_id,
  102. 'url': url,
  103. 'title': title,
  104. 'formats': formats,
  105. }