]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/comedycentral.py
   2 import xml
.etree
.ElementTree
 
   4 from .common 
import InfoExtractor
 
  14 class ComedyCentralIE(InfoExtractor
): 
  15     IE_DESC 
= u
'The Daily Show / Colbert Report' 
  16     # urls can be abbreviations like :thedailyshow or :colbert 
  17     # urls for episodes like: 
  18     # or urls for clips like: http://www.thedailyshow.com/watch/mon-december-10-2012/any-given-gun-day 
  19     #                     or: http://www.colbertnation.com/the-colbert-report-videos/421667/november-29-2012/moon-shattering-news 
  20     #                     or: http://www.colbertnation.com/the-colbert-report-collections/422008/festival-of-lights/79524 
  21     _VALID_URL 
= r
"""^(:(?P<shortname>tds|thedailyshow|cr|colbert|colbertnation|colbertreport) 
  23                           (?P<showname>thedailyshow|colbertnation)\.com/ 
  24                          (full-episodes/(?P<episode>.*)| 
  26                               (the-colbert-report-(videos|collections)/(?P<clipID>[0-9]+)/[^/]*/(?P<cntitle>.*?)) 
  27                               |(watch/(?P<date>[^/]*)/(?P<tdstitle>.*))))) 
  30         u
'url': u
'http://www.thedailyshow.com/watch/thu-december-13-2012/kristen-stewart', 
  31         u
'file': u
'422212.mp4', 
  32         u
'md5': u
'4e2f5cb088a83cd8cdb7756132f9739d', 
  34             u
"upload_date": u
"20121214",  
  35             u
"description": u
"Kristen Stewart",  
  36             u
"uploader": u
"thedailyshow",  
  37             u
"title": u
"thedailyshow-kristen-stewart part 1" 
  41     _available_formats 
= ['3500', '2200', '1700', '1200', '750', '400'] 
  61     def suitable(cls
, url
): 
  62         """Receives a URL and returns True if suitable for this IE.""" 
  63         return re
.match(cls
._VALID
_URL
, url
, re
.VERBOSE
) is not None 
  65     def _print_formats(self
, formats
): 
  66         print('Available formats:') 
  68             print('%s\t:\t%s\t[%s]' %(x
, self
._video
_extensions
.get(x
, 'mp4'), self
._video
_dimensions
.get(x
, '???'))) 
  71     def _real_extract(self
, url
): 
  72         mobj 
= re
.match(self
._VALID
_URL
, url
, re
.VERBOSE
) 
  74             raise ExtractorError(u
'Invalid URL: %s' % url
) 
  76         if mobj
.group('shortname'): 
  77             if mobj
.group('shortname') in ('tds', 'thedailyshow'): 
  78                 url 
= u
'http://www.thedailyshow.com/full-episodes/' 
  80                 url 
= u
'http://www.colbertnation.com/full-episodes/' 
  81             mobj 
= re
.match(self
._VALID
_URL
, url
, re
.VERBOSE
) 
  82             assert mobj 
is not None 
  84         if mobj
.group('clip'): 
  85             if mobj
.group('showname') == 'thedailyshow': 
  86                 epTitle 
= mobj
.group('tdstitle') 
  88                 epTitle 
= mobj
.group('cntitle') 
  91             dlNewest 
= not mobj
.group('episode') 
  93                 epTitle 
= mobj
.group('showname') 
  95                 epTitle 
= mobj
.group('episode') 
  97         self
.report_extraction(epTitle
) 
  98         webpage
,htmlHandle 
= self
._download
_webpage
_handle
(url
, epTitle
) 
 100             url 
= htmlHandle
.geturl() 
 101             mobj 
= re
.match(self
._VALID
_URL
, url
, re
.VERBOSE
) 
 103                 raise ExtractorError(u
'Invalid redirected URL: ' + url
) 
 104             if mobj
.group('episode') == '': 
 105                 raise ExtractorError(u
'Redirected URL is still not specific: ' + url
) 
 106             epTitle 
= mobj
.group('episode') 
 108         mMovieParams 
= re
.findall('(?:<param name="movie" value="|var url = ")(http://media.mtvnservices.com/([^"]*(?:episode|video).*?:.*?))"', webpage
) 
 110         if len(mMovieParams
) == 0: 
 111             # The Colbert Report embeds the information in a without 
 112             # a URL prefix; so extract the alternate reference 
 113             # and then add the URL prefix manually. 
 115             altMovieParams 
= re
.findall('data-mgid="([^"]*(?:episode|video).*?:.*?)"', webpage
) 
 116             if len(altMovieParams
) == 0: 
 117                 raise ExtractorError(u
'unable to find Flash URL in webpage ' + url
) 
 119                 mMovieParams 
= [("http://media.mtvnservices.com/" + altMovieParams
[0], altMovieParams
[0])] 
 121         uri 
= mMovieParams
[0][1] 
 122         indexUrl 
= 'http://shadow.comedycentral.com/feeds/video_player/mrss/?' + compat_urllib_parse
.urlencode({'uri': uri
}) 
 123         indexXml 
= self
._download
_webpage
(indexUrl
, epTitle
, 
 124                                           u
'Downloading show index', 
 125                                           u
'unable to download episode index') 
 129         idoc 
= xml
.etree
.ElementTree
.fromstring(indexXml
) 
 130         itemEls 
= idoc
.findall('.//item') 
 131         for partNum
,itemEl 
in enumerate(itemEls
): 
 132             mediaId 
= itemEl
.findall('./guid')[0].text
 
 133             shortMediaId 
= mediaId
.split(':')[-1] 
 134             showId 
= mediaId
.split(':')[-2].replace('.com', '') 
 135             officialTitle 
= itemEl
.findall('./title')[0].text
 
 136             officialDate 
= unified_strdate(itemEl
.findall('./pubDate')[0].text
) 
 138             configUrl 
= ('http://www.comedycentral.com/global/feeds/entertainment/media/mediaGenEntertainment.jhtml?' + 
 139                         compat_urllib_parse
.urlencode({'uri': mediaId
})) 
 140             configXml 
= self
._download
_webpage
(configUrl
, epTitle
, 
 141                                                u
'Downloading configuration for %s' % shortMediaId
) 
 143             cdoc 
= xml
.etree
.ElementTree
.fromstring(configXml
) 
 145             for rendition 
in cdoc
.findall('.//rendition'): 
 146                 finfo 
= (rendition
.attrib
['bitrate'], rendition
.findall('./src')[0].text
) 
 150                 self
._downloader
.report_error(u
'unable to download ' + mediaId 
+ ': No videos found') 
 153             if self
._downloader
.params
.get('listformats', None): 
 154                 self
._print
_formats
([i
[0] for i 
in turls
]) 
 157             # For now, just pick the highest bitrate 
 158             format
,rtmp_video_url 
= turls
[-1] 
 160             # Get the format arg from the arg stream 
 161             req_format 
= self
._downloader
.params
.get('format', None) 
 163             # Select format if we can find one 
 166                     format
, rtmp_video_url 
= f
, v
 
 169             m 
= re
.match(r
'^rtmpe?://.*?/(?P<finalid>gsp.comedystor/.*)$', rtmp_video_url
) 
 171                 raise ExtractorError(u
'Cannot transform RTMP url') 
 172             base 
= 'http://mtvnmobile.vo.llnwd.net/kip0/_pxn=1+_pxI0=Ripod-h264+_pxL0=undefined+_pxM0=+_pxK=18639+_pxE=mp4/44620/mtvnorigin/' 
 173             video_url 
= base 
+ m
.group('finalid') 
 175             effTitle 
= showId 
+ u
'-' + epTitle 
+ u
' part ' + compat_str(partNum
+1) 
 180                 'upload_date': officialDate
, 
 185                 'description': compat_str(officialTitle
),