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.

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