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.

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