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