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.

187 lines
7.6 KiB

  1. import re
  2. import xml.etree.ElementTree
  3. from .common import InfoExtractor
  4. from ..utils import (
  5. compat_str,
  6. compat_urllib_parse,
  7. ExtractorError,
  8. unified_strdate,
  9. )
  10. class ComedyCentralIE(InfoExtractor):
  11. IE_DESC = u'The Daily Show / Colbert Report'
  12. # urls can be abbreviations like :thedailyshow or :colbert
  13. # urls for episodes like:
  14. # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
  15. # or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
  16. # or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
  17. _VALID_URL = r"""^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport)
  18. |(https?://)?(www\.)?
  19. (?P<showname>thedailyshow|colbertnation)\.com/
  20. (full-episodes/(?P<episode>.*)|
  21. (?P<clip>
  22. (the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
  23. |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*)))|
  24. (?P<interview>
  25. extended-interviews/(?P<interID>[0-9]+)/playlist_tds_extended_(?P<interview_title>.*?)/.*?)))
  26. $"""
  27. _TEST = {
  28. u'url': u'http://www.thedailyshow.com/watch/thu-december-13-2012/kristen-stewart',
  29. u'file': u'422212.mp4',
  30. u'md5': u'4e2f5cb088a83cd8cdb7756132f9739d',
  31. u'info_dict': {
  32. u"upload_date": u"20121214",
  33. u"description": u"Kristen Stewart",
  34. u"uploader": u"thedailyshow",
  35. u"title": u"thedailyshow-kristen-stewart part 1"
  36. }
  37. }
  38. _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
  39. _video_extensions = {
  40. '3500': 'mp4',
  41. '2200': 'mp4',
  42. '1700': 'mp4',
  43. '1200': 'mp4',
  44. '750': 'mp4',
  45. '400': 'mp4',
  46. }
  47. _video_dimensions = {
  48. '3500': (1280, 720),
  49. '2200': (960, 540),
  50. '1700': (768, 432),
  51. '1200': (640, 360),
  52. '750': (512, 288),
  53. '400': (384, 216),
  54. }
  55. @classmethod
  56. def suitable(cls, url):
  57. """Receives a URL and returns True if suitable for this IE."""
  58. return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
  59. @staticmethod
  60. def _transform_rtmp_url(rtmp_video_url):
  61. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp.comedystor/.*)$', rtmp_video_url)
  62. if not m:
  63. raise ExtractorError(u'Cannot transform RTMP url')
  64. base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
  65. return base + m.group('finalid')
  66. def _real_extract(self, url):
  67. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  68. if mobj is None:
  69. raise ExtractorError(u'Invalid URL: %s' % url)
  70. if mobj.group('shortname'):
  71. if mobj.group('shortname') in ('tds', 'thedailyshow'):
  72. url = u'http://www.thedailyshow.com/full-episodes/'
  73. else:
  74. url = u'http://www.colbertnation.com/full-episodes/'
  75. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  76. assert mobj is not None
  77. if mobj.group('clip'):
  78. if mobj.group('showname') == 'thedailyshow':
  79. epTitle = mobj.group('tdstitle')
  80. else:
  81. epTitle = mobj.group('cntitle')
  82. dlNewest = False
  83. elif mobj.group('interview'):
  84. epTitle = mobj.group('interview_title')
  85. dlNewest = False
  86. else:
  87. dlNewest = not mobj.group('episode')
  88. if dlNewest:
  89. epTitle = mobj.group('showname')
  90. else:
  91. epTitle = mobj.group('episode')
  92. self.report_extraction(epTitle)
  93. webpage,htmlHandle = self._download_webpage_handle(url, epTitle)
  94. if dlNewest:
  95. url = htmlHandle.geturl()
  96. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  97. if mobj is None:
  98. raise ExtractorError(u'Invalid redirected URL: ' + url)
  99. if mobj.group('episode') == '':
  100. raise ExtractorError(u'Redirected URL is still not specific: ' + url)
  101. epTitle = mobj.group('episode')
  102. mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
  103. if len(mMovieParams) == 0:
  104. # The Colbert Report embeds the information in a without
  105. # a URL prefix; so extract the alternate reference
  106. # and then add the URL prefix manually.
  107. altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video).*?:.*?)"', webpage)
  108. if len(altMovieParams) == 0:
  109. raise ExtractorError(u'unable to find Flash URL in webpage ' + url)
  110. else:
  111. mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
  112. uri = mMovieParams[0][1]
  113. indexUrl = 'http://shadow.comedycentral.com/feeds/video_player/mrss/?' + compat_urllib_parse.urlencode({'uri': uri})
  114. indexXml = self._download_webpage(indexUrl, epTitle,
  115. u'Downloading show index',
  116. u'unable to download episode index')
  117. results = []
  118. idoc = xml.etree.ElementTree.fromstring(indexXml)
  119. itemEls = idoc.findall('.//item')
  120. for partNum,itemEl in enumerate(itemEls):
  121. mediaId = itemEl.findall('./guid')[0].text
  122. shortMediaId = mediaId.split(':')[-1]
  123. showId = mediaId.split(':')[-2].replace('.com', '')
  124. officialTitle = itemEl.findall('./title')[0].text
  125. officialDate = unified_strdate(itemEl.findall('./pubDate')[0].text)
  126. configUrl = ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' +
  127. compat_urllib_parse.urlencode({'uri': mediaId}))
  128. configXml = self._download_webpage(configUrl, epTitle,
  129. u'Downloading configuration for %s' % shortMediaId)
  130. cdoc = xml.etree.ElementTree.fromstring(configXml)
  131. turls = []
  132. for rendition in cdoc.findall('.//rendition'):
  133. finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
  134. turls.append(finfo)
  135. if len(turls) == 0:
  136. self._downloader.report_error(u'unable to download ' + mediaId + ': No videos found')
  137. continue
  138. formats = []
  139. for format, rtmp_video_url in turls:
  140. w, h = self._video_dimensions.get(format, (None, None))
  141. formats.append({
  142. 'url': self._transform_rtmp_url(rtmp_video_url),
  143. 'ext': self._video_extensions.get(format, 'mp4'),
  144. 'format_id': format,
  145. 'height': h,
  146. 'width': w,
  147. })
  148. effTitle = showId + u'-' + epTitle + u' part ' + compat_str(partNum+1)
  149. info = {
  150. 'id': shortMediaId,
  151. 'formats': formats,
  152. 'uploader': showId,
  153. 'upload_date': officialDate,
  154. 'title': effTitle,
  155. 'thumbnail': None,
  156. 'description': compat_str(officialTitle),
  157. }
  158. # TODO: Remove when #980 has been merged
  159. info.update(info['formats'][-1])
  160. results.append(info)
  161. return results