streamango.py 3.6 KB

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