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.

125 lines
4.3 KiB

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