Merge pull request #9288 from reyyed/issue#9063fix
[youtube-dl] / youtube_dl / extractor / cbc.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     js_to_json,
9     smuggle_url,
10 )
11
12
13 class CBCIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?!player/)(?:[^/]+/)+(?P<id>[^/?#]+)'
15     _TESTS = [{
16         # with mediaId
17         'url': 'http://www.cbc.ca/22minutes/videos/clips-season-23/don-cherry-play-offs',
18         'md5': '97e24d09672fc4cf56256d6faa6c25bc',
19         'info_dict': {
20             'id': '2682904050',
21             'ext': 'mp4',
22             'title': 'Don Cherry – All-Stars',
23             'description': 'Don Cherry has a bee in his bonnet about AHL player John Scott because that guy’s got heart.',
24             'timestamp': 1454463000,
25             'upload_date': '20160203',
26             'uploader': 'CBCC-NEW',
27         },
28     }, {
29         # with clipId
30         'url': 'http://www.cbc.ca/archives/entry/1978-robin-williams-freestyles-on-90-minutes-live',
31         'md5': '0274a90b51a9b4971fe005c63f592f12',
32         'info_dict': {
33             'id': '2487345465',
34             'ext': 'mp4',
35             'title': 'Robin Williams freestyles on 90 Minutes Live',
36             'description': 'Wacky American comedian Robin Williams shows off his infamous "freestyle" comedic talents while being interviewed on CBC\'s 90 Minutes Live.',
37             'upload_date': '19780210',
38             'uploader': 'CBCC-NEW',
39             'timestamp': 255977160,
40         },
41     }, {
42         # multiple iframes
43         'url': 'http://www.cbc.ca/natureofthings/blog/birds-eye-view-from-vancouvers-burrard-street-bridge-how-we-got-the-shot',
44         'playlist': [{
45             'md5': '377572d0b49c4ce0c9ad77470e0b96b4',
46             'info_dict': {
47                 'id': '2680832926',
48                 'ext': 'mp4',
49                 'title': 'An Eagle\'s-Eye View Off Burrard Bridge',
50                 'description': 'Hercules the eagle flies from Vancouver\'s Burrard Bridge down to a nearby park with a mini-camera strapped to his back.',
51                 'upload_date': '20160201',
52                 'timestamp': 1454342820,
53                 'uploader': 'CBCC-NEW',
54             },
55         }, {
56             'md5': '415a0e3f586113894174dfb31aa5bb1a',
57             'info_dict': {
58                 'id': '2658915080',
59                 'ext': 'mp4',
60                 'title': 'Fly like an eagle!',
61                 'description': 'Eagle equipped with a mini camera flies from the world\'s tallest tower',
62                 'upload_date': '20150315',
63                 'timestamp': 1426443984,
64                 'uploader': 'CBCC-NEW',
65             },
66         }],
67     }]
68
69     @classmethod
70     def suitable(cls, url):
71         return False if CBCPlayerIE.suitable(url) else super(CBCIE, cls).suitable(url)
72
73     def _real_extract(self, url):
74         display_id = self._match_id(url)
75         webpage = self._download_webpage(url, display_id)
76         player_init = self._search_regex(
77             r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage, 'player init',
78             default=None)
79         if player_init:
80             player_info = self._parse_json(player_init, display_id, js_to_json)
81             media_id = player_info.get('mediaId')
82             if not media_id:
83                 clip_id = player_info['clipId']
84                 media_id = self._download_json(
85                     'http://feed.theplatform.com/f/h9dtGB/punlNGjMlc1F?fields=id&byContent=byReleases%3DbyId%253D' + clip_id,
86                     clip_id)['entries'][0]['id'].split('/')[-1]
87             return self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
88         else:
89             entries = [self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id) for media_id in re.findall(r'<iframe[^>]+src="[^"]+?mediaId=(\d+)"', webpage)]
90             return self.playlist_result(entries)
91
92
93 class CBCPlayerIE(InfoExtractor):
94     _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/|i/caffeine/syndicate/\?mediaId=))(?P<id>\d+)'
95     _TESTS = [{
96         'url': 'http://www.cbc.ca/player/play/2683190193',
97         'md5': '64d25f841ddf4ddb28a235338af32e2c',
98         'info_dict': {
99             'id': '2683190193',
100             'ext': 'mp4',
101             'title': 'Gerry Runs a Sweat Shop',
102             'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
103             'timestamp': 1455071400,
104             'upload_date': '20160210',
105             'uploader': 'CBCC-NEW',
106         },
107     }, {
108         # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/
109         'url': 'http://www.cbc.ca/player/play/2657631896',
110         'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
111         'info_dict': {
112             'id': '2657631896',
113             'ext': 'mp3',
114             'title': 'CBC Montreal is organizing its first ever community hackathon!',
115             '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.',
116             'timestamp': 1425704400,
117             'upload_date': '20150307',
118             'uploader': 'CBCC-NEW',
119         },
120     }, {
121         # available only when we add `formats=MPEG4,FLV,MP3` to theplatform url
122         'url': 'http://www.cbc.ca/player/play/2164402062',
123         'md5': '17a61eb813539abea40618d6323a7f82',
124         'info_dict': {
125             'id': '2164402062',
126             'ext': 'flv',
127             'title': 'Cancer survivor four times over',
128             'description': 'Tim Mayer has beaten three different forms of cancer four times in five years.',
129             'timestamp': 1320410746,
130             'upload_date': '20111104',
131             'uploader': 'CBCC-NEW',
132         },
133     }]
134
135     def _real_extract(self, url):
136         video_id = self._match_id(url)
137         return {
138             '_type': 'url_transparent',
139             'ie_key': 'ThePlatform',
140             'url': smuggle_url(
141                 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true&formats=MPEG4,FLV,MP3' % video_id, {
142                     'force_smil_url': True
143                 }),
144             'id': video_id,
145         }