comedycentral.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from .mtv import MTVServicesInfoExtractor
  5. from ..compat import (
  6. compat_str,
  7. compat_urllib_parse,
  8. )
  9. from ..utils import (
  10. ExtractorError,
  11. float_or_none,
  12. unified_strdate,
  13. )
  14. class ComedyCentralIE(MTVServicesInfoExtractor):
  15. _VALID_URL = r'''(?x)https?://(?:www\.)?cc\.com/
  16. (video-clips|episodes|cc-studios|video-collections|full-episodes)
  17. /(?P<title>.*)'''
  18. _FEED_URL = 'http://comedycentral.com/feeds/mrss/'
  19. _TEST = {
  20. 'url': 'http://www.cc.com/video-clips/kllhuv/stand-up-greg-fitzsimmons--uncensored---too-good-of-a-mother',
  21. 'md5': 'c4f48e9eda1b16dd10add0744344b6d8',
  22. 'info_dict': {
  23. 'id': 'cef0cbb3-e776-4bc9-b62e-8016deccb354',
  24. 'ext': 'mp4',
  25. 'title': 'CC:Stand-Up|Greg Fitzsimmons: Life on Stage|Uncensored - Too Good of a Mother',
  26. 'description': 'After a certain point, breastfeeding becomes c**kblocking.',
  27. },
  28. }
  29. class ComedyCentralShowsIE(MTVServicesInfoExtractor):
  30. IE_DESC = 'The Daily Show / The Colbert Report'
  31. # urls can be abbreviations like :thedailyshow
  32. # urls for episodes like:
  33. # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day
  34. # or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news
  35. # or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524
  36. _VALID_URL = r'''(?x)^(:(?P<shortname>tds|thedailyshow)
  37. |https?://(:www\.)?
  38. (?P<showname>thedailyshow|thecolbertreport)\.(?:cc\.)?com/
  39. ((?:full-)?episodes/(?:[0-9a-z]{6}/)?(?P<episode>.*)|
  40. (?P<clip>
  41. (?:(?:guests/[^/]+|videos|video-playlists|special-editions|news-team/[^/]+)/[^/]+/(?P<videotitle>[^/?#]+))
  42. |(the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?))
  43. |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*))
  44. )|
  45. (?P<interview>
  46. extended-interviews/(?P<interID>[0-9a-z]+)/
  47. (?:playlist_tds_extended_)?(?P<interview_title>[^/?#]*?)
  48. (?:/[^/?#]?|[?#]|$))))
  49. '''
  50. _TESTS = [{
  51. 'url': 'http://thedailyshow.cc.com/watch/thu-december-13-2012/kristen-stewart',
  52. 'md5': '4e2f5cb088a83cd8cdb7756132f9739d',
  53. 'info_dict': {
  54. 'id': 'ab9ab3e7-5a98-4dbe-8b21-551dc0523d55',
  55. 'ext': 'mp4',
  56. 'upload_date': '20121213',
  57. 'description': 'Kristen Stewart learns to let loose in "On the Road."',
  58. 'uploader': 'thedailyshow',
  59. 'title': 'thedailyshow kristen-stewart part 1',
  60. }
  61. }, {
  62. 'url': 'http://thedailyshow.cc.com/extended-interviews/b6364d/sarah-chayes-extended-interview',
  63. 'info_dict': {
  64. 'id': 'sarah-chayes-extended-interview',
  65. 'description': 'Carnegie Endowment Senior Associate Sarah Chayes discusses how corrupt institutions function throughout the world in her book "Thieves of State: Why Corruption Threatens Global Security."',
  66. 'title': 'thedailyshow Sarah Chayes Extended Interview',
  67. },
  68. 'playlist': [
  69. {
  70. 'info_dict': {
  71. 'id': '0baad492-cbec-4ec1-9e50-ad91c291127f',
  72. 'ext': 'mp4',
  73. 'upload_date': '20150129',
  74. 'description': 'Carnegie Endowment Senior Associate Sarah Chayes discusses how corrupt institutions function throughout the world in her book "Thieves of State: Why Corruption Threatens Global Security."',
  75. 'uploader': 'thedailyshow',
  76. 'title': 'thedailyshow sarah-chayes-extended-interview part 1',
  77. },
  78. },
  79. {
  80. 'info_dict': {
  81. 'id': '1e4fb91b-8ce7-4277-bd7c-98c9f1bbd283',
  82. 'ext': 'mp4',
  83. 'upload_date': '20150129',
  84. 'description': 'Carnegie Endowment Senior Associate Sarah Chayes discusses how corrupt institutions function throughout the world in her book "Thieves of State: Why Corruption Threatens Global Security."',
  85. 'uploader': 'thedailyshow',
  86. 'title': 'thedailyshow sarah-chayes-extended-interview part 2',
  87. },
  88. },
  89. ],
  90. 'params': {
  91. 'skip_download': True,
  92. },
  93. }, {
  94. 'url': 'http://thedailyshow.cc.com/extended-interviews/xm3fnq/andrew-napolitano-extended-interview',
  95. 'only_matching': True,
  96. }, {
  97. 'url': 'http://thecolbertreport.cc.com/videos/29w6fx/-realhumanpraise-for-fox-news',
  98. 'only_matching': True,
  99. }, {
  100. 'url': 'http://thecolbertreport.cc.com/videos/gh6urb/neil-degrasse-tyson-pt--1?xrs=eml_col_031114',
  101. 'only_matching': True,
  102. }, {
  103. 'url': 'http://thedailyshow.cc.com/guests/michael-lewis/3efna8/exclusive---michael-lewis-extended-interview-pt--3',
  104. 'only_matching': True,
  105. }, {
  106. 'url': 'http://thedailyshow.cc.com/episodes/sy7yv0/april-8--2014---denis-leary',
  107. 'only_matching': True,
  108. }, {
  109. 'url': 'http://thecolbertreport.cc.com/episodes/8ase07/april-8--2014---jane-goodall',
  110. 'only_matching': True,
  111. }, {
  112. 'url': 'http://thedailyshow.cc.com/video-playlists/npde3s/the-daily-show-19088-highlights',
  113. 'only_matching': True,
  114. }, {
  115. 'url': 'http://thedailyshow.cc.com/video-playlists/t6d9sg/the-daily-show-20038-highlights/be3cwo',
  116. 'only_matching': True,
  117. }, {
  118. 'url': 'http://thedailyshow.cc.com/special-editions/2l8fdb/special-edition---a-look-back-at-food',
  119. 'only_matching': True,
  120. }, {
  121. 'url': 'http://thedailyshow.cc.com/news-team/michael-che/7wnfel/we-need-to-talk-about-israel',
  122. 'only_matching': True,
  123. }]
  124. _available_formats = ['3500', '2200', '1700', '1200', '750', '400']
  125. _video_extensions = {
  126. '3500': 'mp4',
  127. '2200': 'mp4',
  128. '1700': 'mp4',
  129. '1200': 'mp4',
  130. '750': 'mp4',
  131. '400': 'mp4',
  132. }
  133. _video_dimensions = {
  134. '3500': (1280, 720),
  135. '2200': (960, 540),
  136. '1700': (768, 432),
  137. '1200': (640, 360),
  138. '750': (512, 288),
  139. '400': (384, 216),
  140. }
  141. def _real_extract(self, url):
  142. mobj = re.match(self._VALID_URL, url)
  143. if mobj.group('shortname'):
  144. if mobj.group('shortname') in ('tds', 'thedailyshow'):
  145. url = 'http://thedailyshow.cc.com/full-episodes/'
  146. else:
  147. url = 'http://thecolbertreport.cc.com/full-episodes/'
  148. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  149. assert mobj is not None
  150. if mobj.group('clip'):
  151. if mobj.group('videotitle'):
  152. epTitle = mobj.group('videotitle')
  153. elif mobj.group('showname') == 'thedailyshow':
  154. epTitle = mobj.group('tdstitle')
  155. else:
  156. epTitle = mobj.group('cntitle')
  157. dlNewest = False
  158. elif mobj.group('interview'):
  159. epTitle = mobj.group('interview_title')
  160. dlNewest = False
  161. else:
  162. dlNewest = not mobj.group('episode')
  163. if dlNewest:
  164. epTitle = mobj.group('showname')
  165. else:
  166. epTitle = mobj.group('episode')
  167. show_name = mobj.group('showname')
  168. webpage, htmlHandle = self._download_webpage_handle(url, epTitle)
  169. if dlNewest:
  170. url = htmlHandle.geturl()
  171. mobj = re.match(self._VALID_URL, url, re.VERBOSE)
  172. if mobj is None:
  173. raise ExtractorError('Invalid redirected URL: ' + url)
  174. if mobj.group('episode') == '':
  175. raise ExtractorError('Redirected URL is still not specific: ' + url)
  176. epTitle = (mobj.group('episode') or mobj.group('videotitle')).rpartition('/')[-1]
  177. mMovieParams = re.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage)
  178. if len(mMovieParams) == 0:
  179. # The Colbert Report embeds the information in a without
  180. # a URL prefix; so extract the alternate reference
  181. # and then add the URL prefix manually.
  182. altMovieParams = re.findall('data-mgid="([^"]*(?:episode|video|playlist).*?:.*?)"', webpage)
  183. if len(altMovieParams) == 0:
  184. raise ExtractorError('unable to find Flash URL in webpage ' + url)
  185. else:
  186. mMovieParams = [("http://media.mtvnservices.com/" + altMovieParams[0], altMovieParams[0])]
  187. uri = mMovieParams[0][1]
  188. # Correct cc.com in uri
  189. uri = re.sub(r'(episode:[^.]+)(\.cc)?\.com', r'\1.cc.com', uri)
  190. index_url = 'http://%s.cc.com/feeds/mrss?%s' % (show_name, compat_urllib_parse.urlencode({'uri': uri}))
  191. idoc = self._download_xml(
  192. index_url, epTitle,
  193. 'Downloading show index', 'Unable to download episode index')
  194. title = idoc.find('./channel/title').text
  195. description = idoc.find('./channel/description').text
  196. entries = []
  197. item_els = idoc.findall('.//item')
  198. for part_num, itemEl in enumerate(item_els):
  199. upload_date = unified_strdate(itemEl.findall('./pubDate')[0].text)
  200. thumbnail = itemEl.find('.//{http://search.yahoo.com/mrss/}thumbnail').attrib.get('url')
  201. content = itemEl.find('.//{http://search.yahoo.com/mrss/}content')
  202. duration = float_or_none(content.attrib.get('duration'))
  203. mediagen_url = content.attrib['url']
  204. guid = itemEl.find('./guid').text.rpartition(':')[-1]
  205. cdoc = self._download_xml(
  206. mediagen_url, epTitle,
  207. 'Downloading configuration for segment %d / %d' % (part_num + 1, len(item_els)))
  208. turls = []
  209. for rendition in cdoc.findall('.//rendition'):
  210. finfo = (rendition.attrib['bitrate'], rendition.findall('./src')[0].text)
  211. turls.append(finfo)
  212. formats = []
  213. for format, rtmp_video_url in turls:
  214. w, h = self._video_dimensions.get(format, (None, None))
  215. formats.append({
  216. 'format_id': 'vhttp-%s' % format,
  217. 'url': self._transform_rtmp_url(rtmp_video_url),
  218. 'ext': self._video_extensions.get(format, 'mp4'),
  219. 'height': h,
  220. 'width': w,
  221. })
  222. formats.append({
  223. 'format_id': 'rtmp-%s' % format,
  224. 'url': rtmp_video_url.replace('viacomccstrm', 'viacommtvstrm'),
  225. 'ext': self._video_extensions.get(format, 'mp4'),
  226. 'height': h,
  227. 'width': w,
  228. })
  229. self._sort_formats(formats)
  230. subtitles = self._extract_subtitles(cdoc, guid)
  231. virtual_id = show_name + ' ' + epTitle + ' part ' + compat_str(part_num + 1)
  232. entries.append({
  233. 'id': guid,
  234. 'title': virtual_id,
  235. 'formats': formats,
  236. 'uploader': show_name,
  237. 'upload_date': upload_date,
  238. 'duration': duration,
  239. 'thumbnail': thumbnail,
  240. 'description': description,
  241. 'subtitles': subtitles,
  242. })
  243. return {
  244. '_type': 'playlist',
  245. 'id': epTitle,
  246. 'entries': entries,
  247. 'title': show_name + ' ' + title,
  248. 'description': description,
  249. }
  250. class TheDailyShowPodcastIE(InfoExtractor):
  251. _VALID_URL = r'(?P<scheme>https?:)?//thedailyshow\.cc\.com/podcast/(?P<id>[a-z\-]+)'
  252. def _real_extract(self, url):
  253. display_id = self._match_id(url)
  254. webpage = self._download_webpage(url, display_id)
  255. player_url = self._search_regex(r'<iframe(?:\s+[^>]+)?\s*src="((?:https?:)?//html5-player\.libsyn\.com/embed/episode/id/[0-9]+)', webpage, 'player URL')
  256. if player_url.startswith('//'):
  257. mobj = re.match(self._VALID_URL, url)
  258. scheme = mobj.group('scheme')
  259. if not scheme:
  260. scheme = 'https:'
  261. player_url = scheme + player_url
  262. return {
  263. '_type': 'url_transparent',
  264. 'url': player_url,
  265. }