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.

144 lines
5.2 KiB

10 years ago
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import base64
  4. import binascii
  5. import re
  6. import json
  7. from .common import InfoExtractor
  8. from ..utils import (
  9. ExtractorError,
  10. qualities,
  11. )
  12. from ..compat import compat_ord
  13. class TeamcocoIE(InfoExtractor):
  14. _VALID_URL = r'http://teamcoco\.com/video/(?P<video_id>[0-9]+)?/?(?P<display_id>.*)'
  15. _TESTS = [
  16. {
  17. 'url': 'http://teamcoco.com/video/80187/conan-becomes-a-mary-kay-beauty-consultant',
  18. 'md5': '3f7746aa0dc86de18df7539903d399ea',
  19. 'info_dict': {
  20. 'id': '80187',
  21. 'ext': 'mp4',
  22. 'title': 'Conan Becomes A Mary Kay Beauty Consultant',
  23. 'description': 'Mary Kay is perhaps the most trusted name in female beauty, so of course Conan is a natural choice to sell their products.',
  24. 'duration': 504,
  25. 'age_limit': 0,
  26. }
  27. }, {
  28. 'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
  29. 'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
  30. 'info_dict': {
  31. 'id': '19705',
  32. 'ext': 'mp4',
  33. 'description': 'Louis C.K. got starstruck by George W. Bush, so what? Part one.',
  34. 'title': 'Louis C.K. Interview Pt. 1 11/3/11',
  35. 'duration': 288,
  36. 'age_limit': 0,
  37. }
  38. }, {
  39. 'url': 'http://teamcoco.com/video/timothy-olyphant-drinking-whiskey',
  40. 'info_dict': {
  41. 'id': '88748',
  42. 'ext': 'mp4',
  43. 'title': 'Timothy Olyphant Raises A Toast To “Justified”',
  44. 'description': 'md5:15501f23f020e793aeca761205e42c24',
  45. },
  46. 'params': {
  47. 'skip_download': True, # m3u8 downloads
  48. }
  49. }
  50. ]
  51. _VIDEO_ID_REGEXES = (
  52. r'"eVar42"\s*:\s*(\d+)',
  53. r'Ginger\.TeamCoco\.openInApp\("video",\s*"([^"]+)"',
  54. r'"id_not"\s*:\s*(\d+)'
  55. )
  56. def _real_extract(self, url):
  57. mobj = re.match(self._VALID_URL, url)
  58. display_id = mobj.group('display_id')
  59. webpage, urlh = self._download_webpage_handle(url, display_id)
  60. if 'src=expired' in urlh.geturl():
  61. raise ExtractorError('This video is expired.', expected=True)
  62. video_id = mobj.group('video_id')
  63. if not video_id:
  64. video_id = self._html_search_regex(
  65. self._VIDEO_ID_REGEXES, webpage, 'video id')
  66. data = None
  67. preload_codes = self._html_search_regex(
  68. r'(function.+)setTimeout\(function\(\)\{playlist',
  69. webpage, 'preload codes')
  70. base64_fragments = re.findall(r'"([a-zA-z0-9+/=]+)"', preload_codes)
  71. base64_fragments.remove('init')
  72. def _check_sequence(cur_fragments):
  73. if not cur_fragments:
  74. return
  75. for i in range(len(cur_fragments)):
  76. cur_sequence = (''.join(cur_fragments[i:] + cur_fragments[:i])).encode('ascii')
  77. try:
  78. raw_data = base64.b64decode(cur_sequence)
  79. if compat_ord(raw_data[0]) == compat_ord('{'):
  80. return json.loads(raw_data.decode('utf-8'))
  81. except (TypeError, binascii.Error, UnicodeDecodeError, ValueError):
  82. continue
  83. def _check_data():
  84. for i in range(len(base64_fragments) + 1):
  85. for j in range(i, len(base64_fragments) + 1):
  86. data = _check_sequence(base64_fragments[:i] + base64_fragments[j:])
  87. if data:
  88. return data
  89. self.to_screen('Try to compute possible data sequence. This may take some time.')
  90. data = _check_data()
  91. if not data:
  92. raise ExtractorError(
  93. 'Preload information could not be extracted', expected=True)
  94. formats = []
  95. get_quality = qualities(['500k', '480p', '1000k', '720p', '1080p'])
  96. for filed in data['files']:
  97. if filed['type'] == 'hls':
  98. formats.extend(self._extract_m3u8_formats(
  99. filed['url'], video_id, ext='mp4'))
  100. else:
  101. m_format = re.search(r'(\d+(k|p))\.mp4', filed['url'])
  102. if m_format is not None:
  103. format_id = m_format.group(1)
  104. else:
  105. format_id = filed['bitrate']
  106. tbr = (
  107. int(filed['bitrate'])
  108. if filed['bitrate'].isdigit()
  109. else None)
  110. formats.append({
  111. 'url': filed['url'],
  112. 'ext': 'mp4',
  113. 'tbr': tbr,
  114. 'format_id': format_id,
  115. 'quality': get_quality(format_id),
  116. })
  117. self._sort_formats(formats)
  118. return {
  119. 'id': video_id,
  120. 'display_id': display_id,
  121. 'formats': formats,
  122. 'title': data['title'],
  123. 'thumbnail': data.get('thumb', {}).get('href'),
  124. 'description': data.get('teaser'),
  125. 'duration': data.get('duration'),
  126. 'age_limit': self._family_friendly_search(webpage),
  127. }