teamcoco.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. from .common import InfoExtractor
  5. from ..utils import (
  6. determine_ext,
  7. ExtractorError,
  8. int_or_none,
  9. mimetype2ext,
  10. parse_duration,
  11. parse_iso8601,
  12. qualities,
  13. )
  14. class TeamcocoIE(InfoExtractor):
  15. _VALID_URL = r'https?://teamcoco\.com/video/(?P<id>([^/]+/)*[^/?#]+)'
  16. _TESTS = [
  17. {
  18. 'url': 'http://teamcoco.com/video/mary-kay-remote',
  19. 'md5': '55d532f81992f5c92046ad02fec34d7d',
  20. 'info_dict': {
  21. 'id': '80187',
  22. 'ext': 'mp4',
  23. 'title': 'Conan Becomes A Mary Kay Beauty Consultant',
  24. 'description': 'Mary Kay is perhaps the most trusted name in female beauty, so of course Conan is a natural choice to sell their products.',
  25. 'duration': 495.0,
  26. 'upload_date': '20140402',
  27. 'timestamp': 1396407600,
  28. }
  29. }, {
  30. 'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
  31. 'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
  32. 'info_dict': {
  33. 'id': '19705',
  34. 'ext': 'mp4',
  35. 'description': 'Louis C.K. got starstruck by George W. Bush, so what? Part one.',
  36. 'title': 'Louis C.K. Interview Pt. 1 11/3/11',
  37. 'duration': 288,
  38. 'upload_date': '20111104',
  39. 'timestamp': 1320405840,
  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. 'upload_date': '20150415',
  49. 'timestamp': 1429088400,
  50. },
  51. 'params': {
  52. 'skip_download': True, # m3u8 downloads
  53. }
  54. }, {
  55. 'url': 'http://teamcoco.com/video/full-episode-mon-6-1-joel-mchale-jake-tapper-and-musical-guest-courtney-barnett?playlist=x;eyJ0eXBlIjoidGFnIiwiaWQiOjl9',
  56. 'info_dict': {
  57. 'id': '89341',
  58. 'ext': 'mp4',
  59. 'title': 'Full Episode - Mon. 6/1 - Joel McHale, Jake Tapper, And Musical Guest Courtney Barnett',
  60. 'description': 'Guests: Joel McHale, Jake Tapper, And Musical Guest Courtney Barnett',
  61. },
  62. 'params': {
  63. 'skip_download': True, # m3u8 downloads
  64. },
  65. 'skip': 'This video is no longer available.',
  66. }, {
  67. 'url': 'http://teamcoco.com/video/the-conan-audiencey-awards-for-04/25/18',
  68. 'only_matching': True,
  69. }
  70. ]
  71. def _graphql_call(self, query_template, object_type, object_id):
  72. find_object = 'find' + object_type
  73. return self._download_json(
  74. 'http://teamcoco.com/graphql/', object_id, data=json.dumps({
  75. 'query': query_template % (find_object, object_id)
  76. }))['data'][find_object]
  77. def _real_extract(self, url):
  78. display_id = self._match_id(url)
  79. response = self._graphql_call('''{
  80. %s(slug: "video/%s") {
  81. ... on RecordSlug {
  82. record {
  83. id
  84. title
  85. teaser
  86. publishOn
  87. thumb {
  88. preview
  89. }
  90. tags {
  91. name
  92. }
  93. duration
  94. }
  95. }
  96. ... on NotFoundSlug {
  97. status
  98. }
  99. }
  100. }''', 'Slug', display_id)
  101. if response.get('status'):
  102. raise ExtractorError('This video is no longer available.', expected=True)
  103. record = response['record']
  104. video_id = record['id']
  105. srcs = self._graphql_call('''{
  106. %s(id: "%s") {
  107. src
  108. }
  109. }''', 'RecordVideoSource', video_id)['src']
  110. formats = []
  111. get_quality = qualities(['low', 'sd', 'hd', 'uhd'])
  112. for format_id, src in srcs.items():
  113. if not isinstance(src, dict):
  114. continue
  115. src_url = src.get('src')
  116. if not src_url:
  117. continue
  118. ext = determine_ext(src_url, mimetype2ext(src.get('type')))
  119. if format_id == 'hls' or ext == 'm3u8':
  120. # compat_urllib_parse.urljoin does not work here
  121. if src_url.startswith('/'):
  122. src_url = 'http://ht.cdn.turner.com/tbs/big/teamcoco' + src_url
  123. formats.extend(self._extract_m3u8_formats(
  124. src_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  125. else:
  126. if src_url.startswith('/mp4:protected/'):
  127. # TODO Correct extraction for these files
  128. continue
  129. tbr = int_or_none(self._search_regex(
  130. r'(\d+)k\.mp4', src_url, 'tbr', default=None))
  131. formats.append({
  132. 'url': src_url,
  133. 'ext': ext,
  134. 'tbr': tbr,
  135. 'format_id': format_id,
  136. 'quality': get_quality(format_id),
  137. })
  138. self._sort_formats(formats)
  139. return {
  140. 'id': video_id,
  141. 'display_id': display_id,
  142. 'formats': formats,
  143. 'title': record['title'],
  144. 'thumbnail': record.get('thumb', {}).get('preview'),
  145. 'description': record.get('teaser'),
  146. 'duration': parse_duration(record.get('duration')),
  147. 'timestamp': parse_iso8601(record.get('publishOn')),
  148. }