comedycentral.py 12 KB

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