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.

189 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. $"""
  25. _TEST = {
  26. u'url': u'http://www.thedailyshow.com/watch/thu-december-13-2012/kristen-stewart',
  27. u'file': u'422212.mp4',
  28. u'md5': u'4e2f5cb088a83cd8cdb7756132f9739d',
  29. u'info_dict': {
  30. u"upload_date": u"20121214",
  31. u"description": u"Kristen Stewart",
  32. u"uploader": u"thedailyshow",
  33. u"title": u"thedailyshow-kristen-stewart part 1"
  34. }
  35. }
  36. _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
  37. _video_extensions = {
  38. '3500': 'mp4',
  39. '2200': 'mp4',
  40. '1700': 'mp4',
  41. '1200': 'mp4',
  42. '750': 'mp4',
  43. '400': 'mp4',
  44. }
  45. _video_dimensions = {
  46. '3500': '1280x720',
  47. '2200': '960x540',
  48. '1700': '768x432',
  49. '1200': '640x360',
  50. '750': '512x288',
  51. '400': '384x216',
  52. }
  53. @classmethod
  54. def suitable(cls, url):
  55. """Receives a URL and returns True if suitable for this IE."""
  56. return re.match(cls._VALID_URL, url, re.VERBOSE) is not None
  57. def _print_formats(self, formats):
  58. print('Available formats:')
  59. for x in formats:
  60. print('%s\t:\t%s\t[%s]' %(x, self._video_extensions.get(x, 'mp4'), self._video_dimensions.get(x, '???')))
  61. def _real_extract(self, url):
  62. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  63. if mobj is None:
  64. raise ExtractorError(u'Invalid URL: %s' % url)
  65. if mobj.group('shortname'):
  66. if mobj.group('shortname') in ('tds', 'thedailyshow'):
  67. url = u'http://www.thedailyshow.com/full-episodes/'
  68. else:
  69. url = u'http://www.colbertnation.com/full-episodes/'
  70. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  71. assert mobj is not None
  72. if mobj.group('clip'):
  73. if mobj.group('showname') == 'thedailyshow':
  74. epTitle = mobj.group('tdstitle')
  75. else:
  76. epTitle = mobj.group('cntitle')
  77. dlNewest = False
  78. else:
  79. dlNewest = not mobj.group('episode')
  80. if dlNewest:
  81. epTitle = mobj.group('showname')
  82. else:
  83. epTitle = mobj.group('episode')
  84. self.report_extraction(epTitle)
  85. webpage,htmlHandle = self._download_webpage_handle(url, epTitle)
  86. if dlNewest:
  87. url = htmlHandle.geturl()
  88. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  89. if mobj is None:
  90. raise ExtractorError(u'Invalid redirected URL: ' + url)
  91. if mobj.group('episode') == '':
  92. raise ExtractorError(u'Redirected URL is still not specific: ' + url)
  93. epTitle = mobj.group('episode')
  94. mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
  95. if len(mMovieParams) == 0:
  96. # The Colbert Report embeds the information in a without
  97. # a URL prefix; so extract the alternate reference
  98. # and then add the URL prefix manually.
  99. altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video).*?:.*?)"', webpage)
  100. if len(altMovieParams) == 0:
  101. raise ExtractorError(u'unable to find Flash URL in webpage ' + url)
  102. else:
  103. mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
  104. uri = mMovieParams[0][1]
  105. indexUrl = 'http://shadow.comedycentral.com/feeds/video_player/mrss/?' + compat_urllib_parse.urlencode({'uri': uri})
  106. indexXml = self._download_webpage(indexUrl, epTitle,
  107. u'Downloading show index',
  108. u'unable to download episode index')
  109. results = []
  110. idoc = xml.etree.ElementTree.fromstring(indexXml)
  111. itemEls = idoc.findall('.//item')
  112. for partNum,itemEl in enumerate(itemEls):
  113. mediaId = itemEl.findall('./guid')[0].text
  114. shortMediaId = mediaId.split(':')[-1]
  115. showId = mediaId.split(':')[-2].replace('.com', '')
  116. officialTitle = itemEl.findall('./title')[0].text
  117. officialDate = unified_strdate(itemEl.findall('./pubDate')[0].text)
  118. configUrl = ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' +
  119. compat_urllib_parse.urlencode({'uri': mediaId}))
  120. configXml = self._download_webpage(configUrl, epTitle,
  121. u'Downloading configuration for %s' % shortMediaId)
  122. cdoc = xml.etree.ElementTree.fromstring(configXml)
  123. turls = []
  124. for rendition in cdoc.findall('.//rendition'):
  125. finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
  126. turls.append(finfo)
  127. if len(turls) == 0:
  128. self._downloader.report_error(u'unable to download ' + mediaId + ': No videos found')
  129. continue
  130. if self._downloader.params.get('listformats', None):
  131. self._print_formats([i[0] for i in turls])
  132. return
  133. # For now, just pick the highest bitrate
  134. format,rtmp_video_url = turls[-1]
  135. # Get the format arg from the arg stream
  136. req_format = self._downloader.params.get('format', None)
  137. # Select format if we can find one
  138. for f,v in turls:
  139. if f == req_format:
  140. format, rtmp_video_url = f, v
  141. break
  142. m = re.match(r'^rtmpe?://.*?/(?P<finalid>gsp.comedystor/.*)$', rtmp_video_url)
  143. if not m:
  144. raise ExtractorError(u'Cannot transform RTMP url')
  145. base = 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/'
  146. video_url = base + m.group('finalid')
  147. effTitle = showId + u'-' + epTitle + u' part ' + compat_str(partNum+1)
  148. info = {
  149. 'id': shortMediaId,
  150. 'url': video_url,
  151. 'uploader': showId,
  152. 'upload_date': officialDate,
  153. 'title': effTitle,
  154. 'ext': 'mp4',
  155. 'format': format,
  156. 'thumbnail': None,
  157. 'description': compat_str(officialTitle),
  158. }
  159. results.append(info)
  160. return results