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.

211 lines
8.6 KiB

  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from .mtv import MTVServicesInfoExtractor
  5. from ..utils import (
  6. compat_str,
  7. compat_urllib_parse,
  8. ExtractorError,
  9. int_or_none,
  10. unified_strdate,
  11. )
  12. class ComedyCentralIE(MTVServicesInfoExtractor):
  13. _VALID_URL = r'''(?x)https?://(?:www\.)?(comedycentral|cc)\.com/
  14. (video-clips|episodes|cc-studios|video-collections)
  15. /(?P<title>.*)'''
  16. _FEED_URL = 'http://comedycentral.com/feeds/mrss/'
  17. _TEST = {
  18. 'url': 'http://www.comedycentral.com/video-clips/kllhuv/stand-up-greg-fitzsimmons--uncensored---too-good-of-a-mother',
  19. 'md5': '4167875aae411f903b751a21f357f1ee',
  20. 'info_dict': {
  21. 'id': 'cef0cbb3-e776-4bc9-b62e-8016deccb354',
  22. 'ext': 'mp4',
  23. 'title': 'CC:Stand-Up|Greg Fitzsimmons: Life on Stage|Uncensored - Too Good of a Mother',
  24. 'description': 'After a certain point, breastfeeding becomes c**kblocking.',
  25. },
  26. }
  27. class ComedyCentralShowsIE(InfoExtractor):
  28. IE_DESC = 'The Daily Show / The Colbert Report'
  29. # urls can be abbreviations like :thedailyshow or :colbert
  30. # urls for episodes like:
  31. # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
  32. # or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
  33. # or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
  34. _VALID_URL = r'''(?x)^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport)
  35. |https?://(:www\.)?
  36. (?P<showname>thedailyshow|thecolbertreport)\.(?:cc\.)?com/
  37. (full-episodes/(?P<episode>.*)|
  38. (?P<clip>
  39. (the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
  40. |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*)))|
  41. (?P<interview>
  42. extended-interviews/(?P<interID>[0-9a-z]+)/(?:playlist_tds_extended_)?(?P<interview_title>.*?)(/.*?)?)))
  43. $'''
  44. _TEST = {
  45. 'url': 'http://thedailyshow.cc.com/watch/thu-december-13-2012/kristen-stewart',
  46. 'md5': '4e2f5cb088a83cd8cdb7756132f9739d',
  47. 'info_dict': {
  48. 'id': 'ab9ab3e7-5a98-4dbe-8b21-551dc0523d55',
  49. 'ext': 'mp4',
  50. 'upload_date': '20121213',
  51. 'description': 'Kristen Stewart learns to let loose in "On the Road."',
  52. 'uploader': 'thedailyshow',
  53. 'title': 'thedailyshow-kristen-stewart part 1',
  54. }
  55. }
  56. _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
  57. _video_extensions = {
  58. '3500': 'mp4',
  59. '2200': 'mp4',
  60. '1700': 'mp4',
  61. '1200': 'mp4',
  62. '750': 'mp4',
  63. '400': 'mp4',
  64. }
  65. _video_dimensions = {
  66. '3500': (1280, 720),
  67. '2200': (960, 540),
  68. '1700': (768, 432),
  69. '1200': (640, 360),
  70. '750': (512, 288),
  71. '400': (384, 216),
  72. }
  73. @staticmethod
  74. def _transform_rtmp_url(rtmp_video_url):
  75. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\.comedystor/.*)$', rtmp_video_url)
  76. if not m:
  77. raise ExtractorError('Cannot transform RTMP url')
  78. base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
  79. return base + m.group('finalid')
  80. def _real_extract(self, url):
  81. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  82. if mobj is None:
  83. raise ExtractorError('Invalid URL: %s' % url)
  84. if mobj.group('shortname'):
  85. if mobj.group('shortname') in ('tds', 'thedailyshow'):
  86. url = 'http://thedailyshow.cc.com/full-episodes/'
  87. else:
  88. url = 'http://thecolbertreport.cc.com/full-episodes/'
  89. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  90. assert mobj is not None
  91. if mobj.group('clip'):
  92. if mobj.group('showname') == 'thedailyshow':
  93. epTitle = mobj.group('tdstitle')
  94. else:
  95. epTitle = mobj.group('cntitle')
  96. dlNewest = False
  97. elif mobj.group('interview'):
  98. epTitle = mobj.group('interview_title')
  99. dlNewest = False
  100. else:
  101. dlNewest = not mobj.group('episode')
  102. if dlNewest:
  103. epTitle = mobj.group('showname')
  104. else:
  105. epTitle = mobj.group('episode')
  106. show_name = mobj.group('showname')
  107. webpage, htmlHandle = self._download_webpage_handle(url, epTitle)
  108. if dlNewest:
  109. url = htmlHandle.geturl()
  110. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  111. if mobj is None:
  112. raise ExtractorError('Invalid redirected URL: ' + url)
  113. if mobj.group('episode') == '':
  114. raise ExtractorError('Redirected URL is still not specific: ' + url)
  115. epTitle = mobj.group('episode').rpartition('/')[-1]
  116. mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
  117. if len(mMovieParams) == 0:
  118. # The Colbert Report embeds the information in a without
  119. # a URL prefix; so extract the alternate reference
  120. # and then add the URL prefix manually.
  121. altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video|playlist).*?:.*?)"', webpage)
  122. if len(altMovieParams) == 0:
  123. raise ExtractorError('unable to find Flash URL in webpage ' + url)
  124. else:
  125. mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
  126. uri = mMovieParams[0][1]
  127. # Correct cc.com in uri
  128. uri = re.sub(r'(episode:[^.]+)(\.cc)?\.com', r'\1.cc.com', uri)
  129. index_url = 'http://%s.cc.com/feeds/mrss?%s' % (show_name, compat_urllib_parse.urlencode({'uri': uri}))
  130. idoc = self._download_xml(
  131. index_url, epTitle,
  132. 'Downloading show index', 'Unable to download episode index')
  133. title = idoc.find('./channel/title').text
  134. description = idoc.find('./channel/description').text
  135. entries = []
  136. item_els = idoc.findall('.//item')
  137. for part_num, itemEl in enumerate(item_els):
  138. upload_date = unified_strdate(itemEl.findall('./pubDate')[0].text)
  139. thumbnail = itemEl.find('.//{http://search.yahoo.com/mrss/}thumbnail').attrib.get('url')
  140. content = itemEl.find('.//{http://search.yahoo.com/mrss/}content')
  141. duration = int_or_none(content.attrib.get('duration'))
  142. mediagen_url = content.attrib['url']
  143. guid = itemEl.find('.//guid').text.rpartition(':')[-1]
  144. cdoc = self._download_xml(
  145. mediagen_url, epTitle,
  146. 'Downloading configuration for segment %d / %d' % (part_num + 1, len(item_els)))
  147. turls = []
  148. for rendition in cdoc.findall('.//rendition'):
  149. finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
  150. turls.append(finfo)
  151. formats = []
  152. for format, rtmp_video_url in turls:
  153. w, h = self._video_dimensions.get(format, (None, None))
  154. formats.append({
  155. 'format_id': 'vhttp-%s' % format,
  156. 'url': self._transform_rtmp_url(rtmp_video_url),
  157. 'ext': self._video_extensions.get(format, 'mp4'),
  158. 'height': h,
  159. 'width': w,
  160. })
  161. formats.append({
  162. 'format_id': 'rtmp-%s' % format,
  163. 'url': rtmp_video_url,
  164. 'ext': self._video_extensions.get(format, 'mp4'),
  165. 'height': h,
  166. 'width': w,
  167. })
  168. self._sort_formats(formats)
  169. virtual_id = show_name + ' ' + epTitle + ' part ' + compat_str(part_num + 1)
  170. entries.append({
  171. 'id': guid,
  172. 'title': virtual_id,
  173. 'formats': formats,
  174. 'uploader': show_name,
  175. 'upload_date': upload_date,
  176. 'duration': duration,
  177. 'thumbnail': thumbnail,
  178. 'description': description,
  179. })
  180. return {
  181. '_type': 'playlist',
  182. 'entries': entries,
  183. 'title': show_name + ' ' + title,
  184. 'description': description,
  185. }