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.

137 lines
4.5 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. from .common import InfoExtractor
  3. from ..compat import (
  4. compat_chr,
  5. compat_ord,
  6. compat_urllib_parse_unquote,
  7. )
  8. from ..utils import (
  9. int_or_none,
  10. parse_iso8601,
  11. urljoin,
  12. )
  13. class BeegIE(InfoExtractor):
  14. _VALID_URL = r'https?://(?:www\.)?beeg\.com/(?P<id>\d+)'
  15. _TEST = {
  16. 'url': 'http://beeg.com/5416503',
  17. 'md5': 'a1a1b1a8bc70a89e49ccfd113aed0820',
  18. 'info_dict': {
  19. 'id': '5416503',
  20. 'ext': 'mp4',
  21. 'title': 'Sultry Striptease',
  22. 'description': 'md5:d22219c09da287c14bed3d6c37ce4bc2',
  23. 'timestamp': 1391813355,
  24. 'upload_date': '20140207',
  25. 'duration': 383,
  26. 'tags': list,
  27. 'age_limit': 18,
  28. }
  29. }
  30. def _real_extract(self, url):
  31. video_id = self._match_id(url)
  32. webpage = self._download_webpage(url, video_id)
  33. cpl_url = self._search_regex(
  34. r'<script[^>]+src=(["\'])(?P<url>(?:/static|(?:https?:)?//static\.beeg\.com)/cpl/\d+\.js.*?)\1',
  35. webpage, 'cpl', default=None, group='url')
  36. cpl_url = urljoin(url, cpl_url)
  37. beeg_version, beeg_salt = [None] * 2
  38. if cpl_url:
  39. cpl = self._download_webpage(
  40. self._proto_relative_url(cpl_url), video_id,
  41. 'Downloading cpl JS', fatal=False)
  42. if cpl:
  43. beeg_version = int_or_none(self._search_regex(
  44. r'beeg_version\s*=\s*([^\b]+)', cpl,
  45. 'beeg version', default=None)) or self._search_regex(
  46. r'/(\d+)\.js', cpl_url, 'beeg version', default=None)
  47. beeg_salt = self._search_regex(
  48. r'beeg_salt\s*=\s*(["\'])(?P<beeg_salt>.+?)\1', cpl, 'beeg salt',
  49. default=None, group='beeg_salt')
  50. beeg_version = beeg_version or '2185'
  51. beeg_salt = beeg_salt or 'pmweAkq8lAYKdfWcFCUj0yoVgoPlinamH5UE1CB3H'
  52. for api_path in ('', 'api.'):
  53. video = self._download_json(
  54. 'https://%sbeeg.com/api/v6/%s/video/%s'
  55. % (api_path, beeg_version, video_id), video_id,
  56. fatal=api_path == 'api.')
  57. if video:
  58. break
  59. def split(o, e):
  60. def cut(s, x):
  61. n.append(s[:x])
  62. return s[x:]
  63. n = []
  64. r = len(o) % e
  65. if r > 0:
  66. o = cut(o, r)
  67. while len(o) > e:
  68. o = cut(o, e)
  69. n.append(o)
  70. return n
  71. def decrypt_key(key):
  72. # Reverse engineered from http://static.beeg.com/cpl/1738.js
  73. a = beeg_salt
  74. e = compat_urllib_parse_unquote(key)
  75. o = ''.join([
  76. compat_chr(compat_ord(e[n]) - compat_ord(a[n % len(a)]) % 21)
  77. for n in range(len(e))])
  78. return ''.join(split(o, 3)[::-1])
  79. def decrypt_url(encrypted_url):
  80. encrypted_url = self._proto_relative_url(
  81. encrypted_url.replace('{DATA_MARKERS}', ''), 'https:')
  82. key = self._search_regex(
  83. r'/key=(.*?)%2Cend=', encrypted_url, 'key', default=None)
  84. if not key:
  85. return encrypted_url
  86. return encrypted_url.replace(key, decrypt_key(key))
  87. formats = []
  88. for format_id, video_url in video.items():
  89. if not video_url:
  90. continue
  91. height = self._search_regex(
  92. r'^(\d+)[pP]$', format_id, 'height', default=None)
  93. if not height:
  94. continue
  95. formats.append({
  96. 'url': decrypt_url(video_url),
  97. 'format_id': format_id,
  98. 'height': int(height),
  99. })
  100. self._sort_formats(formats)
  101. title = video['title']
  102. video_id = video.get('id') or video_id
  103. display_id = video.get('code')
  104. description = video.get('desc')
  105. timestamp = parse_iso8601(video.get('date'), ' ')
  106. duration = int_or_none(video.get('duration'))
  107. tags = [tag.strip() for tag in video['tags'].split(',')] if video.get('tags') else None
  108. return {
  109. 'id': video_id,
  110. 'display_id': display_id,
  111. 'title': title,
  112. 'description': description,
  113. 'timestamp': timestamp,
  114. 'duration': duration,
  115. 'tags': tags,
  116. 'formats': formats,
  117. 'age_limit': self._rta_search(webpage),
  118. }