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.

172 lines
6.5 KiB

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