bbc.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import re
  4. import xml.etree.ElementTree
  5. from .common import InfoExtractor
  6. from ..utils import (
  7. ExtractorError,
  8. float_or_none,
  9. int_or_none,
  10. parse_duration,
  11. parse_iso8601,
  12. )
  13. from ..compat import compat_HTTPError
  14. class BBCCoUkIE(InfoExtractor):
  15. IE_NAME = 'bbc.co.uk'
  16. IE_DESC = 'BBC iPlayer'
  17. _VALID_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:(?:(?:programmes|iplayer(?:/[^/]+)?/(?:episode|playlist))/)|music/clips[/#])(?P<id>[\da-z]{8})'
  18. _MEDIASELECTOR_URL = 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/pc/vpid/%s'
  19. _TESTS = [
  20. {
  21. 'url': 'http://www.bbc.co.uk/programmes/b039g8p7',
  22. 'info_dict': {
  23. 'id': 'b039d07m',
  24. 'ext': 'flv',
  25. 'title': 'Kaleidoscope, Leonard Cohen',
  26. 'description': 'The Canadian poet and songwriter reflects on his musical career.',
  27. 'duration': 1740,
  28. },
  29. 'params': {
  30. # rtmp download
  31. 'skip_download': True,
  32. }
  33. },
  34. {
  35. 'url': 'http://www.bbc.co.uk/iplayer/episode/b00yng5w/The_Man_in_Black_Series_3_The_Printed_Name/',
  36. 'info_dict': {
  37. 'id': 'b00yng1d',
  38. 'ext': 'flv',
  39. 'title': 'The Man in Black: Series 3: The Printed Name',
  40. 'description': "Mark Gatiss introduces Nicholas Pierpan's chilling tale of a writer's devilish pact with a mysterious man. Stars Ewan Bailey.",
  41. 'duration': 1800,
  42. },
  43. 'params': {
  44. # rtmp download
  45. 'skip_download': True,
  46. },
  47. 'skip': 'Episode is no longer available on BBC iPlayer Radio',
  48. },
  49. {
  50. 'url': 'http://www.bbc.co.uk/iplayer/episode/b03vhd1f/The_Voice_UK_Series_3_Blind_Auditions_5/',
  51. 'info_dict': {
  52. 'id': 'b00yng1d',
  53. 'ext': 'flv',
  54. 'title': 'The Voice UK: Series 3: Blind Auditions 5',
  55. '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.",
  56. 'duration': 5100,
  57. },
  58. 'params': {
  59. # rtmp download
  60. 'skip_download': True,
  61. },
  62. 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
  63. },
  64. {
  65. 'url': 'http://www.bbc.co.uk/iplayer/episode/p026c7jt/tomorrows-worlds-the-unearthly-history-of-science-fiction-2-invasion',
  66. 'info_dict': {
  67. 'id': 'b03k3pb7',
  68. 'ext': 'flv',
  69. 'title': "Tomorrow's Worlds: The Unearthly History of Science Fiction",
  70. 'description': '2. Invasion',
  71. 'duration': 3600,
  72. },
  73. 'params': {
  74. # rtmp download
  75. 'skip_download': True,
  76. },
  77. 'skip': 'Currently BBC iPlayer TV programmes are available to play in the UK only',
  78. }, {
  79. 'url': 'http://www.bbc.co.uk/programmes/b04v20dw',
  80. 'info_dict': {
  81. 'id': 'b04v209v',
  82. 'ext': 'flv',
  83. 'title': 'Pete Tong, The Essential New Tune Special',
  84. 'description': "Pete has a very special mix - all of 2014's Essential New Tunes!",
  85. 'duration': 10800,
  86. },
  87. 'params': {
  88. # rtmp download
  89. 'skip_download': True,
  90. }
  91. }, {
  92. 'url': 'http://www.bbc.co.uk/music/clips/p02frcc3',
  93. 'note': 'Audio',
  94. 'info_dict': {
  95. 'id': 'p02frcch',
  96. 'ext': 'flv',
  97. 'title': 'Pete Tong, Past, Present and Future Special, Madeon - After Hours mix',
  98. 'description': 'French house superstar Madeon takes us out of the club and onto the after party.',
  99. 'duration': 3507,
  100. },
  101. 'params': {
  102. # rtmp download
  103. 'skip_download': True,
  104. }
  105. }, {
  106. 'url': 'http://www.bbc.co.uk/music/clips/p025c0zz',
  107. 'note': 'Video',
  108. 'info_dict': {
  109. 'id': 'p025c103',
  110. 'ext': 'flv',
  111. 'title': 'Reading and Leeds Festival, 2014, Rae Morris - Closer (Live on BBC Three)',
  112. 'description': 'Rae Morris performs Closer for BBC Three at Reading 2014',
  113. 'duration': 226,
  114. },
  115. 'params': {
  116. # rtmp download
  117. 'skip_download': True,
  118. }
  119. }, {
  120. 'url': 'http://www.bbc.co.uk/iplayer/episode/b054fn09/ad/natural-world-20152016-2-super-powered-owls',
  121. 'info_dict': {
  122. 'id': 'p02n76xf',
  123. 'ext': 'flv',
  124. 'title': 'Natural World, 2015-2016: 2. Super Powered Owls',
  125. 'description': 'md5:e4db5c937d0e95a7c6b5e654d429183d',
  126. 'duration': 3540,
  127. },
  128. 'params': {
  129. # rtmp download
  130. 'skip_download': True,
  131. },
  132. 'skip': 'geolocation',
  133. }, {
  134. 'url': 'http://www.bbc.co.uk/iplayer/episode/b05zmgwn/royal-academy-summer-exhibition',
  135. 'info_dict': {
  136. 'id': 'b05zmgw1',
  137. 'ext': 'flv',
  138. 'description': 'Kirsty Wark and Morgan Quaintance visit the Royal Academy as it prepares for its annual artistic extravaganza, meeting people who have come together to make the show unique.',
  139. 'title': 'Royal Academy Summer Exhibition',
  140. 'duration': 3540,
  141. },
  142. 'params': {
  143. # rtmp download
  144. 'skip_download': True,
  145. },
  146. 'skip': 'geolocation',
  147. }, {
  148. 'url': 'http://www.bbc.co.uk/iplayer/playlist/p01dvks4',
  149. 'only_matching': True,
  150. }, {
  151. 'url': 'http://www.bbc.co.uk/music/clips#p02frcc3',
  152. 'only_matching': True,
  153. }, {
  154. 'url': 'http://www.bbc.co.uk/iplayer/cbeebies/episode/b0480276/bing-14-atchoo',
  155. 'only_matching': True,
  156. }
  157. ]
  158. def _extract_asx_playlist(self, connection, programme_id):
  159. asx = self._download_xml(connection.get('href'), programme_id, 'Downloading ASX playlist')
  160. return [ref.get('href') for ref in asx.findall('./Entry/ref')]
  161. def _extract_connection(self, connection, programme_id):
  162. formats = []
  163. protocol = connection.get('protocol')
  164. supplier = connection.get('supplier')
  165. if protocol == 'http':
  166. href = connection.get('href')
  167. transfer_format = connection.get('transferFormat')
  168. # ASX playlist
  169. if supplier == 'asx':
  170. for i, ref in enumerate(self._extract_asx_playlist(connection, programme_id)):
  171. formats.append({
  172. 'url': ref,
  173. 'format_id': 'ref%s_%s' % (i, supplier),
  174. })
  175. # Skip DASH until supported
  176. elif transfer_format == 'dash':
  177. pass
  178. # Direct link
  179. else:
  180. formats.append({
  181. 'url': href,
  182. 'format_id': supplier,
  183. })
  184. elif protocol == 'rtmp':
  185. application = connection.get('application', 'ondemand')
  186. auth_string = connection.get('authString')
  187. identifier = connection.get('identifier')
  188. server = connection.get('server')
  189. formats.append({
  190. 'url': '%s://%s/%s?%s' % (protocol, server, application, auth_string),
  191. 'play_path': identifier,
  192. 'app': '%s?%s' % (application, auth_string),
  193. 'page_url': 'http://www.bbc.co.uk',
  194. 'player_url': 'http://www.bbc.co.uk/emp/releases/iplayer/revisions/617463_618125_4/617463_618125_4_emp.swf',
  195. 'rtmp_live': False,
  196. 'ext': 'flv',
  197. 'format_id': supplier,
  198. })
  199. return formats
  200. def _extract_items(self, playlist):
  201. return playlist.findall('./{http://bbc.co.uk/2008/emp/playlist}item')
  202. def _extract_medias(self, media_selection):
  203. error = media_selection.find('./{http://bbc.co.uk/2008/mp/mediaselection}error')
  204. if error is not None:
  205. raise ExtractorError(
  206. '%s returned error: %s' % (self.IE_NAME, error.get('id')), expected=True)
  207. return media_selection.findall('./{http://bbc.co.uk/2008/mp/mediaselection}media')
  208. def _extract_connections(self, media):
  209. return media.findall('./{http://bbc.co.uk/2008/mp/mediaselection}connection')
  210. def _extract_video(self, media, programme_id):
  211. formats = []
  212. vbr = int_or_none(media.get('bitrate'))
  213. vcodec = media.get('encoding')
  214. service = media.get('service')
  215. width = int_or_none(media.get('width'))
  216. height = int_or_none(media.get('height'))
  217. file_size = int_or_none(media.get('media_file_size'))
  218. for connection in self._extract_connections(media):
  219. conn_formats = self._extract_connection(connection, programme_id)
  220. for format in conn_formats:
  221. format.update({
  222. 'format_id': '%s_%s' % (service, format['format_id']),
  223. 'width': width,
  224. 'height': height,
  225. 'vbr': vbr,
  226. 'vcodec': vcodec,
  227. 'filesize': file_size,
  228. })
  229. formats.extend(conn_formats)
  230. return formats
  231. def _extract_audio(self, media, programme_id):
  232. formats = []
  233. abr = int_or_none(media.get('bitrate'))
  234. acodec = media.get('encoding')
  235. service = media.get('service')
  236. for connection in self._extract_connections(media):
  237. conn_formats = self._extract_connection(connection, programme_id)
  238. for format in conn_formats:
  239. format.update({
  240. 'format_id': '%s_%s' % (service, format['format_id']),
  241. 'abr': abr,
  242. 'acodec': acodec,
  243. })
  244. formats.extend(conn_formats)
  245. return formats
  246. def _get_subtitles(self, media, programme_id):
  247. subtitles = {}
  248. for connection in self._extract_connections(media):
  249. captions = self._download_xml(connection.get('href'), programme_id, 'Downloading captions')
  250. lang = captions.get('{http://www.w3.org/XML/1998/namespace}lang', 'en')
  251. subtitles[lang] = [
  252. {
  253. 'url': connection.get('href'),
  254. 'ext': 'ttml',
  255. },
  256. ]
  257. return subtitles
  258. def _download_media_selector(self, programme_id):
  259. try:
  260. return self._download_media_selector_url(
  261. self._MEDIASELECTOR_URL % programme_id, programme_id)
  262. except ExtractorError as e:
  263. if hasattr(self, '_MEDIASELECTOR_ALT_URL') and str(e) == 'bbc returned error: notukerror':
  264. # notukerror on bbc.com/travel using bbc news mediaselector: fallback to /mediaselector/5/
  265. return self._download_media_selector_url(
  266. self._MEDIASELECTOR_ALT_URL % programme_id, programme_id)
  267. else:
  268. raise
  269. def _download_media_selector_url(self, url, programme_id=None):
  270. try:
  271. media_selection = self._download_xml(
  272. url, programme_id, 'Downloading media selection XML')
  273. except ExtractorError as ee:
  274. if isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 403:
  275. media_selection = xml.etree.ElementTree.fromstring(ee.cause.read().decode('utf-8'))
  276. else:
  277. raise
  278. return self._process_media_selector(media_selection, programme_id)
  279. def _process_media_selector(self, media_selection, programme_id):
  280. formats = []
  281. subtitles = None
  282. for media in self._extract_medias(media_selection):
  283. kind = media.get('kind')
  284. if kind == 'audio':
  285. formats.extend(self._extract_audio(media, programme_id))
  286. elif kind == 'video':
  287. formats.extend(self._extract_video(media, programme_id))
  288. elif kind == 'captions':
  289. subtitles = self.extract_subtitles(media, programme_id)
  290. return formats, subtitles
  291. def _download_playlist(self, playlist_id):
  292. try:
  293. playlist = self._download_json(
  294. 'http://www.bbc.co.uk/programmes/%s/playlist.json' % playlist_id,
  295. playlist_id, 'Downloading playlist JSON')
  296. version = playlist.get('defaultAvailableVersion')
  297. if version:
  298. smp_config = version['smpConfig']
  299. title = smp_config['title']
  300. description = smp_config['summary']
  301. for item in smp_config['items']:
  302. kind = item['kind']
  303. if kind != 'programme' and kind != 'radioProgramme':
  304. continue
  305. programme_id = item.get('vpid')
  306. duration = int_or_none(item.get('duration'))
  307. formats, subtitles = self._download_media_selector(programme_id)
  308. return programme_id, title, description, duration, formats, subtitles
  309. except ExtractorError as ee:
  310. if not (isinstance(ee.cause, compat_HTTPError) and ee.cause.code == 404):
  311. raise
  312. # fallback to legacy playlist
  313. return self._process_legacy_playlist(playlist_id)
  314. def _process_legacy_playlist_url(self, url, display_id):
  315. playlist = self._download_legacy_playlist_url(url, display_id)
  316. return self._extract_from_legacy_playlist(playlist, display_id)
  317. def _process_legacy_playlist(self, playlist_id):
  318. return self._process_legacy_playlist_url(
  319. 'http://www.bbc.co.uk/iplayer/playlist/%s' % playlist_id, playlist_id)
  320. def _download_legacy_playlist_url(self, url, playlist_id=None):
  321. return self._download_xml(
  322. url, playlist_id, 'Downloading legacy playlist XML')
  323. def _extract_from_legacy_playlist(self, playlist, playlist_id):
  324. no_items = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}noItems')
  325. if no_items is not None:
  326. reason = no_items.get('reason')
  327. if reason == 'preAvailability':
  328. msg = 'Episode %s is not yet available' % playlist_id
  329. elif reason == 'postAvailability':
  330. msg = 'Episode %s is no longer available' % playlist_id
  331. elif reason == 'noMedia':
  332. msg = 'Episode %s is not currently available' % playlist_id
  333. else:
  334. msg = 'Episode %s is not available: %s' % (playlist_id, reason)
  335. raise ExtractorError(msg, expected=True)
  336. for item in self._extract_items(playlist):
  337. kind = item.get('kind')
  338. if kind != 'programme' and kind != 'radioProgramme':
  339. continue
  340. title = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}title').text
  341. description = playlist.find('./{http://bbc.co.uk/2008/emp/playlist}summary').text
  342. def get_programme_id(item):
  343. def get_from_attributes(item):
  344. for p in('identifier', 'group'):
  345. value = item.get(p)
  346. if value and re.match(r'^[pb][\da-z]{7}$', value):
  347. return value
  348. get_from_attributes(item)
  349. mediator = item.find('./{http://bbc.co.uk/2008/emp/playlist}mediator')
  350. if mediator is not None:
  351. return get_from_attributes(mediator)
  352. programme_id = get_programme_id(item)
  353. duration = int_or_none(item.get('duration'))
  354. # TODO: programme_id can be None and media items can be incorporated right inside
  355. # playlist's item (e.g. http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
  356. # as f4m and m3u8
  357. formats, subtitles = self._download_media_selector(programme_id)
  358. return programme_id, title, description, duration, formats, subtitles
  359. def _real_extract(self, url):
  360. group_id = self._match_id(url)
  361. webpage = self._download_webpage(url, group_id, 'Downloading video page')
  362. programme_id = None
  363. tviplayer = self._search_regex(
  364. r'mediator\.bind\(({.+?})\s*,\s*document\.getElementById',
  365. webpage, 'player', default=None)
  366. if tviplayer:
  367. player = self._parse_json(tviplayer, group_id).get('player', {})
  368. duration = int_or_none(player.get('duration'))
  369. programme_id = player.get('vpid')
  370. if not programme_id:
  371. programme_id = self._search_regex(
  372. r'"vpid"\s*:\s*"([\da-z]{8})"', webpage, 'vpid', fatal=False, default=None)
  373. if programme_id:
  374. formats, subtitles = self._download_media_selector(programme_id)
  375. title = self._og_search_title(webpage)
  376. description = self._search_regex(
  377. r'<p class="[^"]*medium-description[^"]*">([^<]+)</p>',
  378. webpage, 'description', fatal=False)
  379. else:
  380. programme_id, title, description, duration, formats, subtitles = self._download_playlist(group_id)
  381. self._sort_formats(formats)
  382. return {
  383. 'id': programme_id,
  384. 'title': title,
  385. 'description': description,
  386. 'thumbnail': self._og_search_thumbnail(webpage, default=None),
  387. 'duration': duration,
  388. 'formats': formats,
  389. 'subtitles': subtitles,
  390. }
  391. class BBCIE(BBCCoUkIE):
  392. IE_NAME = 'bbc'
  393. IE_DESC = 'BBC'
  394. _VALID_URL = r'https?://(?:www\.)?bbc\.(?:com|co\.uk)/(?:[^/]+/)+(?P<id>[^/#?]+)'
  395. # fails with notukerror for some videos ( non news sites such as bbc.com/travel )
  396. _MEDIASELECTOR_URL = 'http://open.live.bbc.co.uk/mediaselector/4/mtis/stream/%s'
  397. # limited selection of formats but may work where the above does not
  398. _MEDIASELECTOR_ALT_URL = 'http://open.live.bbc.co.uk/mediaselector/5/select/version/2.0/mediaset/journalism-pc/vpid/%s'
  399. _TESTS = [{
  400. # article with multiple videos embedded with data-media-meta containing
  401. # playlist.sxml, externalId and no direct video links
  402. 'url': 'http://www.bbc.com/news/world-europe-32668511',
  403. 'info_dict': {
  404. 'id': 'world-europe-32668511',
  405. 'title': 'Russia stages massive WW2 parade despite Western boycott',
  406. 'description': 'md5:00ff61976f6081841f759a08bf78cc9c',
  407. },
  408. 'playlist_count': 2,
  409. }, {
  410. # article with multiple videos embedded with data-media-meta (more videos)
  411. 'url': 'http://www.bbc.com/news/business-28299555',
  412. 'info_dict': {
  413. 'id': 'business-28299555',
  414. 'title': 'Farnborough Airshow: Video highlights',
  415. 'description': 'BBC reports and video highlights at the Farnborough Airshow.',
  416. },
  417. 'playlist_count': 9,
  418. 'skip': 'Save time',
  419. }, {
  420. # article with multiple videos embedded with `new SMP()`
  421. 'url': 'http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460',
  422. 'info_dict': {
  423. 'id': '3662a707-0af9-3149-963f-47bea720b460',
  424. 'title': 'BBC Blogs - Adam Curtis - BUGGER',
  425. },
  426. 'playlist_count': 18,
  427. }, {
  428. # single video embedded with mediaAssetPage.init()
  429. 'url': 'http://www.bbc.com/news/world-europe-32041533',
  430. 'info_dict': {
  431. 'id': 'p02mprgb',
  432. 'ext': 'mp4',
  433. 'title': 'Aerial footage showed the site of the crash in the Alps - courtesy BFM TV',
  434. 'duration': 47,
  435. 'timestamp': 1427219242,
  436. 'upload_date': '20150324',
  437. },
  438. 'params': {
  439. # rtmp download
  440. 'skip_download': True,
  441. }
  442. }, {
  443. # article with single video embedded with data-media-meta containing
  444. # direct video links (for now these are extracted) and playlist.xml (with
  445. # media items as f4m and m3u8 - currently unsupported)
  446. 'url': 'http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu',
  447. 'info_dict': {
  448. 'id': '150615_telabyad_kentin_cogu',
  449. 'ext': 'mp4',
  450. 'title': "YPG: Tel Abyad'ın tamamı kontrolümüzde",
  451. 'duration': 47,
  452. 'timestamp': 1434397334,
  453. 'upload_date': '20150615',
  454. },
  455. 'params': {
  456. 'skip_download': True,
  457. }
  458. }, {
  459. # single video embedded with mediaAssetPage.init() (regional section)
  460. 'url': 'http://www.bbc.com/mundo/video_fotos/2015/06/150619_video_honduras_militares_hospitales_corrupcion_aw',
  461. 'info_dict': {
  462. 'id': '150619_video_honduras_militares_hospitales_corrupcion_aw',
  463. 'ext': 'mp4',
  464. 'title': 'Honduras militariza sus hospitales por nuevo escándalo de corrupción',
  465. 'duration': 87,
  466. 'timestamp': 1434713142,
  467. 'upload_date': '20150619',
  468. },
  469. 'params': {
  470. 'skip_download': True,
  471. }
  472. }, {
  473. # single video story with digitalData
  474. 'url': 'http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret',
  475. 'info_dict': {
  476. 'id': 'p02q6gc4',
  477. 'ext': 'flv',
  478. 'title': 'Sri Lanka’s spicy secret',
  479. 'description': 'As a new train line to Jaffna opens up the country’s north, travellers can experience a truly distinct slice of Tamil culture.',
  480. 'timestamp': 1437674293,
  481. 'upload_date': '20150723',
  482. },
  483. 'params': {
  484. # rtmp download
  485. 'skip_download': True,
  486. }
  487. }, {
  488. # single video story without digitalData
  489. 'url': 'http://www.bbc.com/autos/story/20130513-hyundais-rock-star',
  490. 'info_dict': {
  491. 'id': 'p018zqqg',
  492. 'ext': 'mp4',
  493. 'title': 'Hyundai Santa Fe Sport: Rock star',
  494. 'description': 'md5:b042a26142c4154a6e472933cf20793d',
  495. 'timestamp': 1368473503,
  496. 'upload_date': '20130513',
  497. },
  498. 'params': {
  499. # rtmp download
  500. 'skip_download': True,
  501. }
  502. }, {
  503. # single video with playlist.sxml URL
  504. 'url': 'http://www.bbc.com/sport/0/football/33653409',
  505. 'info_dict': {
  506. 'id': 'p02xycnp',
  507. 'ext': 'mp4',
  508. 'title': 'Transfers: Cristiano Ronaldo to Man Utd, Arsenal to spend?',
  509. 'description': 'md5:398fca0e2e701c609d726e034fa1fc89',
  510. 'duration': 140,
  511. },
  512. 'params': {
  513. # rtmp download
  514. 'skip_download': True,
  515. }
  516. }, {
  517. # single video with playlist URL from weather section
  518. 'url': 'http://www.bbc.com/weather/features/33601775',
  519. 'only_matching': True,
  520. }, {
  521. # custom redirection to www.bbc.com
  522. 'url': 'http://www.bbc.co.uk/news/science-environment-33661876',
  523. 'only_matching': True,
  524. }]
  525. @classmethod
  526. def suitable(cls, url):
  527. return False if BBCCoUkIE.suitable(url) else super(BBCIE, cls).suitable(url)
  528. def _extract_from_media_meta(self, media_meta, video_id):
  529. # Direct links to media in media metadata (e.g.
  530. # http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
  531. # TODO: there are also f4m and m3u8 streams incorporated in playlist.sxml
  532. source_files = media_meta.get('sourceFiles')
  533. if source_files:
  534. return [{
  535. 'url': f['url'],
  536. 'format_id': format_id,
  537. 'ext': f.get('encoding'),
  538. 'tbr': float_or_none(f.get('bitrate'), 1000),
  539. 'filesize': int_or_none(f.get('filesize')),
  540. } for format_id, f in source_files.items() if f.get('url')], []
  541. programme_id = media_meta.get('externalId')
  542. if programme_id:
  543. return self._download_media_selector(programme_id)
  544. # Process playlist.sxml as legacy playlist
  545. href = media_meta.get('href')
  546. if href:
  547. playlist = self._download_legacy_playlist_url(href)
  548. _, _, _, _, formats, subtitles = self._extract_from_legacy_playlist(playlist, video_id)
  549. return formats, subtitles
  550. return [], []
  551. def _real_extract(self, url):
  552. playlist_id = self._match_id(url)
  553. webpage = self._download_webpage(url, playlist_id)
  554. timestamp = parse_iso8601(self._search_regex(
  555. [r'"datePublished":\s*"([^"]+)',
  556. r'<meta[^>]+property="article:published_time"[^>]+content="([^"]+)"',
  557. r'itemprop="datePublished"[^>]+datetime="([^"]+)"'],
  558. webpage, 'date', default=None))
  559. # single video with playlist.sxml URL (e.g. http://www.bbc.com/sport/0/football/3365340ng)
  560. playlist = self._search_regex(
  561. r'<param[^>]+name="playlist"[^>]+value="([^"]+)"',
  562. webpage, 'playlist', default=None)
  563. if playlist:
  564. programme_id, title, description, duration, formats, subtitles = \
  565. self._process_legacy_playlist_url(playlist, playlist_id)
  566. self._sort_formats(formats)
  567. return {
  568. 'id': programme_id,
  569. 'title': title,
  570. 'description': description,
  571. 'duration': duration,
  572. 'timestamp': timestamp,
  573. 'formats': formats,
  574. 'subtitles': subtitles,
  575. }
  576. # single video story (e.g. http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret)
  577. programme_id = self._search_regex(
  578. [r'data-video-player-vpid="([\da-z]{8})"',
  579. r'<param[^>]+name="externalIdentifier"[^>]+value="([\da-z]{8})"'],
  580. webpage, 'vpid', default=None)
  581. if programme_id:
  582. formats, subtitles = self._download_media_selector(programme_id)
  583. self._sort_formats(formats)
  584. # digitalData may be missing (e.g. http://www.bbc.com/autos/story/20130513-hyundais-rock-star)
  585. digital_data = self._parse_json(
  586. self._search_regex(
  587. r'var\s+digitalData\s*=\s*({.+?});?\n', webpage, 'digital data', default='{}'),
  588. programme_id, fatal=False)
  589. page_info = digital_data.get('page', {}).get('pageInfo', {})
  590. title = page_info.get('pageName') or self._og_search_title(webpage)
  591. description = page_info.get('description') or self._og_search_description(webpage)
  592. timestamp = parse_iso8601(page_info.get('publicationDate')) or timestamp
  593. return {
  594. 'id': programme_id,
  595. 'title': title,
  596. 'description': description,
  597. 'timestamp': timestamp,
  598. 'formats': formats,
  599. 'subtitles': subtitles,
  600. }
  601. playlist_title = self._html_search_regex(
  602. r'<title>(.*?)(?:\s*-\s*BBC [^ ]+)?</title>', webpage, 'playlist title')
  603. playlist_description = self._og_search_description(webpage, default=None)
  604. def extract_all(pattern):
  605. return list(filter(None, map(
  606. lambda s: self._parse_json(s, playlist_id, fatal=False),
  607. re.findall(pattern, webpage))))
  608. # Multiple video article (e.g.
  609. # http://www.bbc.co.uk/blogs/adamcurtis/entries/3662a707-0af9-3149-963f-47bea720b460)
  610. EMBED_URL = r'https?://(?:www\.)?bbc\.co\.uk/(?:[^/]+/)+[\da-z]{8}(?:\b[^"]+)?'
  611. entries = []
  612. for match in extract_all(r'new\s+SMP\(({.+?})\)'):
  613. embed_url = match.get('playerSettings', {}).get('externalEmbedUrl')
  614. if embed_url and re.match(EMBED_URL, embed_url):
  615. entries.append(embed_url)
  616. entries.extend(re.findall(
  617. r'setPlaylist\("(%s)"\)' % EMBED_URL, webpage))
  618. if entries:
  619. return self.playlist_result(
  620. [self.url_result(entry, 'BBCCoUk') for entry in entries],
  621. playlist_id, playlist_title, playlist_description)
  622. # Multiple video article (e.g. http://www.bbc.com/news/world-europe-32668511)
  623. medias = extract_all(r"data-media-meta='({[^']+})'")
  624. if not medias:
  625. # Single video article (e.g. http://www.bbc.com/news/video_and_audio/international)
  626. media_asset_page = self._parse_json(
  627. self._search_regex(
  628. r'mediaAssetPage\.init\(\s*({.+?}), "/', webpage, 'media asset'),
  629. playlist_id)
  630. medias = []
  631. for video in media_asset_page.get('videos', {}).values():
  632. medias.extend(video.values())
  633. entries = []
  634. for num, media_meta in enumerate(medias, start=1):
  635. formats, subtitles = self._extract_from_media_meta(media_meta, playlist_id)
  636. if not formats:
  637. continue
  638. self._sort_formats(formats)
  639. video_id = media_meta.get('externalId')
  640. if not video_id:
  641. video_id = playlist_id if len(medias) == 1 else '%s-%s' % (playlist_id, num)
  642. title = media_meta.get('caption')
  643. if not title:
  644. title = playlist_title if len(medias) == 1 else '%s - Video %s' % (playlist_title, num)
  645. duration = int_or_none(media_meta.get('durationInSeconds')) or parse_duration(media_meta.get('duration'))
  646. images = []
  647. for image in media_meta.get('images', {}).values():
  648. images.extend(image.values())
  649. if 'image' in media_meta:
  650. images.append(media_meta['image'])
  651. thumbnails = [{
  652. 'url': image.get('href'),
  653. 'width': int_or_none(image.get('width')),
  654. 'height': int_or_none(image.get('height')),
  655. } for image in images]
  656. entries.append({
  657. 'id': video_id,
  658. 'title': title,
  659. 'thumbnails': thumbnails,
  660. 'duration': duration,
  661. 'timestamp': timestamp,
  662. 'formats': formats,
  663. 'subtitles': subtitles,
  664. })
  665. return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)