[ctvnews] Add new extractor(closes #2156)
[youtube-dl] / youtube_dl / extractor / ctvnews.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7
8
9 class CTVNewsIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?ctvnews\.ca/(?:video\?(?:clip|playlist|bin)Id=|.*?)(?P<id>[0-9.]+)'
11     _TESTS = [{
12         'url': 'http://www.ctvnews.ca/video?clipId=901995',
13         'md5': '10deb320dc0ccb8d01d34d12fc2ea672',
14         'info_dict': {
15             'id': '901995',
16             'ext': 'mp4',
17             'title': 'Extended: \'That person cannot be me\' Johnson says',
18             'description': 'md5:958dd3b4f5bbbf0ed4d045c790d89285',
19             'timestamp': 1467286284,
20             'upload_date': '20160630',
21         }
22     }, {
23         'url': 'http://www.ctvnews.ca/video?playlistId=1.2966224',
24         'info_dict':
25         {
26             'id': '1.2966224',
27         },
28         'playlist_mincount': 19,
29     }, {
30         'url': 'http://www.ctvnews.ca/video?binId=1.810401',
31         'info_dict':
32         {
33             'id': '1.810401',
34         },
35         'playlist_mincount': 91,
36     }, {
37         'url': 'http://www.ctvnews.ca/1.810401',
38         'only_matching': True,
39     }, {
40         'url': 'http://www.ctvnews.ca/canadiens-send-p-k-subban-to-nashville-in-blockbuster-trade-1.2967231',
41         'only_matching': True,
42     }]
43
44     def _real_extract(self, url):
45         page_id = self._match_id(url)
46
47         def ninecninemedia_url_result(clip_id):
48             return {
49                 '_type': 'url_transparent',
50                 'id': clip_id,
51                 'url': '9c9media:ctvnews_web:%s' % clip_id,
52                 'ie_key': 'NineCNineMedia',
53             }
54
55         if page_id.isdigit():
56             return ninecninemedia_url_result(page_id)
57         else:
58             webpage = self._download_webpage('http://www.ctvnews.ca/%s' % page_id, page_id, query={
59                 'ot': 'example.AjaxPageLayout.ot',
60                 'maxItemsPerPage': 20,
61             })
62             entries = [ninecninemedia_url_result(clip_id) for clip_id in set(
63                 re.findall(r'clip\.id\s*=\s*(\d+);', webpage))]
64             return self.playlist_result(entries, page_id)