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.

351 lines
14 KiB

10 years ago
10 years ago
  1. from __future__ import unicode_literals
  2. import xml.etree.ElementTree
  3. from .common import InfoExtractor
  4. from ..utils import ExtractorError
  5. from ..compat import compat_HTTPError
  6. class BBCCoUkIE(InfoExtractor):
  7. IE_NAME = 'bbc.co.uk'
  8. IE_DESC = 'BBC iPlayer'
  9. _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:(?:(?:programmes|iplayer(?:/[^/]+)?/(?:episode|playlist))/)|music/clips[/#])(?P<id>[\da-z]{8})'
  10. _TESTS = [
  11. {
  12. 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
  13. 'info_dict': {
  14. 'id': 'b039d07m',
  15. 'ext': 'flv',
  16. 'title': 'Kaleidoscope, Leonard Cohen',
  17. 'description': 'The Canadian poet and songwriter reflects on his musical career.',
  18. 'duration': 1740,
  19. },
  20. 'params': {
  21. # rtmp download
  22. 'skip_download': True,
  23. }
  24. },
  25. {
  26. 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
  27. 'info_dict': {
  28. 'id': 'b00yng1d',
  29. 'ext': 'flv',
  30. 'title': 'The Man in Black: Series 3: The Printed Name',
  31. 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
  32. 'duration': 1800,
  33. },
  34. 'params': {
  35. # rtmp download
  36. 'skip_download': True,
  37. },
  38. 'skip': 'Episode is no longer available on BBC iPlayer Radio',
  39. },
  40. {
  41. 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
  42. 'info_dict': {
  43. 'id': 'b00yng1d',
  44. 'ext': 'flv',
  45. 'title': 'The Voice UK: Series 3: Blind Auditions 5',
  46. '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.",
  47. 'duration': 5100,
  48. },
  49. 'params': {
  50. # rtmp download
  51. 'skip_download': True,
  52. },
  53. 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
  54. },
  55. {
  56. 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
  57. 'info_dict': {
  58. 'id': 'b03k3pb7',
  59. 'ext': 'flv',
  60. 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
  61. 'description': '2. Invasion',
  62. 'duration': 3600,
  63. },
  64. 'params': {
  65. # rtmp download
  66. 'skip_download': True,
  67. },
  68. 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
  69. }, {
  70. 'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
  71. 'info_dict': {
  72. 'id': 'b04v209v',
  73. 'ext': 'flv',
  74. 'title': 'Pete Tong, The Essential New Tune Special',
  75. 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
  76. 'duration': 10800,
  77. },
  78. 'params': {
  79. # rtmp download
  80. 'skip_download': True,
  81. }
  82. }, {
  83. 'url': 'http://www.bbc.co.uk/music/clips/p02frcc3',
  84. 'note': 'Audio',
  85. 'info_dict': {
  86. 'id': 'p02frcch',
  87. 'ext': 'flv',
  88. 'title': 'Pete Tong, Past, Present and Future Special, Madeon - After Hours mix',
  89. 'description': 'French house superstar Madeon takes us out of the club and onto the after party.',
  90. 'duration': 3507,
  91. },
  92. 'params': {
  93. # rtmp download
  94. 'skip_download': True,
  95. }
  96. }, {
  97. 'url': 'http://www.bbc.co.uk/music/clips/p025c0zz',
  98. 'note': 'Video',
  99. 'info_dict': {
  100. 'id': 'p025c103',
  101. 'ext': 'flv',
  102. 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)',
  103. 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014',
  104. 'duration': 226,
  105. },
  106. 'params': {
  107. # rtmp download
  108. 'skip_download': True,
  109. }
  110. }, {
  111. 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
  112. 'only_matching': True,
  113. }, {
  114. 'url': 'http://www.bbc.co.uk/music/clips#p02frcc3',
  115. 'only_matching': True,
  116. }, {
  117. 'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo',
  118. 'only_matching': True,
  119. }
  120. ]
  121. def _extract_asx_playlist(self, connection, programme_id):
  122. asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
  123. return [ref.get('href') for ref in asx.findall('./Entry/ref')]
  124. def _extract_connection(self, connection, programme_id):
  125. formats = []
  126. protocol = connection.get('protocol')
  127. supplier = connection.get('supplier')
  128. if protocol == 'http':
  129. href = connection.get('href')
  130. # ASX playlist
  131. if supplier == 'asx':
  132. for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
  133. formats.append({
  134. 'url': ref,
  135. 'format_id': 'ref%s_%s' % (i, supplier),
  136. })
  137. # Direct link
  138. else:
  139. formats.append({
  140. 'url': href,
  141. 'format_id': supplier,
  142. })
  143. elif protocol == 'rtmp':
  144. application = connection.get('application', 'ondemand')
  145. auth_string = connection.get('authString')
  146. identifier = connection.get('identifier')
  147. server = connection.get('server')
  148. formats.append({
  149. 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
  150. 'play_path': identifier,
  151. 'app': '%s?%s' % (application, auth_string),
  152. 'page_url': 'http://www.bbc.co.uk',
  153. 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
  154. 'rtmp_live': False,
  155. 'ext': 'flv',
  156. 'format_id': supplier,
  157. })
  158. return formats
  159. def _extract_items(self, playlist):
  160. return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
  161. def _extract_medias(self, media_selection):
  162. error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
  163. if error is not None:
  164. raise ExtractorError(
  165. '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
  166. return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
  167. def _extract_connections(self, media):
  168. return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
  169. def _extract_video(self, media, programme_id):
  170. formats = []
  171. vbr = int(media.get('bitrate'))
  172. vcodec = media.get('encoding')
  173. service = media.get('service')
  174. width = int(media.get('width'))
  175. height = int(media.get('height'))
  176. file_size = int(media.get('media_file_size'))
  177. for connection in self._extract_connections(media):
  178. conn_formats = self._extract_connection(connection, programme_id)
  179. for format in conn_formats:
  180. format.update({
  181. 'format_id': '%s_%s' % (service, format['format_id']),
  182. 'width': width,
  183. 'height': height,
  184. 'vbr': vbr,
  185. 'vcodec': vcodec,
  186. 'filesize': file_size,
  187. })
  188. formats.extend(conn_formats)
  189. return formats
  190. def _extract_audio(self, media, programme_id):
  191. formats = []
  192. abr = int(media.get('bitrate'))
  193. acodec = media.get('encoding')
  194. service = media.get('service')
  195. for connection in self._extract_connections(media):
  196. conn_formats = self._extract_connection(connection, programme_id)
  197. for format in conn_formats:
  198. format.update({
  199. 'format_id': '%s_%s' % (service, format['format_id']),
  200. 'abr': abr,
  201. 'acodec': acodec,
  202. })
  203. formats.extend(conn_formats)
  204. return formats
  205. def _get_subtitles(self, media, programme_id):
  206. subtitles = {}
  207. for connection in self._extract_connections(media):
  208. captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
  209. lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
  210. ps = captions.findall('./{0}body/{0}div/{0}p'.format('{http://www.w3.org/2006/10/ttaf1}'))
  211. srt = ''
  212. def _extract_text(p):
  213. if p.text is not None:
  214. stripped_text = p.text.strip()
  215. if stripped_text:
  216. return stripped_text
  217. return ' '.join(span.text.strip() for span in p.findall('{http://www.w3.org/2006/10/ttaf1}span'))
  218. for pos, p in enumerate(ps):
  219. srt += '%s\r\n%s --> %s\r\n%s\r\n\r\n' % (str(pos), p.get('begin'), p.get('end'), _extract_text(p))
  220. subtitles[lang] = [
  221. {
  222. 'url': connection.get('href'),
  223. 'ext': 'ttml',
  224. },
  225. {
  226. 'data': srt,
  227. 'ext': 'srt',
  228. },
  229. ]
  230. return subtitles
  231. def _download_media_selector(self, programme_id):
  232. try:
  233. media_selection = self._download_xml(
  234. 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s' % programme_id,
  235. programme_id, 'Downloading media selection XML')
  236. except ExtractorError as ee:
  237. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
  238. media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().encode('utf-8'))
  239. else:
  240. raise
  241. formats = []
  242. subtitles = None
  243. for media in self._extract_medias(media_selection):
  244. kind = media.get('kind')
  245. if kind == 'audio':
  246. formats.extend(self._extract_audio(media, programme_id))
  247. elif kind == 'video':
  248. formats.extend(self._extract_video(media, programme_id))
  249. elif kind == 'captions':
  250. subtitles = self.extract_subtitles(media, programme_id)
  251. return formats, subtitles
  252. def _download_playlist(self, playlist_id):
  253. try:
  254. playlist = self._download_json(
  255. 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
  256. playlist_id, 'Downloading playlist JSON')
  257. version = playlist.get('defaultAvailableVersion')
  258. if version:
  259. smp_config = version['smpConfig']
  260. title = smp_config['title']
  261. description = smp_config['summary']
  262. for item in smp_config['items']:
  263. kind = item['kind']
  264. if kind != 'programme' and kind != 'radioProgramme':
  265. continue
  266. programme_id = item.get('vpid')
  267. duration = int(item.get('duration'))
  268. formats, subtitles = self._download_media_selector(programme_id)
  269. return programme_id, title, description, duration, formats, subtitles
  270. except ExtractorError as ee:
  271. if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404):
  272. raise
  273. # fallback to legacy playlist
  274. playlist = self._download_xml(
  275. 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id,
  276. playlist_id, 'Downloading legacy playlist XML')
  277. no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
  278. if no_items is not None:
  279. reason = no_items.get('reason')
  280. if reason == 'preAvailability':
  281. msg = 'Episode %s is not yet available' % playlist_id
  282. elif reason == 'postAvailability':
  283. msg = 'Episode %s is no longer available' % playlist_id
  284. elif reason == 'noMedia':
  285. msg = 'Episode %s is not currently available' % playlist_id
  286. else:
  287. msg = 'Episode %s is not available: %s' % (playlist_id, reason)
  288. raise ExtractorError(msg, expected=True)
  289. for item in self._extract_items(playlist):
  290. kind = item.get('kind')
  291. if kind != 'programme' and kind != 'radioProgramme':
  292. continue
  293. title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
  294. description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
  295. programme_id = item.get('identifier')
  296. duration = int(item.get('duration'))
  297. formats, subtitles = self._download_media_selector(programme_id)
  298. return programme_id, title, description, duration, formats, subtitles
  299. def _real_extract(self, url):
  300. group_id = self._match_id(url)
  301. webpage = self._download_webpage(url, group_id, 'Downloading video page')
  302. programme_id = self._search_regex(
  303. r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None)
  304. if programme_id:
  305. player = self._download_json(
  306. 'http://www.bbc.co.uk/iplayer/episode/%s.json' % group_id,
  307. group_id)['jsConf']['player']
  308. title = player['title']
  309. description = player['subtitle']
  310. duration = player['duration']
  311. formats, subtitles = self._download_media_selector(programme_id)
  312. else:
  313. programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
  314. self._sort_formats(formats)
  315. return {
  316. 'id': programme_id,
  317. 'title': title,
  318. 'description': description,
  319. 'duration': duration,
  320. 'formats': formats,
  321. 'subtitles': subtitles,
  322. }