[cbc] Add the test case from #5156
[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         'info_dict': {
32             'id': '2487345465',
33             'ext': 'mp4',
34             'title': 'Robin Williams freestyles on 90 Minutes Live',
35             'description': 'Wacky American comedian Robin Williams shows off his infamous "freestyle" comedic talents while being interviewed on CBC\'s 90 Minutes Live.',
36             'upload_date': '19780210',
37             'uploader': 'CBCC-NEW',
38             'timestamp': 255977160,
39         },
40     }, {
41         # multiple iframes
42         'url': 'http://www.cbc.ca/natureofthings/blog/birds-eye-view-from-vancouvers-burrard-street-bridge-how-we-got-the-shot',
43         'playlist': [{
44             'md5': '377572d0b49c4ce0c9ad77470e0b96b4',
45             'info_dict': {
46                 'id': '2680832926',
47                 'ext': 'mp4',
48                 'title': 'An Eagle\'s-Eye View Off Burrard Bridge',
49                 'description': 'Hercules the eagle flies from Vancouver\'s Burrard Bridge down to a nearby park with a mini-camera strapped to his back.',
50                 'upload_date': '20160201',
51                 'timestamp': 1454342820,
52                 'uploader': 'CBCC-NEW',
53             },
54         }, {
55             'md5': '415a0e3f586113894174dfb31aa5bb1a',
56             'info_dict': {
57                 'id': '2658915080',
58                 'ext': 'mp4',
59                 'title': 'Fly like an eagle!',
60                 'description': 'Eagle equipped with a mini camera flies from the world\'s tallest tower',
61                 'upload_date': '20150315',
62                 'timestamp': 1426443984,
63                 'uploader': 'CBCC-NEW',
64             },
65         }],
66     }]
67
68     @classmethod
69     def suitable(cls, url):
70         return False if CBCPlayerIE.suitable(url) else super(CBCIE, cls).suitable(url)
71
72     def _real_extract(self, url):
73         display_id = self._match_id(url)
74         webpage = self._download_webpage(url, display_id)
75         player_init = self._search_regex(
76             r'CBC\.APP\.Caffeine\.initInstance\(({.+?})\);', webpage, 'player init',
77             default=None)
78         if player_init:
79             player_info = self._parse_json(player_init, display_id, js_to_json)
80             media_id = player_info.get('mediaId')
81             if not media_id:
82                 clip_id = player_info['clipId']
83                 media_id = self._download_json(
84                     'http://feed.theplatform.com/f/h9dtGB/punlNGjMlc1F?fields=id&byContent=byReleases%3DbyId%253D' + clip_id,
85                     clip_id)['entries'][0]['id'].split('/')[-1]
86             return self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id)
87         else:
88             entries = [self.url_result('cbcplayer:%s' % media_id, 'CBCPlayer', media_id) for media_id in re.findall(r'<iframe[^>]+src="[^"]+?mediaId=(\d+)"', webpage)]
89             return self.playlist_result(entries)
90
91
92 class CBCPlayerIE(InfoExtractor):
93     _VALID_URL = r'(?:cbcplayer:|https?://(?:www\.)?cbc\.ca/(?:player/play/|i/caffeine/syndicate/\?mediaId=))(?P<id>\d+)'
94     _TESTS = [{
95         'url': 'http://www.cbc.ca/player/play/2683190193',
96         'info_dict': {
97             'id': '2683190193',
98             'ext': 'mp4',
99             'title': 'Gerry Runs a Sweat Shop',
100             'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
101             'timestamp': 1455071400,
102             'upload_date': '20160210',
103             'uploader': 'CBCC-NEW',
104         },
105     }, {
106         # Redirected from http://www.cbc.ca/player/AudioMobile/All%20in%20a%20Weekend%20Montreal/ID/2657632011/
107         'url': 'http://www.cbc.ca/player/play/2657631896',
108         'md5': 'e5e708c34ae6fca156aafe17c43e8b75',
109         'info_dict': {
110             'id': '2657631896',
111             'ext': 'mp3',
112             'title': 'CBC Montreal is organizing its first ever community hackathon!',
113             '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.',
114             'timestamp': 1425704400,
115             'upload_date': '20150307',
116             'uploader': 'CBCC-NEW',
117         },
118     }]
119
120     def _real_extract(self, url):
121         video_id = self._match_id(url)
122         return {
123             '_type': 'url_transparent',
124             'ie_key': 'ThePlatform',
125             'url': smuggle_url(
126                 'http://link.theplatform.com/s/ExhSPC/media/guid/2655402169/%s?mbr=true' % video_id, {
127                     'force_smil_url': True
128                 }),
129             'id': video_id,
130         }