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.

341 lines
14 KiB

8 years ago
  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. @classmethod
  91. def suitable(cls, url):
  92. return False if CBCPlayerIE.suitable(url) else super(CBCIE, cls).suitable(url)
  93. def _real_extract(self, url):
  94. display_id = self._match_id(url)
  95. webpage = self._download_webpage(url, display_id)
  96. player_init = self._search_regex(
  97. r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage, 'player init',
  98. default=None)
  99. if player_init:
  100. player_info = self._parse_json(player_init, display_id, js_to_json)
  101. media_id = player_info.get('mediaId')
  102. if not media_id:
  103. clip_id = player_info['clipId']
  104. feed = self._download_json(
  105. 'http://tpfeed.cbc.ca/f/ExhSPC/vms_5akSXx4Ng_Zn?byCustomValue={:mpsReleases}{%s}' % clip_id,
  106. clip_id, fatal=False)
  107. if feed:
  108. media_id = try_get(feed, lambda x: x['entries'][0]['guid'], compat_str)
  109. if not media_id:
  110. media_id = self._download_json(
  111. 'http://feed.theplatform.com/f/h9dtGB/punlNGjMlc1F?fields=id&byContent=byReleases%3DbyId%253D' + clip_id,
  112. clip_id)['entries'][0]['id'].split('/')[-1]
  113. return self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
  114. else:
  115. entries = [self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id) for media_id in re.findall(r'<iframe[^>]+src="[^"]+?mediaId=(\d+)"', webpage)]
  116. return self.playlist_result(entries)
  117. class CBCPlayerIE(InfoExtractor):
  118. IE_NAME = 'cbc.ca:player'
  119. _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/|i/caffeine/syndicate/\?mediaId=))(?P<id>\d+)'
  120. _TESTS = [{
  121. 'url': 'http://www.cbc.ca/player/play/2683190193',
  122. 'md5': '64d25f841ddf4ddb28a235338af32e2c',
  123. 'info_dict': {
  124. 'id': '2683190193',
  125. 'ext': 'mp4',
  126. 'title': 'Gerry Runs a Sweat Shop',
  127. 'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
  128. 'timestamp': 1455071400,
  129. 'upload_date': '20160210',
  130. 'uploader': 'CBCC-NEW',
  131. },
  132. 'skip': 'Geo-restricted to Canada',
  133. }, {
  134. # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/
  135. 'url': 'http://www.cbc.ca/player/play/2657631896',
  136. 'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
  137. 'info_dict': {
  138. 'id': '2657631896',
  139. 'ext': 'mp3',
  140. 'title': 'CBC Montreal is organizing its first ever community hackathon!',
  141. '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.',
  142. 'timestamp': 1425704400,
  143. 'upload_date': '20150307',
  144. 'uploader': 'CBCC-NEW',
  145. },
  146. }, {
  147. # available only when we add `formats=MPEG4,FLV,MP3` to theplatform url
  148. 'url': 'http://www.cbc.ca/player/play/2164402062',
  149. 'md5': '17a61eb813539abea40618d6323a7f82',
  150. 'info_dict': {
  151. 'id': '2164402062',
  152. 'ext': 'flv',
  153. 'title': 'Cancer survivor four times over',
  154. 'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.',
  155. 'timestamp': 1320410746,
  156. 'upload_date': '20111104',
  157. 'uploader': 'CBCC-NEW',
  158. },
  159. }]
  160. def _real_extract(self, url):
  161. video_id = self._match_id(url)
  162. return {
  163. '_type': 'url_transparent',
  164. 'ie_key': 'ThePlatform',
  165. 'url': smuggle_url(
  166. 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true&formats=MPEG4,FLV,MP3' % video_id, {
  167. 'force_smil_url': True
  168. }),
  169. 'id': video_id,
  170. }
  171. class CBCWatchBaseIE(InfoExtractor):
  172. _device_id = None
  173. _device_token = None
  174. _API_BASE_URL = 'https://api-cbc.cloud.clearleap.com/cloffice/client/'
  175. _NS_MAP = {
  176. 'media': 'http://search.yahoo.com/mrss/',
  177. 'clearleap': 'http://www.clearleap.com/namespace/clearleap/1.0/',
  178. }
  179. def _call_api(self, path, video_id):
  180. url = path if path.startswith('http') else self._API_BASE_URL + path
  181. result = self._download_xml(url, video_id, headers={
  182. 'X-Clearleap-DeviceId': self._device_id,
  183. 'X-Clearleap-DeviceToken': self._device_token,
  184. })
  185. error_message = xpath_text(result, 'userMessage') or xpath_text(result, 'systemMessage')
  186. if error_message:
  187. raise ExtractorError('%s said: %s' % (self.IE_NAME, error_message))
  188. return result
  189. def _real_initialize(self):
  190. if not self._device_id or not self._device_token:
  191. device = self._downloader.cache.load('cbcwatch', 'device') or {}
  192. self._device_id, self._device_token = device.get('id'), device.get('token')
  193. if not self._device_id or not self._device_token:
  194. result = self._download_xml(
  195. self._API_BASE_URL + 'device/register',
  196. None, data=b'<device><type>web</type></device>')
  197. self._device_id = xpath_text(result, 'deviceId', fatal=True)
  198. self._device_token = xpath_text(result, 'deviceToken', fatal=True)
  199. self._downloader.cache.store(
  200. 'cbcwatch', 'device', {
  201. 'id': self._device_id,
  202. 'token': self._device_token,
  203. })
  204. def _parse_rss_feed(self, rss):
  205. channel = xpath_element(rss, 'channel', fatal=True)
  206. def _add_ns(path):
  207. return xpath_with_ns(path, self._NS_MAP)
  208. entries = []
  209. for item in channel.findall('item'):
  210. guid = xpath_text(item, 'guid', fatal=True)
  211. title = xpath_text(item, 'title', fatal=True)
  212. media_group = xpath_element(item, _add_ns('media:group'), fatal=True)
  213. content = xpath_element(media_group, _add_ns('media:content'), fatal=True)
  214. content_url = content.attrib['url']
  215. thumbnails = []
  216. for thumbnail in media_group.findall(_add_ns('media:thumbnail')):
  217. thumbnail_url = thumbnail.get('url')
  218. if not thumbnail_url:
  219. continue
  220. thumbnails.append({
  221. 'id': thumbnail.get('profile'),
  222. 'url': thumbnail_url,
  223. 'width': int_or_none(thumbnail.get('width')),
  224. 'height': int_or_none(thumbnail.get('height')),
  225. })
  226. timestamp = None
  227. release_date = find_xpath_attr(
  228. item, _add_ns('media:credit'), 'role', 'releaseDate')
  229. if release_date is not None:
  230. timestamp = parse_iso8601(release_date.text)
  231. entries.append({
  232. '_type': 'url_transparent',
  233. 'url': content_url,
  234. 'id': guid,
  235. 'title': title,
  236. 'description': xpath_text(item, 'description'),
  237. 'timestamp': timestamp,
  238. 'duration': int_or_none(content.get('duration')),
  239. 'age_limit': parse_age_limit(xpath_text(item, _add_ns('media:rating'))),
  240. 'episode': xpath_text(item, _add_ns('clearleap:episode')),
  241. 'episode_number': int_or_none(xpath_text(item, _add_ns('clearleap:episodeInSeason'))),
  242. 'series': xpath_text(item, _add_ns('clearleap:series')),
  243. 'season_number': int_or_none(xpath_text(item, _add_ns('clearleap:season'))),
  244. 'thumbnails': thumbnails,
  245. 'ie_key': 'CBCWatchVideo',
  246. })
  247. return self.playlist_result(
  248. entries, xpath_text(channel, 'guid'),
  249. xpath_text(channel, 'title'),
  250. xpath_text(channel, 'description'))
  251. class CBCWatchVideoIE(CBCWatchBaseIE):
  252. IE_NAME = 'cbc.ca:watch:video'
  253. _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})'
  254. def _real_extract(self, url):
  255. video_id = self._match_id(url)
  256. result = self._call_api(url, video_id)
  257. m3u8_url = xpath_text(result, 'url', fatal=True)
  258. formats = self._extract_m3u8_formats(re.sub(r'/([^/]+)/[^/?]+\.m3u8', r'/\1/\1.m3u8', m3u8_url), video_id, 'mp4', fatal=False)
  259. if len(formats) < 2:
  260. formats = self._extract_m3u8_formats(m3u8_url, video_id, 'mp4')
  261. # Despite metadata in m3u8 all video+audio formats are
  262. # actually video-only (no audio)
  263. for f in formats:
  264. if f.get('acodec') != 'none' and f.get('vcodec') != 'none':
  265. f['acodec'] = 'none'
  266. self._sort_formats(formats)
  267. info = {
  268. 'id': video_id,
  269. 'title': video_id,
  270. 'formats': formats,
  271. }
  272. rss = xpath_element(result, 'rss')
  273. if rss:
  274. info.update(self._parse_rss_feed(rss)['entries'][0])
  275. del info['url']
  276. del info['_type']
  277. del info['ie_key']
  278. return info
  279. class CBCWatchIE(CBCWatchBaseIE):
  280. IE_NAME = 'cbc.ca:watch'
  281. _VALID_URL = r'https?://watch\.cbc\.ca/(?:[^/]+/)+(?P<id>[0-9a-f-]+)'
  282. _TESTS = [{
  283. 'url': 'http://watch.cbc.ca/doc-zone/season-6/customer-disservice/38e815a-009e3ab12e4',
  284. 'info_dict': {
  285. 'id': '38e815a-009e3ab12e4',
  286. 'ext': 'mp4',
  287. 'title': 'Customer (Dis)Service',
  288. 'description': 'md5:8bdd6913a0fe03d4b2a17ebe169c7c87',
  289. 'upload_date': '20160219',
  290. 'timestamp': 1455840000,
  291. },
  292. 'params': {
  293. # m3u8 download
  294. 'skip_download': True,
  295. 'format': 'bestvideo',
  296. },
  297. 'skip': 'Geo-restricted to Canada',
  298. }, {
  299. 'url': 'http://watch.cbc.ca/arthur/all/1ed4b385-cd84-49cf-95f0-80f004680057',
  300. 'info_dict': {
  301. 'id': '1ed4b385-cd84-49cf-95f0-80f004680057',
  302. 'title': 'Arthur',
  303. 'description': 'Arthur, the sweetest 8-year-old aardvark, and his pals solve all kinds of problems with humour, kindness and teamwork.',
  304. },
  305. 'playlist_mincount': 30,
  306. 'skip': 'Geo-restricted to Canada',
  307. }]
  308. def _real_extract(self, url):
  309. video_id = self._match_id(url)
  310. rss = self._call_api('web/browse/' + video_id, video_id)
  311. return self._parse_rss_feed(rss)