teamcoco.py 3.4 KB

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