]>
Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/bbccouk.py
beb6cfc8ae88a3c40ac2b4ee7f6b0ae2c6ddfc87
1 from __future__
import unicode_literals
4 import xml
. etree
. ElementTree
6 from . subtitles
import SubtitlesInfoExtractor
7 from .. utils
import ExtractorError
8 from .. compat
import compat_HTTPError
11 class BBCCoUkIE ( SubtitlesInfoExtractor
):
13 IE_DESC
= 'BBC iPlayer'
14 _VALID_URL
= r
'https?://(?:www\.)?bbc\.co\.uk/(?:programmes|iplayer/episode)/(?P<id>[\da-z] {8} )'
18 'url' : 'http://www.bbc.co.uk/programmes/b039g8p7' ,
22 'title' : 'Kaleidoscope: Leonard Cohen' ,
23 'description' : 'md5:db4755d7a665ae72343779f7dacb402c' ,
28 'skip_download' : True ,
32 'url' : 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/' ,
36 'title' : 'The Man in Black: Series 3: The Printed Name' ,
37 'description' : "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey." ,
42 'skip_download' : True ,
44 'skip' : 'Episode is no longer available on BBC iPlayer Radio' ,
47 'url' : 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/' ,
51 'title' : 'The Voice UK: Series 3: Blind Auditions 5' ,
52 'description' : "Emma Willis and Marvin Humes present the fifth set of blind auditions in the singing competition, as the coaches continue to build their teams based on voice alone." ,
57 'skip_download' : True ,
59 'skip' : 'Currently BBC iPlayer TV programmes are available to play in the UK only' ,
62 'url' : 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion' ,
66 'title' : "Tomorrow's Worlds: The Unearthly History of Science Fiction" ,
67 'description' : '2. Invasion' ,
72 'skip_download' : True ,
74 'skip' : 'Currently BBC iPlayer TV programmes are available to play in the UK only' ,
78 def _extract_asx_playlist ( self
, connection
, programme_id
):
79 asx
= self
._ download
_ xml
( connection
. get ( 'href' ), programme_id
, 'Downloading ASX playlist' )
80 return [ ref
. get ( 'href' ) for ref
in asx
. findall ( './Entry/ref' )]
82 def _extract_connection ( self
, connection
, programme_id
):
84 protocol
= connection
. get ( 'protocol' )
85 supplier
= connection
. get ( 'supplier' )
86 if protocol
== 'http' :
87 href
= connection
. get ( 'href' )
90 for i
, ref
in enumerate ( self
._ extract
_ asx
_ playlist
( connection
, programme_id
)):
93 'format_id' : 'ref %s _ %s ' % ( i
, supplier
),
99 'format_id' : supplier
,
101 elif protocol
== 'rtmp' :
102 application
= connection
. get ( 'application' , 'ondemand' )
103 auth_string
= connection
. get ( 'authString' )
104 identifier
= connection
. get ( 'identifier' )
105 server
= connection
. get ( 'server' )
107 'url' : ' %s :// %s / %s ? %s ' % ( protocol
, server
, application
, auth_string
),
108 'play_path' : identifier
,
109 'app' : ' %s ? %s ' % ( application
, auth_string
),
110 'page_url' : 'http://www.bbc.co.uk' ,
111 'player_url' : 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf' ,
114 'format_id' : supplier
,
118 def _extract_items ( self
, playlist
):
119 return playlist
. findall ( './{http://bbc.co.uk/2008/emp/playlist}item' )
121 def _extract_medias ( self
, media_selection
):
122 error
= media_selection
. find ( './{http://bbc.co.uk/2008/mp/mediaselection}error' )
123 if error
is not None :
124 raise ExtractorError (
125 ' %s returned error: %s ' % ( self
. IE_NAME
, error
. get ( 'id' )), expected
= True )
126 return media_selection
. findall ( './{http://bbc.co.uk/2008/mp/mediaselection}media' )
128 def _extract_connections ( self
, media
):
129 return media
. findall ( './{http://bbc.co.uk/2008/mp/mediaselection}connection' )
131 def _extract_video ( self
, media
, programme_id
):
133 vbr
= int ( media
. get ( 'bitrate' ))
134 vcodec
= media
. get ( 'encoding' )
135 service
= media
. get ( 'service' )
136 width
= int ( media
. get ( 'width' ))
137 height
= int ( media
. get ( 'height' ))
138 file_size
= int ( media
. get ( 'media_file_size' ))
139 for connection
in self
._ extract
_ connections
( media
):
140 conn_formats
= self
._ extract
_ connection
( connection
, programme_id
)
141 for format
in conn_formats
:
143 'format_id' : ' %s _ %s ' % ( service
, format
[ 'format_id' ]),
148 'filesize' : file_size
,
150 formats
. extend ( conn_formats
)
153 def _extract_audio ( self
, media
, programme_id
):
155 abr
= int ( media
. get ( 'bitrate' ))
156 acodec
= media
. get ( 'encoding' )
157 service
= media
. get ( 'service' )
158 for connection
in self
._ extract
_ connections
( media
):
159 conn_formats
= self
._ extract
_ connection
( connection
, programme_id
)
160 for format
in conn_formats
:
162 'format_id' : ' %s _ %s ' % ( service
, format
[ 'format_id' ]),
166 formats
. extend ( conn_formats
)
169 def _extract_captions ( self
, media
, programme_id
):
171 for connection
in self
._ extract
_ connections
( media
):
172 captions
= self
._ download
_ xml
( connection
. get ( 'href' ), programme_id
, 'Downloading captions' )
173 lang
= captions
. get ( '{http://www.w3.org/XML/1998/namespace}lang' , 'en' )
174 ps
= captions
. findall ( './ {0} body/ {0} div/ {0} p' . format ( '{http://www.w3.org/2006/10/ttaf1}' ))
176 for pos
, p
in enumerate ( ps
):
177 srt
+= ' %s \r\n %s --> %s \r\n %s \r\n\r\n ' % ( str ( pos
), p
. get ( 'begin' ), p
. get ( 'end' ),
178 p
. text
. strip () if p
. text
is not None else '' )
179 subtitles
[ lang
] = srt
182 def _download_media_selector ( self
, programme_id
):
184 media_selection
= self
._ download
_ xml
(
185 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/ %s ' % programme_id
,
186 programme_id
, 'Downloading media selection XML' )
187 except ExtractorError
as ee
:
188 if isinstance ( ee
. cause
, compat_HTTPError
) and ee
. cause
. code
== 403 :
189 media_selection
= xml
. etree
. ElementTree
. fromstring ( ee
. cause
. read (). encode ( 'utf-8' ))
196 for media
in self
._ extract
_ medias
( media_selection
):
197 kind
= media
. get ( 'kind' )
199 formats
. extend ( self
._ extract
_ audio
( media
, programme_id
))
200 elif kind
== 'video' :
201 formats
. extend ( self
._ extract
_ video
( media
, programme_id
))
202 elif kind
== 'captions' :
203 subtitles
= self
._ extract
_ captions
( media
, programme_id
)
205 return formats
, subtitles
207 def _real_extract ( self
, url
):
208 group_id
= self
._ match
_ id
( url
)
210 webpage
= self
._ download
_ webpage
( url
, group_id
, 'Downloading video page' )
212 programme_id
= self
._ search
_ regex
(
213 r
'"vpid"\s*:\s*"([\da-z] {8} )"' , webpage
, 'vpid' , fatal
= False )
215 player
= self
._ download
_ json
(
216 'http://www.bbc.co.uk/iplayer/episode/ %s .json' % group_id
,
217 group_id
)[ 'jsConf' ][ 'player' ]
218 title
= player
[ 'title' ]
219 description
= player
[ 'subtitle' ]
220 duration
= player
[ 'duration' ]
221 formats
, subtitles
= self
._ download
_ media
_ selector
( programme_id
)
223 playlist
= self
._ download
_ xml
(
224 'http://www.bbc.co.uk/iplayer/playlist/ %s ' % group_id
,
225 group_id
, 'Downloading playlist XML' )
227 no_items
= playlist
. find ( './{http://bbc.co.uk/2008/emp/playlist}noItems' )
228 if no_items
is not None :
229 reason
= no_items
. get ( 'reason' )
230 if reason
== 'preAvailability' :
231 msg
= 'Episode %s is not yet available' % group_id
232 elif reason
== 'postAvailability' :
233 msg
= 'Episode %s is no longer available' % group_id
234 elif reason
== 'noMedia' :
235 msg
= 'Episode %s is not currently available' % group_id
237 msg
= 'Episode %s is not available: %s ' % ( group_id
, reason
)
238 raise ExtractorError ( msg
, expected
= True )
240 for item
in self
._ extract
_ items
( playlist
):
241 kind
= item
. get ( 'kind' )
242 if kind
!= 'programme' and kind
!= 'radioProgramme' :
244 title
= playlist
. find ( './{http://bbc.co.uk/2008/emp/playlist}title' ). text
245 description
= playlist
. find ( './{http://bbc.co.uk/2008/emp/playlist}summary' ). text
246 programme_id
= item
. get ( 'identifier' )
247 duration
= int ( item
. get ( 'duration' ))
248 formats
, subtitles
= self
._ download
_ media
_ selector
( programme_id
)
250 if self
._ downloader
. params
. get ( 'listsubtitles' , False ):
251 self
._l ist
_ available
_ subtitles
( programme_id
, subtitles
)
254 self
._ sort
_ formats
( formats
)
259 'description' : description
,
260 'duration' : duration
,
262 'subtitles' : subtitles
,