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.

423 lines
17 KiB

  1. # coding: utf-8
  2. from __future__ import unicode_literals
  3. import json
  4. import re
  5. from .common import InfoExtractor
  6. from ..compat import compat_str
  7. from ..utils import (
  8. js_to_json,
  9. smuggle_url,
  10. try_get,
  11. xpath_text,
  12. xpath_element,
  13. xpath_with_ns,
  14. find_xpath_attr,
  15. parse_duration,
  16. parse_iso8601,
  17. parse_age_limit,
  18. int_or_none,
  19. ExtractorError,
  20. )
  21. class CBCIE(InfoExtractor):
  22. IE_NAME = 'cbc.ca'
  23. _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?!player/)(?:[^/]+/)+(?P<id>[^/?#]+)'
  24. _TESTS = [{
  25. # with mediaId
  26. 'url': 'http://www.cbc.ca/22minutes/videos/clips-season-23/don-cherry-play-offs',
  27. 'md5': '97e24d09672fc4cf56256d6faa6c25bc',
  28. 'info_dict': {
  29. 'id': '2682904050',
  30. 'ext': 'mp4',
  31. 'title': 'Don Cherry – All-Stars',
  32. 'description': 'Don Cherry has a bee in his bonnet about AHL player John Scott because that guy’s got heart.',
  33. 'timestamp': 1454463000,
  34. 'upload_date': '20160203',
  35. 'uploader': 'CBCC-NEW',
  36. },
  37. 'skip': 'Geo-restricted to Canada',
  38. }, {
  39. # with clipId, feed available via tpfeed.cbc.ca and feed.theplatform.com
  40. 'url': 'http://www.cbc.ca/22minutes/videos/22-minutes-update/22-minutes-update-episode-4',
  41. 'md5': '162adfa070274b144f4fdc3c3b8207db',
  42. 'info_dict': {
  43. 'id': '2414435309',
  44. 'ext': 'mp4',
  45. 'title': '22 Minutes Update: What Not To Wear Quebec',
  46. 'description': "This week's latest Canadian top political story is What Not To Wear Quebec.",
  47. 'upload_date': '20131025',
  48. 'uploader': 'CBCC-NEW',
  49. 'timestamp': 1382717907,
  50. },
  51. }, {
  52. # with clipId, feed only available via tpfeed.cbc.ca
  53. 'url': 'http://www.cbc.ca/archives/entry/1978-robin-williams-freestyles-on-90-minutes-live',
  54. 'md5': '0274a90b51a9b4971fe005c63f592f12',
  55. 'info_dict': {
  56. 'id': '2487345465',
  57. 'ext': 'mp4',
  58. 'title': 'Robin Williams freestyles on 90 Minutes Live',
  59. 'description': 'Wacky American comedian Robin Williams shows off his infamous "freestyle" comedic talents while being interviewed on CBC\'s 90 Minutes Live.',
  60. 'upload_date': '19780210',
  61. 'uploader': 'CBCC-NEW',
  62. 'timestamp': 255977160,
  63. },
  64. }, {
  65. # multiple iframes
  66. 'url': 'http://www.cbc.ca/natureofthings/blog/birds-eye-view-from-vancouvers-burrard-street-bridge-how-we-got-the-shot',
  67. 'playlist': [{
  68. 'md5': '377572d0b49c4ce0c9ad77470e0b96b4',
  69. 'info_dict': {
  70. 'id': '2680832926',
  71. 'ext': 'mp4',
  72. 'title': 'An Eagle\'s-Eye View Off Burrard Bridge',
  73. 'description': 'Hercules the eagle flies from Vancouver\'s Burrard Bridge down to a nearby park with a mini-camera strapped to his back.',
  74. 'upload_date': '20160201',
  75. 'timestamp': 1454342820,
  76. 'uploader': 'CBCC-NEW',
  77. },
  78. }, {
  79. 'md5': '415a0e3f586113894174dfb31aa5bb1a',
  80. 'info_dict': {
  81. 'id': '2658915080',
  82. 'ext': 'mp4',
  83. 'title': 'Fly like an eagle!',
  84. 'description': 'Eagle equipped with a mini camera flies from the world\'s tallest tower',
  85. 'upload_date': '20150315',
  86. 'timestamp': 1426443984,
  87. 'uploader': 'CBCC-NEW',
  88. },
  89. }],
  90. 'skip': 'Geo-restricted to Canada',
  91. }, {
  92. # multiple CBC.APP.Caffeine.initInstance(...)
  93. 'url': 'http://www.cbc.ca/news/canada/calgary/dog-indoor-exercise-winter-1.3928238',
  94. 'info_dict': {
  95. 'title': 'Keep Rover active during the deep freeze with doggie pushups and other fun indoor tasks',
  96. 'id': 'dog-indoor-exercise-winter-1.3928238',
  97. 'description': 'md5:c18552e41726ee95bd75210d1ca9194c',
  98. },
  99. 'playlist_mincount': 6,
  100. }]
  101. @classmethod
  102. def suitable(cls, url):
  103. return False if CBCPlayerIE.suitable(url) else super(CBCIE, cls).suitable(url)
  104. def _extract_player_init(self, player_init, display_id):
  105. player_info = self._parse_json(player_init, display_id, js_to_json)
  106. media_id = player_info.get('mediaId')
  107. if not media_id:
  108. clip_id = player_info['clipId']
  109. feed = self._download_json(
  110. 'http://tpfeed.cbc.ca/f/ExhSPC/vms_5akSXx4Ng_Zn?byCustomValue={:mpsReleases}{%s}' % clip_id,
  111. clip_id, fatal=False)
  112. if feed:
  113. media_id = try_get(feed, lambda x: x['entries'][0]['guid'], compat_str)
  114. if not media_id:
  115. media_id = self._download_json(
  116. 'http://feed.theplatform.com/f/h9dtGB/punlNGjMlc1F?fields=id&byContent=byReleases%3DbyId%253D' + clip_id,
  117. clip_id)['entries'][0]['id'].split('/')[-1]
  118. return self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
  119. def _real_extract(self, url):
  120. display_id = self._match_id(url)
  121. webpage = self._download_webpage(url, display_id)
  122. entries = [
  123. self._extract_player_init(player_init, display_id)
  124. for player_init in re.findall(r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage)]
  125. entries.extend([
  126. self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
  127. for media_id in re.findall(r'<iframe[^>]+src="[^"]+?mediaId=(\d+)"', webpage)])
  128. return self.playlist_result(
  129. entries, display_id,
  130. self._og_search_title(webpage, fatal=False),
  131. self._og_search_description(webpage))
  132. class CBCPlayerIE(InfoExtractor):
  133. IE_NAME = 'cbc.ca:player'
  134. _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/|i/caffeine/syndicate/\?mediaId=))(?P<id>\d+)'
  135. _TESTS = [{
  136. 'url': 'http://www.cbc.ca/player/play/2683190193',
  137. 'md5': '64d25f841ddf4ddb28a235338af32e2c',
  138. 'info_dict': {
  139. 'id': '2683190193',
  140. 'ext': 'mp4',
  141. 'title': 'Gerry Runs a Sweat Shop',
  142. 'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
  143. 'timestamp': 1455071400,
  144. 'upload_date': '20160210',
  145. 'uploader': 'CBCC-NEW',
  146. },
  147. 'skip': 'Geo-restricted to Canada',
  148. }, {
  149. # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/
  150. 'url': 'http://www.cbc.ca/player/play/2657631896',
  151. 'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
  152. 'info_dict': {
  153. 'id': '2657631896',
  154. 'ext': 'mp3',
  155. 'title': 'CBC Montreal is organizing its first ever community hackathon!',
  156. 'description': 'The modern technology we tend to depend on so heavily, is never without it\'s share of hiccups and headaches. Next weekend - CBC Montreal will be getting members of the public for its first Hackathon.',
  157. 'timestamp': 1425704400,
  158. 'upload_date': '20150307',
  159. 'uploader': 'CBCC-NEW',
  160. },
  161. }, {
  162. 'url': 'http://www.cbc.ca/player/play/2164402062',
  163. 'md5': '33fcd8f6719b9dd60a5e73adcb83b9f6',
  164. 'info_dict': {
  165. 'id': '2164402062',
  166. 'ext': 'mp4',
  167. 'title': 'Cancer survivor four times over',
  168. 'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.',
  169. 'timestamp': 1320410746,
  170. 'upload_date': '20111104',
  171. 'uploader': 'CBCC-NEW',
  172. },
  173. }]
  174. def _real_extract(self, url):
  175. video_id = self._match_id(url)
  176. return {
  177. '_type': 'url_transparent',
  178. 'ie_key': 'ThePlatform',
  179. 'url': smuggle_url(
  180. 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true&formats=MPEG4,FLV,MP3' % video_id, {
  181. 'force_smil_url': True
  182. }),
  183. 'id': video_id,
  184. }
  185. class CBCWatchBaseIE(InfoExtractor):
  186. _device_id = None
  187. _device_token = None
  188. _API_BASE_URL = 'https://api-cbc.cloud.clearleap.com/cloffice/client/'
  189. _NS_MAP = {
  190. 'media': 'http://search.yahoo.com/mrss/',
  191. 'clearleap': 'http://www.clearleap.com/namespace/clearleap/1.0/',
  192. }
  193. _GEO_COUNTRIES = ['CA']
  194. def _call_api(self, path, video_id):
  195. url = path if path.startswith('http') else self._API_BASE_URL + path
  196. result = self._download_xml(url, video_id, headers={
  197. 'X-Clearleap-DeviceId': self._device_id,
  198. 'X-Clearleap-DeviceToken': self._device_token,
  199. })
  200. error_message = xpath_text(result, 'userMessage') or xpath_text(result, 'systemMessage')
  201. if error_message:
  202. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message))
  203. return result
  204. def _real_initialize(self):
  205. if not self._device_id or not self._device_token:
  206. device = self._downloader.cache.load('cbcwatch', 'device') or {}
  207. self._device_id, self._device_token = device.get('id'), device.get('token')
  208. if not self._device_id or not self._device_token:
  209. result = self._download_xml(
  210. self._API_BASE_URL + 'device/register',
  211. None, data=b'<device><type>web</type></device>')
  212. self._device_id = xpath_text(result, 'deviceId', fatal=True)
  213. self._device_token = xpath_text(result, 'deviceToken', fatal=True)
  214. self._downloader.cache.store(
  215. 'cbcwatch', 'device', {
  216. 'id': self._device_id,
  217. 'token': self._device_token,
  218. })
  219. def _parse_rss_feed(self, rss):
  220. channel = xpath_element(rss, 'channel', fatal=True)
  221. def _add_ns(path):
  222. return xpath_with_ns(path, self._NS_MAP)
  223. entries = []
  224. for item in channel.findall('item'):
  225. guid = xpath_text(item, 'guid', fatal=True)
  226. title = xpath_text(item, 'title', fatal=True)
  227. media_group = xpath_element(item, _add_ns('media:group'), fatal=True)
  228. content = xpath_element(media_group, _add_ns('media:content'), fatal=True)
  229. content_url = content.attrib['url']
  230. thumbnails = []
  231. for thumbnail in media_group.findall(_add_ns('media:thumbnail')):
  232. thumbnail_url = thumbnail.get('url')
  233. if not thumbnail_url:
  234. continue
  235. thumbnails.append({
  236. 'id': thumbnail.get('profile'),
  237. 'url': thumbnail_url,
  238. 'width': int_or_none(thumbnail.get('width')),
  239. 'height': int_or_none(thumbnail.get('height')),
  240. })
  241. timestamp = None
  242. release_date = find_xpath_attr(
  243. item, _add_ns('media:credit'), 'role', 'releaseDate')
  244. if release_date is not None:
  245. timestamp = parse_iso8601(release_date.text)
  246. entries.append({
  247. '_type': 'url_transparent',
  248. 'url': content_url,
  249. 'id': guid,
  250. 'title': title,
  251. 'description': xpath_text(item, 'description'),
  252. 'timestamp': timestamp,
  253. 'duration': int_or_none(content.get('duration')),
  254. 'age_limit': parse_age_limit(xpath_text(item, _add_ns('media:rating'))),
  255. 'episode': xpath_text(item, _add_ns('clearleap:episode')),
  256. 'episode_number': int_or_none(xpath_text(item, _add_ns('clearleap:episodeInSeason'))),
  257. 'series': xpath_text(item, _add_ns('clearleap:series')),
  258. 'season_number': int_or_none(xpath_text(item, _add_ns('clearleap:season'))),
  259. 'thumbnails': thumbnails,
  260. 'ie_key': 'CBCWatchVideo',
  261. })
  262. return self.playlist_result(
  263. entries, xpath_text(channel, 'guid'),
  264. xpath_text(channel, 'title'),
  265. xpath_text(channel, 'description'))
  266. class CBCWatchVideoIE(CBCWatchBaseIE):
  267. IE_NAME = 'cbc.ca:watch:video'
  268. _VALID_URL = r'https?://api-cbc\.cloud\.clearleap\.com/cloffice/client/web/play/?\?.*?\bcontentId=(?P<id>[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12})'
  269. _TEST = {
  270. # geo-restricted to Canada, bypassable
  271. 'url': 'https://api-cbc.cloud.clearleap.com/cloffice/client/web/play/?contentId=3c84472a-1eea-4dee-9267-2655d5055dcf&categoryId=ebc258f5-ee40-4cca-b66b-ba6bd55b7235',
  272. 'only_matching': True,
  273. }
  274. def _real_extract(self, url):
  275. video_id = self._match_id(url)
  276. result = self._call_api(url, video_id)
  277. m3u8_url = xpath_text(result, 'url', fatal=True)
  278. formats = self._extract_m3u8_formats(re.sub(r'/([^/]+)/[^/?]+\.m3u8', r'/\1/\1.m3u8', m3u8_url), video_id, 'mp4', fatal=False)
  279. if len(formats) < 2:
  280. formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
  281. for f in formats:
  282. format_id = f.get('format_id')
  283. if format_id.startswith('AAC'):
  284. f['acodec'] = 'aac'
  285. elif format_id.startswith('AC3'):
  286. f['acodec'] = 'ac-3'
  287. self._sort_formats(formats)
  288. info = {
  289. 'id': video_id,
  290. 'title': video_id,
  291. 'formats': formats,
  292. }
  293. rss = xpath_element(result, 'rss')
  294. if rss:
  295. info.update(self._parse_rss_feed(rss)['entries'][0])
  296. del info['url']
  297. del info['_type']
  298. del info['ie_key']
  299. return info
  300. class CBCWatchIE(CBCWatchBaseIE):
  301. IE_NAME = 'cbc.ca:watch'
  302. _VALID_URL = r'https?://watch\.cbc\.ca/(?:[^/]+/)+(?P<id>[0-9a-f-]+)'
  303. _TESTS = [{
  304. # geo-restricted to Canada, bypassable
  305. 'url': 'http://watch.cbc.ca/doc-zone/season-6/customer-disservice/38e815a-009e3ab12e4',
  306. 'info_dict': {
  307. 'id': '9673749a-5e77-484c-8b62-a1092a6b5168',
  308. 'ext': 'mp4',
  309. 'title': 'Customer (Dis)Service',
  310. 'description': 'md5:8bdd6913a0fe03d4b2a17ebe169c7c87',
  311. 'upload_date': '20160219',
  312. 'timestamp': 1455840000,
  313. },
  314. 'params': {
  315. # m3u8 download
  316. 'skip_download': True,
  317. 'format': 'bestvideo',
  318. },
  319. }, {
  320. # geo-restricted to Canada, bypassable
  321. 'url': 'http://watch.cbc.ca/arthur/all/1ed4b385-cd84-49cf-95f0-80f004680057',
  322. 'info_dict': {
  323. 'id': '1ed4b385-cd84-49cf-95f0-80f004680057',
  324. 'title': 'Arthur',
  325. 'description': 'Arthur, the sweetest 8-year-old aardvark, and his pals solve all kinds of problems with humour, kindness and teamwork.',
  326. },
  327. 'playlist_mincount': 30,
  328. }]
  329. def _real_extract(self, url):
  330. video_id = self._match_id(url)
  331. rss = self._call_api('web/browse/' + video_id, video_id)
  332. return self._parse_rss_feed(rss)
  333. class CBCOlympicsIE(InfoExtractor):
  334. IE_NAME = 'cbc.ca:olympics'
  335. _VALID_URL = r'https?://olympics\.cbc\.ca/video/[^/]+/(?P<id>[^/?#]+)'
  336. _TESTS = [{
  337. 'url': 'https://olympics.cbc.ca/video/whats-on-tv/olympic-morning-featuring-the-opening-ceremony/',
  338. 'only_matching': True,
  339. }]
  340. def _real_extract(self, url):
  341. display_id = self._match_id(url)
  342. webpage = self._download_webpage(url, display_id)
  343. video_id = self._hidden_inputs(webpage)['videoId']
  344. video_doc = self._download_xml(
  345. 'https://olympics.cbc.ca/videodata/%s.xml' % video_id, video_id)
  346. title = xpath_text(video_doc, 'title', fatal=True)
  347. is_live = xpath_text(video_doc, 'kind') == 'Live'
  348. if is_live:
  349. title = self._live_title(title)
  350. formats = []
  351. for video_source in video_doc.findall('videoSources/videoSource'):
  352. uri = xpath_text(video_source, 'uri')
  353. if not uri:
  354. continue
  355. tokenize = self._download_json(
  356. 'https://olympics.cbc.ca/api/api-akamai/tokenize',
  357. video_id, data=json.dumps({
  358. 'VideoSource': uri,
  359. }).encode(), headers={
  360. 'Content-Type': 'application/json',
  361. 'Referer': url,
  362. # d3.VideoPlayer._init in https://olympics.cbc.ca/components/script/base.js
  363. 'Cookie': '_dvp=TK:C0ObxjerU', # AKAMAI CDN cookie
  364. }, fatal=False)
  365. if not tokenize:
  366. continue
  367. content_url = tokenize['ContentUrl']
  368. video_source_format = video_source.get('format')
  369. if video_source_format == 'IIS':
  370. formats.extend(self._extract_ism_formats(
  371. content_url, video_id, ism_id=video_source_format, fatal=False))
  372. else:
  373. formats.extend(self._extract_m3u8_formats(
  374. content_url, video_id, 'mp4',
  375. 'm3u8' if is_live else 'm3u8_native',
  376. m3u8_id=video_source_format, fatal=False))
  377. self._sort_formats(formats)
  378. return {
  379. 'id': video_id,
  380. 'display_id': display_id,
  381. 'title': title,
  382. 'description': xpath_text(video_doc, 'description'),
  383. 'thumbnail': xpath_text(video_doc, 'thumbnailUrl'),
  384. 'duration': parse_duration(xpath_text(video_doc, 'duration')),
  385. 'formats': formats,
  386. 'is_live': is_live,
  387. }