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.

349 lines
14 KiB

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