[ccc] Update _VALID_URL (Closes #8097)
[youtube-dl] / youtube_dl / extractor / ccc.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import (
7     int_or_none,
8     qualities,
9     unified_strdate,
10 )
11
12
13 class CCCIE(InfoExtractor):
14     IE_NAME = 'media.ccc.de'
15     _VALID_URL = r'https?://(?:www\.)?media\.ccc\.de/v/(?P<id>[^/?#&]+)'
16
17     _TESTS = [{
18         'url': 'https://media.ccc.de/v/30C3_-_5443_-_en_-_saal_g_-_201312281830_-_introduction_to_processor_design_-_byterazor#video',
19         'md5': '3a1eda8f3a29515d27f5adb967d7e740',
20         'info_dict': {
21             'id': '30C3_-_5443_-_en_-_saal_g_-_201312281830_-_introduction_to_processor_design_-_byterazor',
22             'ext': 'mp4',
23             'title': 'Introduction to Processor Design',
24             'description': 'md5:5ddbf8c734800267f2cee4eab187bc1b',
25             'thumbnail': 're:^https?://.*\.jpg$',
26             'view_count': int,
27             'upload_date': '20131229',
28         }
29     }, {
30         'url': 'https://media.ccc.de/v/32c3-7368-shopshifting#download',
31         'only_matching': True,
32     }]
33
34     def _real_extract(self, url):
35         video_id = self._match_id(url)
36         webpage = self._download_webpage(url, video_id)
37
38         if self._downloader.params.get('prefer_free_formats'):
39             preference = qualities(['mp3', 'opus', 'mp4-lq', 'webm-lq', 'h264-sd', 'mp4-sd', 'webm-sd', 'mp4', 'webm', 'mp4-hd', 'h264-hd', 'webm-hd'])
40         else:
41             preference = qualities(['opus', 'mp3', 'webm-lq', 'mp4-lq', 'webm-sd', 'h264-sd', 'mp4-sd', 'webm', 'mp4', 'webm-hd', 'mp4-hd', 'h264-hd'])
42
43         title = self._html_search_regex(
44             r'(?s)<h1>(.*?)</h1>', webpage, 'title')
45         description = self._html_search_regex(
46             r"(?s)<p class='description'>(.*?)</p>",
47             webpage, 'description', fatal=False)
48         upload_date = unified_strdate(self._html_search_regex(
49             r"(?s)<span class='[^']*fa-calendar-o'></span>(.*?)</li>",
50             webpage, 'upload date', fatal=False))
51         view_count = int_or_none(self._html_search_regex(
52             r"(?s)<span class='[^']*fa-eye'></span>(.*?)</li>",
53             webpage, 'view count', fatal=False))
54
55         matches = re.finditer(r'''(?xs)
56             <(?:span|div)\s+class='label\s+filetype'>(?P<format>.*?)</(?:span|div)>\s*
57             <a\s+download\s+href='(?P<http_url>[^']+)'>\s*
58             (?:
59                 .*?
60                 <a\s+href='(?P<torrent_url>[^']+\.torrent)'
61             )?''', webpage)
62         formats = []
63         for m in matches:
64             format = m.group('format')
65             format_id = self._search_regex(
66                 r'.*/([a-z0-9_-]+)/[^/]*$',
67                 m.group('http_url'), 'format id', default=None)
68             vcodec = 'h264' if 'h264' in format_id else (
69                 'none' if format_id in ('mp3', 'opus') else None
70             )
71             formats.append({
72                 'format_id': format_id,
73                 'format': format,
74                 'url': m.group('http_url'),
75                 'vcodec': vcodec,
76                 'preference': preference(format_id),
77             })
78
79             if m.group('torrent_url'):
80                 formats.append({
81                     'format_id': 'torrent-%s' % (format if format_id is None else format_id),
82                     'format': '%s (torrent)' % format,
83                     'proto': 'torrent',
84                     'format_note': '(unsupported; will just download the .torrent file)',
85                     'vcodec': vcodec,
86                     'preference': -100 + preference(format_id),
87                     'url': m.group('torrent_url'),
88                 })
89         self._sort_formats(formats)
90
91         thumbnail = self._html_search_regex(
92             r"<video.*?poster='([^']+)'", webpage, 'thumbnail', fatal=False)
93
94         return {
95             'id': video_id,
96             'title': title,
97             'description': description,
98             'thumbnail': thumbnail,
99             'view_count': view_count,
100             'upload_date': upload_date,
101             'formats': formats,
102         }