Merge branch 'akamai_pv' of https://github.com/remitamine/youtube-dl into remitamine...
[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 js_to_json
8
9
10 class CBCIE(InfoExtractor):
11     _VALID_URL = r'https?://(?:www\.)?cbc\.ca/(?:[^/]+/)+(?P<id>[^/?#]+)'
12     _TESTS = [{
13         # with mediaId
14         'url': 'http://www.cbc.ca/22minutes/videos/clips-season-23/don-cherry-play-offs',
15         'info_dict': {
16             'id': '2682904050',
17             'ext': 'flv',
18             'title': 'Don Cherry – All-Stars',
19             'description': 'Don Cherry has a bee in his bonnet about AHL player John Scott because that guy’s got heart.',
20             'timestamp': 1454475540,
21             'upload_date': '20160203',
22         },
23         'params': {
24             # rtmp download
25             'skip_download': True,
26         },
27     }, {
28         # with clipId
29         'url': 'http://www.cbc.ca/archives/entry/1978-robin-williams-freestyles-on-90-minutes-live',
30         'info_dict': {
31             'id': '2487345465',
32             'ext': 'flv',
33             'title': 'Robin Williams freestyles on 90 Minutes Live',
34             'description': 'Wacky American comedian Robin Williams shows off his infamous "freestyle" comedic talents while being interviewed on CBC\'s 90 Minutes Live.',
35             'upload_date': '19700101',
36             'uploader': 'CBCC-NEW',
37         },
38         'params': {
39             # rtmp download
40             'skip_download': True,
41         },
42     }, {
43         # multiple iframes
44         'url': 'http://www.cbc.ca/natureofthings/blog/birds-eye-view-from-vancouvers-burrard-street-bridge-how-we-got-the-shot',
45         'playlist': [{
46             'info_dict': {
47                 'id': '2680832926',
48                 'ext': 'flv',
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': '19700101',
52             },
53         }, {
54             'info_dict': {
55                 'id': '2658915080',
56                 'ext': 'flv',
57                 'title': 'Fly like an eagle!',
58                 'description': 'Eagle equipped with a mini camera flies from the world\'s tallest tower',
59                 'upload_date': '19700101',
60             },
61         }],
62         'params': {
63             # rtmp download
64             'skip_download': True,
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     _TEST = {
95         'url': 'http://www.cbc.ca/player/play/2683190193',
96         'info_dict': {
97             'id': '2683190193',
98             'ext': 'flv',
99             'title': 'Gerry Runs a Sweat Shop',
100             'description': 'md5:b457e1c01e8ff408d9d801c1c2cd29b0',
101             'timestamp': 1455067800,
102             'upload_date': '20160210',
103         },
104         'params': {
105             # rtmp download
106             'skip_download': True,
107         },
108     }
109
110     def _real_extract(self, url):
111         video_id = self._match_id(url)
112         return self.url_result(
113             'http://feed.theplatform.com/f/ExhSPC/vms_5akSXx4Ng_Zn?byGuid=%s' % video_id,
114             'ThePlatformFeed', video_id)