bbc.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. # single video embedded with mediaAssetPage.init()
  421. 'url': 'http://www.bbc.com/news/world-europe-32041533',
  422. 'info_dict': {
  423. 'id': 'p02mprgb',
  424. 'ext': 'mp4',
  425. 'title': 'Aerial footage showed the site of the crash in the Alps - courtesy BFM TV',
  426. 'duration': 47,
  427. 'timestamp': 1427219242,
  428. 'upload_date': '20150324',
  429. },
  430. 'params': {
  431. # rtmp download
  432. 'skip_download': True,
  433. }
  434. }, {
  435. # article with single video embedded with data-media-meta containing
  436. # direct video links (for now these are extracted) and playlist.xml (with
  437. # media items as f4m and m3u8 - currently unsupported)
  438. 'url': 'http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu',
  439. 'info_dict': {
  440. 'id': '150615_telabyad_kentin_cogu',
  441. 'ext': 'mp4',
  442. 'title': "YPG: Tel Abyad'ın tamamı kontrolümüzde",
  443. 'duration': 47,
  444. 'timestamp': 1434397334,
  445. 'upload_date': '20150615',
  446. },
  447. 'params': {
  448. 'skip_download': True,
  449. }
  450. }, {
  451. # single video embedded with mediaAssetPage.init() (regional section)
  452. 'url': 'http://www.bbc.com/mundo/video_fotos/2015/06/150619_video_honduras_militares_hospitales_corrupcion_aw',
  453. 'info_dict': {
  454. 'id': '150619_video_honduras_militares_hospitales_corrupcion_aw',
  455. 'ext': 'mp4',
  456. 'title': 'Honduras militariza sus hospitales por nuevo escándalo de corrupción',
  457. 'duration': 87,
  458. 'timestamp': 1434713142,
  459. 'upload_date': '20150619',
  460. },
  461. 'params': {
  462. 'skip_download': True,
  463. }
  464. }, {
  465. # single video story with digitalData
  466. 'url': 'http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret',
  467. 'info_dict': {
  468. 'id': 'p02q6gc4',
  469. 'ext': 'flv',
  470. 'title': 'Sri Lanka’s spicy secret',
  471. 'description': 'As a new train line to Jaffna opens up the country’s north, travellers can experience a truly distinct slice of Tamil culture.',
  472. 'timestamp': 1437674293,
  473. 'upload_date': '20150723',
  474. },
  475. 'params': {
  476. # rtmp download
  477. 'skip_download': True,
  478. }
  479. }, {
  480. # single video story without digitalData
  481. 'url': 'http://www.bbc.com/autos/story/20130513-hyundais-rock-star',
  482. 'info_dict': {
  483. 'id': 'p018zqqg',
  484. 'ext': 'mp4',
  485. 'title': 'Hyundai Santa Fe Sport: Rock star',
  486. 'description': 'md5:b042a26142c4154a6e472933cf20793d',
  487. 'timestamp': 1368473503,
  488. 'upload_date': '20130513',
  489. },
  490. 'params': {
  491. # rtmp download
  492. 'skip_download': True,
  493. }
  494. }, {
  495. # single video with playlist.sxml URL
  496. 'url': 'http://www.bbc.com/sport/0/football/33653409',
  497. 'info_dict': {
  498. 'id': 'p02xycnp',
  499. 'ext': 'mp4',
  500. 'title': 'Transfers: Cristiano Ronaldo to Man Utd, Arsenal to spend?',
  501. 'description': 'md5:398fca0e2e701c609d726e034fa1fc89',
  502. 'duration': 140,
  503. },
  504. 'params': {
  505. # rtmp download
  506. 'skip_download': True,
  507. }
  508. }, {
  509. # single video with playlist URL from weather section
  510. 'url': 'http://www.bbc.com/weather/features/33601775',
  511. 'only_matching': True,
  512. }, {
  513. # custom redirection to www.bbc.com
  514. 'url': 'http://www.bbc.co.uk/news/science-environment-33661876',
  515. 'only_matching': True,
  516. }]
  517. @classmethod
  518. def suitable(cls, url):
  519. return False if BBCCoUkIE.suitable(url) else super(BBCIE, cls).suitable(url)
  520. def _extract_from_media_meta(self, media_meta, video_id):
  521. # Direct links to media in media metadata (e.g.
  522. # http://www.bbc.com/turkce/haberler/2015/06/150615_telabyad_kentin_cogu)
  523. # TODO: there are also f4m and m3u8 streams incorporated in playlist.sxml
  524. source_files = media_meta.get('sourceFiles')
  525. if source_files:
  526. return [{
  527. 'url': f['url'],
  528. 'format_id': format_id,
  529. 'ext': f.get('encoding'),
  530. 'tbr': float_or_none(f.get('bitrate'), 1000),
  531. 'filesize': int_or_none(f.get('filesize')),
  532. } for format_id, f in source_files.items() if f.get('url')], []
  533. programme_id = media_meta.get('externalId')
  534. if programme_id:
  535. return self._download_media_selector(programme_id)
  536. # Process playlist.sxml as legacy playlist
  537. href = media_meta.get('href')
  538. if href:
  539. playlist = self._download_legacy_playlist_url(href)
  540. _, _, _, _, formats, subtitles = self._extract_from_legacy_playlist(playlist, video_id)
  541. return formats, subtitles
  542. return [], []
  543. def _real_extract(self, url):
  544. playlist_id = self._match_id(url)
  545. webpage = self._download_webpage(url, playlist_id)
  546. timestamp = parse_iso8601(self._search_regex(
  547. [r'"datePublished":\s*"([^"]+)',
  548. r'<meta[^>]+property="article:published_time"[^>]+content="([^"]+)"',
  549. r'itemprop="datePublished"[^>]+datetime="([^"]+)"'],
  550. webpage, 'date', default=None))
  551. # single video with playlist.sxml URL (e.g. http://www.bbc.com/sport/0/football/3365340ng)
  552. playlist = self._search_regex(
  553. r'<param[^>]+name="playlist"[^>]+value="([^"]+)"',
  554. webpage, 'playlist', default=None)
  555. if playlist:
  556. programme_id, title, description, duration, formats, subtitles = \
  557. self._process_legacy_playlist_url(playlist, playlist_id)
  558. self._sort_formats(formats)
  559. return {
  560. 'id': programme_id,
  561. 'title': title,
  562. 'description': description,
  563. 'duration': duration,
  564. 'timestamp': timestamp,
  565. 'formats': formats,
  566. 'subtitles': subtitles,
  567. }
  568. # single video story (e.g. http://www.bbc.com/travel/story/20150625-sri-lankas-spicy-secret)
  569. programme_id = self._search_regex(
  570. [r'data-video-player-vpid="([\da-z]{8})"',
  571. r'<param[^>]+name="externalIdentifier"[^>]+value="([\da-z]{8})"'],
  572. webpage, 'vpid', default=None)
  573. if programme_id:
  574. formats, subtitles = self._download_media_selector(programme_id)
  575. self._sort_formats(formats)
  576. # digitalData may be missing (e.g. http://www.bbc.com/autos/story/20130513-hyundais-rock-star)
  577. digital_data = self._parse_json(
  578. self._search_regex(
  579. r'var\s+digitalData\s*=\s*({.+?});?\n', webpage, 'digital data', default='{}'),
  580. programme_id, fatal=False)
  581. page_info = digital_data.get('page', {}).get('pageInfo', {})
  582. title = page_info.get('pageName') or self._og_search_title(webpage)
  583. description = page_info.get('description') or self._og_search_description(webpage)
  584. timestamp = parse_iso8601(page_info.get('publicationDate')) or timestamp
  585. return {
  586. 'id': programme_id,
  587. 'title': title,
  588. 'description': description,
  589. 'timestamp': timestamp,
  590. 'formats': formats,
  591. 'subtitles': subtitles,
  592. }
  593. playlist_title = self._html_search_regex(
  594. r'<title>(.*?)(?:\s*-\s*BBC [^ ]+)?</title>', webpage, 'playlist title')
  595. playlist_description = self._og_search_description(webpage)
  596. # Multiple video article (e.g. http://www.bbc.com/news/world-europe-32668511)
  597. medias = list(filter(None, map(
  598. lambda s: self._parse_json(s, playlist_id, fatal=False),
  599. re.findall(r"data-media-meta='({[^']+})'", webpage))))
  600. if not medias:
  601. # Single video article (e.g. http://www.bbc.com/news/video_and_audio/international)
  602. media_asset_page = self._parse_json(
  603. self._search_regex(
  604. r'mediaAssetPage\.init\(\s*({.+?}), "/', webpage, 'media asset'),
  605. playlist_id)
  606. medias = []
  607. for video in media_asset_page.get('videos', {}).values():
  608. medias.extend(video.values())
  609. entries = []
  610. for num, media_meta in enumerate(medias, start=1):
  611. formats, subtitles = self._extract_from_media_meta(media_meta, playlist_id)
  612. if not formats:
  613. continue
  614. self._sort_formats(formats)
  615. video_id = media_meta.get('externalId')
  616. if not video_id:
  617. video_id = playlist_id if len(medias) == 1 else '%s-%s' % (playlist_id, num)
  618. title = media_meta.get('caption')
  619. if not title:
  620. title = playlist_title if len(medias) == 1 else '%s - Video %s' % (playlist_title, num)
  621. duration = int_or_none(media_meta.get('durationInSeconds')) or parse_duration(media_meta.get('duration'))
  622. images = []
  623. for image in media_meta.get('images', {}).values():
  624. images.extend(image.values())
  625. if 'image' in media_meta:
  626. images.append(media_meta['image'])
  627. thumbnails = [{
  628. 'url': image.get('href'),
  629. 'width': int_or_none(image.get('width')),
  630. 'height': int_or_none(image.get('height')),
  631. } for image in images]
  632. entries.append({
  633. 'id': video_id,
  634. 'title': title,
  635. 'thumbnails': thumbnails,
  636. 'duration': duration,
  637. 'timestamp': timestamp,
  638. 'formats': formats,
  639. 'subtitles': subtitles,
  640. })
  641. return self.playlist_result(entries, playlist_id, playlist_title, playlist_description)