Merge pull request #8130 from dyn888/master
[youtube-dl] / youtube_dl / extractor / canalc2.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 parse_duration
8
9
10 class Canalc2IE(InfoExtractor):
11     IE_NAME = 'canalc2.tv'
12     _VALID_URL = r'https?://(?:(?:www\.)?canalc2\.tv/video/|archives-canalc2\.u-strasbg\.fr/video\.asp\?.*\bidVideo=)(?P<id>\d+)'
13
14     _TESTS = [{
15         'url': 'http://www.canalc2.tv/video/12163',
16         'md5': '060158428b650f896c542dfbb3d6487f',
17         'info_dict': {
18             'id': '12163',
19             'ext': 'flv',
20             'title': 'Terrasses du Numérique',
21             'duration': 122,
22         },
23         'params': {
24             'skip_download': True,  # Requires rtmpdump
25         }
26     }, {
27         'url': 'http://archives-canalc2.u-strasbg.fr/video.asp?idVideo=11427&voir=oui',
28         'only_matching': True,
29     }]
30
31     def _real_extract(self, url):
32         video_id = self._match_id(url)
33
34         webpage = self._download_webpage(
35             'http://www.canalc2.tv/video/%s' % video_id, video_id)
36
37         formats = []
38         for _, video_url in re.findall(r'file\s*=\s*(["\'])(.+?)\1', webpage):
39             if video_url.startswith('rtmp://'):
40                 rtmp = re.search(
41                     r'^(?P<url>rtmp://[^/]+/(?P<app>.+/))(?P<play_path>mp4:.+)$', video_url)
42                 formats.append({
43                     'url': rtmp.group('url'),
44                     'format_id': 'rtmp',
45                     'ext': 'flv',
46                     'app': rtmp.group('app'),
47                     'play_path': rtmp.group('play_path'),
48                     'page_url': url,
49                 })
50             else:
51                 formats.append({
52                     'url': video_url,
53                     'format_id': 'http',
54                 })
55         self._sort_formats(formats)
56
57         title = self._html_search_regex(
58             r'(?s)class="[^"]*col_description[^"]*">.*?<h3>(.*?)</h3>', webpage, 'title')
59         duration = parse_duration(self._search_regex(
60             r'id=["\']video_duree["\'][^>]*>([^<]+)',
61             webpage, 'duration', fatal=False))
62
63         return {
64             'id': video_id,
65             'title': title,
66             'duration': duration,
67             'formats': formats,
68         }