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.

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