teamcoco.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import json
  2. from .turner import TurnerBaseIE
  3. from ..utils import (
  4. determine_ext,
  5. ExtractorError,
  6. int_or_none,
  7. mimetype2ext,
  8. parse_duration,
  9. parse_iso8601,
  10. qualities,
  11. )
  12. class TeamcocoIE(TurnerBaseIE):
  13. _VALID_URL = r'https?://(?:\w+\.)?teamcoco\.com/(?P<id>([^/]+/)*[^/?#]+)'
  14. _TESTS = [
  15. {
  16. 'url': 'http://teamcoco.com/video/mary-kay-remote',
  17. 'md5': '55d532f81992f5c92046ad02fec34d7d',
  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': 495.0,
  24. 'upload_date': '20140402',
  25. 'timestamp': 1396407600,
  26. }
  27. }, {
  28. 'url': 'http://teamcoco.com/video/louis-ck-interview-george-w-bush',
  29. 'md5': 'cde9ba0fa3506f5f017ce11ead928f9a',
  30. 'info_dict': {
  31. 'id': '19705',
  32. 'ext': 'mp4',
  33. 'description': 'Louis C.K. got starstruck by George W. Bush, so what? Part one.',
  34. 'title': 'Louis C.K. Interview Pt. 1 11/3/11',
  35. 'duration': 288,
  36. 'upload_date': '20111104',
  37. 'timestamp': 1320405840,
  38. }
  39. }, {
  40. 'url': 'http://teamcoco.com/video/timothy-olyphant-drinking-whiskey',
  41. 'info_dict': {
  42. 'id': '88748',
  43. 'ext': 'mp4',
  44. 'title': 'Timothy Olyphant Raises A Toast To “Justified”',
  45. 'description': 'md5:15501f23f020e793aeca761205e42c24',
  46. 'upload_date': '20150415',
  47. 'timestamp': 1429088400,
  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. 'skip': 'This video is no longer available.',
  64. }, {
  65. 'url': 'http://teamcoco.com/video/the-conan-audiencey-awards-for-04/25/18',
  66. 'only_matching': True,
  67. }, {
  68. 'url': 'http://teamcoco.com/italy/conan-jordan-schlansky-hit-the-streets-of-florence',
  69. 'only_matching': True,
  70. }, {
  71. 'url': 'http://teamcoco.com/haiti/conan-s-haitian-history-lesson',
  72. 'only_matching': True,
  73. }, {
  74. 'url': 'http://teamcoco.com/israel/conan-hits-the-streets-beaches-of-tel-aviv',
  75. 'only_matching': True,
  76. }, {
  77. 'url': 'https://conan25.teamcoco.com/video/ice-cube-kevin-hart-conan-share-lyft',
  78. 'only_matching': True,
  79. }
  80. ]
  81. _RECORD_TEMPL = '''id
  82. title
  83. teaser
  84. publishOn
  85. thumb {
  86. preview
  87. }
  88. tags {
  89. name
  90. }
  91. duration
  92. turnerMediaId
  93. turnerMediaAuthToken'''
  94. def _graphql_call(self, query_template, object_type, object_id):
  95. find_object = 'find' + object_type
  96. return self._download_json(
  97. 'https://teamcoco.com/graphql', object_id, data=json.dumps({
  98. 'query': query_template % (find_object, object_id)
  99. }).encode(), headers={
  100. 'Content-Type': 'application/json',
  101. })['data'][find_object]
  102. def _real_extract(self, url):
  103. display_id = self._match_id(url)
  104. response = self._graphql_call('''{
  105. %%s(slug: "%%s") {
  106. ... on RecordSlug {
  107. record {
  108. %s
  109. }
  110. }
  111. ... on PageSlug {
  112. child {
  113. id
  114. }
  115. }
  116. ... on NotFoundSlug {
  117. status
  118. }
  119. }
  120. }''' % self._RECORD_TEMPL, 'Slug', display_id)
  121. if response.get('status'):
  122. raise ExtractorError('This video is no longer available.', expected=True)
  123. child = response.get('child')
  124. if child:
  125. record = self._graphql_call('''{
  126. %%s(id: "%%s") {
  127. ... on Video {
  128. %s
  129. }
  130. }
  131. }''' % self._RECORD_TEMPL, 'Record', child['id'])
  132. else:
  133. record = response['record']
  134. video_id = record['id']
  135. info = {
  136. 'id': video_id,
  137. 'display_id': display_id,
  138. 'title': record['title'],
  139. 'thumbnail': record.get('thumb', {}).get('preview'),
  140. 'description': record.get('teaser'),
  141. 'duration': parse_duration(record.get('duration')),
  142. 'timestamp': parse_iso8601(record.get('publishOn')),
  143. }
  144. media_id = record.get('turnerMediaId')
  145. if media_id:
  146. self._initialize_geo_bypass({
  147. 'countries': ['US'],
  148. })
  149. info.update(self._extract_ngtv_info(media_id, {
  150. 'accessToken': record['turnerMediaAuthToken'],
  151. 'accessTokenType': 'jws',
  152. }))
  153. else:
  154. video_sources = self._download_json(
  155. 'https://teamcoco.com/_truman/d/' + video_id,
  156. video_id)['meta']['src']
  157. if isinstance(video_sources, dict):
  158. video_sources = video_sources.values()
  159. formats = []
  160. get_quality = qualities(['low', 'sd', 'hd', 'uhd'])
  161. for src in video_sources:
  162. if not isinstance(src, dict):
  163. continue
  164. src_url = src.get('src')
  165. if not src_url:
  166. continue
  167. format_id = src.get('label')
  168. ext = determine_ext(src_url, mimetype2ext(src.get('type')))
  169. if format_id == 'hls' or ext == 'm3u8':
  170. # compat_urllib_parse.urljoin does not work here
  171. if src_url.startswith('/'):
  172. src_url = 'http://ht.cdn.turner.com/tbs/big/teamcoco' + src_url
  173. formats.extend(self._extract_m3u8_formats(
  174. src_url, video_id, 'mp4', m3u8_id=format_id, fatal=False))
  175. else:
  176. if src_url.startswith('/mp4:protected/'):
  177. # TODO Correct extraction for these files
  178. continue
  179. tbr = int_or_none(self._search_regex(
  180. r'(\d+)k\.mp4', src_url, 'tbr', default=None))
  181. formats.append({
  182. 'url': src_url,
  183. 'ext': ext,
  184. 'tbr': tbr,
  185. 'format_id': format_id,
  186. 'quality': get_quality(format_id),
  187. })
  188. info['formats'] = formats
  189. return info