comedycentral.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. float_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. (?:videos/[^/]+/(?P<videotitle>[^/?#]+))
  40. |(the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
  41. |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*)))|
  42. (?P<interview>
  43. extended-interviews/(?P<interID>[0-9a-z]+)/(?:playlist_tds_extended_)?(?P<interview_title>.*?)(/.*?)?)))
  44. $'''
  45. _TEST = {
  46. 'url': 'http://thedailyshow.cc.com/watch/thu-december-13-2012/kristen-stewart',
  47. 'md5': '4e2f5cb088a83cd8cdb7756132f9739d',
  48. 'info_dict': {
  49. 'id': 'ab9ab3e7-5a98-4dbe-8b21-551dc0523d55',
  50. 'ext': 'mp4',
  51. 'upload_date': '20121213',
  52. 'description': 'Kristen Stewart learns to let loose in "On the Road."',
  53. 'uploader': 'thedailyshow',
  54. 'title': 'thedailyshow-kristen-stewart part 1',
  55. }
  56. }
  57. _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
  58. _video_extensions = {
  59. '3500': 'mp4',
  60. '2200': 'mp4',
  61. '1700': 'mp4',
  62. '1200': 'mp4',
  63. '750': 'mp4',
  64. '400': 'mp4',
  65. }
  66. _video_dimensions = {
  67. '3500': (1280, 720),
  68. '2200': (960, 540),
  69. '1700': (768, 432),
  70. '1200': (640, 360),
  71. '750': (512, 288),
  72. '400': (384, 216),
  73. }
  74. @staticmethod
  75. def _transform_rtmp_url(rtmp_video_url):
  76. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp\.comedystor/.*)$', rtmp_video_url)
  77. if not m:
  78. raise ExtractorError('Cannot transform RTMP url')
  79. base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
  80. return base + m.group('finalid')
  81. def _real_extract(self, url):
  82. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  83. if mobj is None:
  84. raise ExtractorError('Invalid URL: %s' % url)
  85. if mobj.group('shortname'):
  86. if mobj.group('shortname') in ('tds', 'thedailyshow'):
  87. url = 'http://thedailyshow.cc.com/full-episodes/'
  88. else:
  89. url = 'http://thecolbertreport.cc.com/full-episodes/'
  90. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  91. assert mobj is not None
  92. if mobj.group('clip'):
  93. if mobj.group('videotitle'):
  94. epTitle = mobj.group('videotitle')
  95. elif mobj.group('showname') == 'thedailyshow':
  96. epTitle = mobj.group('tdstitle')
  97. else:
  98. epTitle = mobj.group('cntitle')
  99. dlNewest = False
  100. elif mobj.group('interview'):
  101. epTitle = mobj.group('interview_title')
  102. dlNewest = False
  103. else:
  104. dlNewest = not mobj.group('episode')
  105. if dlNewest:
  106. epTitle = mobj.group('showname')
  107. else:
  108. epTitle = mobj.group('episode')
  109. show_name = mobj.group('showname')
  110. webpage, htmlHandle = self._download_webpage_handle(url, epTitle)
  111. if dlNewest:
  112. url = htmlHandle.geturl()
  113. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  114. if mobj is None:
  115. raise ExtractorError('Invalid redirected URL: ' + url)
  116. if mobj.group('episode') == '':
  117. raise ExtractorError('Redirected URL is still not specific: ' + url)
  118. epTitle = mobj.group('episode').rpartition('/')[-1]
  119. mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
  120. if len(mMovieParams) == 0:
  121. # The Colbert Report embeds the information in a without
  122. # a URL prefix; so extract the alternate reference
  123. # and then add the URL prefix manually.
  124. altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video|playlist).*?:.*?)"', webpage)
  125. if len(altMovieParams) == 0:
  126. raise ExtractorError('unable to find Flash URL in webpage ' + url)
  127. else:
  128. mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
  129. uri = mMovieParams[0][1]
  130. # Correct cc.com in uri
  131. uri = re.sub(r'(episode:[^.]+)(\.cc)?\.com', r'\1.cc.com', uri)
  132. index_url = 'http://%s.cc.com/feeds/mrss?%s' % (show_name, compat_urllib_parse.urlencode({'uri': uri}))
  133. idoc = self._download_xml(
  134. index_url, epTitle,
  135. 'Downloading show index', 'Unable to download episode index')
  136. title = idoc.find('./channel/title').text
  137. description = idoc.find('./channel/description').text
  138. entries = []
  139. item_els = idoc.findall('.//item')
  140. for part_num, itemEl in enumerate(item_els):
  141. upload_date = unified_strdate(itemEl.findall('./pubDate')[0].text)
  142. thumbnail = itemEl.find('.//{http://search.yahoo.com/mrss/}thumbnail').attrib.get('url')
  143. content = itemEl.find('.//{http://search.yahoo.com/mrss/}content')
  144. duration = float_or_none(content.attrib.get('duration'))
  145. mediagen_url = content.attrib['url']
  146. guid = itemEl.find('.//guid').text.rpartition(':')[-1]
  147. cdoc = self._download_xml(
  148. mediagen_url, epTitle,
  149. 'Downloading configuration for segment %d / %d' % (part_num + 1, len(item_els)))
  150. turls = []
  151. for rendition in cdoc.findall('.//rendition'):
  152. finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
  153. turls.append(finfo)
  154. formats = []
  155. for format, rtmp_video_url in turls:
  156. w, h = self._video_dimensions.get(format, (None, None))
  157. formats.append({
  158. 'format_id': 'vhttp-%s' % format,
  159. 'url': self._transform_rtmp_url(rtmp_video_url),
  160. 'ext': self._video_extensions.get(format, 'mp4'),
  161. 'height': h,
  162. 'width': w,
  163. })
  164. formats.append({
  165. 'format_id': 'rtmp-%s' % format,
  166. 'url': rtmp_video_url,
  167. 'ext': self._video_extensions.get(format, 'mp4'),
  168. 'height': h,
  169. 'width': w,
  170. })
  171. self._sort_formats(formats)
  172. virtual_id = show_name + ' ' + epTitle + ' part ' + compat_str(part_num + 1)
  173. entries.append({
  174. 'id': guid,
  175. 'title': virtual_id,
  176. 'formats': formats,
  177. 'uploader': show_name,
  178. 'upload_date': upload_date,
  179. 'duration': duration,
  180. 'thumbnail': thumbnail,
  181. 'description': description,
  182. })
  183. return {
  184. '_type': 'playlist',
  185. 'entries': entries,
  186. 'title': show_name + ' ' + title,
  187. 'description': description,
  188. }