streamango.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }, {
  34. 'url': 'https://streamango.com/embed/clapasobsptpkdfe/20170315_150006_mp4',
  35. 'only_matching': True,
  36. }]
  37. def _real_extract(self, url):
  38. def decrypt_src(encoded, val):
  39. ALPHABET = '=/+9876543210zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA'
  40. encoded = re.sub(r'[^A-Za-z0-9+/=]', '', encoded)
  41. decoded = ''
  42. sm = [None] * 4
  43. i = 0
  44. str_len = len(encoded)
  45. while i < str_len:
  46. for j in range(4):
  47. sm[j % 4] = ALPHABET.index(encoded[i])
  48. i += 1
  49. char_code = ((sm[0] << 0x2) | (sm[1] >> 0x4)) ^ val
  50. decoded += compat_chr(char_code)
  51. if sm[2] != 0x40:
  52. char_code = ((sm[1] & 0xf) << 0x4) | (sm[2] >> 0x2)
  53. decoded += compat_chr(char_code)
  54. if sm[3] != 0x40:
  55. char_code = ((sm[2] & 0x3) << 0x6) | sm[3]
  56. decoded += compat_chr(char_code)
  57. return decoded
  58. video_id = self._match_id(url)
  59. webpage = self._download_webpage(url, video_id)
  60. title = self._og_search_title(webpage, default=video_id)
  61. formats = []
  62. for format_ in re.findall(r'({[^}]*\bsrc\s*:\s*[^}]*})', webpage):
  63. mobj = re.search(r'(src\s*:\s*[^(]+\(([^)]*)\)[\s,]*)', format_)
  64. if mobj is None:
  65. continue
  66. format_ = format_.replace(mobj.group(0), '')
  67. video = self._parse_json(
  68. format_, video_id, transform_source=js_to_json,
  69. fatal=False) or {}
  70. mobj = re.search(
  71. r'([\'"])(?P<src>(?:(?!\1).)+)\1\s*,\s*(?P<val>\d+)',
  72. mobj.group(1))
  73. if mobj is None:
  74. continue
  75. src = decrypt_src(mobj.group('src'), int_or_none(mobj.group('val')))
  76. if not src:
  77. continue
  78. ext = determine_ext(src, default_ext=None)
  79. if video.get('type') == 'application/dash+xml' or ext == 'mpd':
  80. formats.extend(self._extract_mpd_formats(
  81. src, video_id, mpd_id='dash', fatal=False))
  82. else:
  83. formats.append({
  84. 'url': src,
  85. 'ext': ext or 'mp4',
  86. 'width': int_or_none(video.get('width')),
  87. 'height': int_or_none(video.get('height')),
  88. 'tbr': int_or_none(video.get('bitrate')),
  89. })
  90. if not formats:
  91. error = self._search_regex(
  92. r'<p[^>]+\bclass=["\']lead[^>]+>(.+?)</p>', webpage,
  93. 'error', default=None)
  94. if not error and '>Sorry' in webpage:
  95. error = 'Video %s is not available' % video_id
  96. if error:
  97. raise ExtractorError(error, expected=True)
  98. self._sort_formats(formats)
  99. return {
  100. 'id': video_id,
  101. 'url': url,
  102. 'title': title,
  103. 'formats': formats,
  104. }