[ndr] Extended to support n-joy.de as well (closes #4527)
[youtube-dl] / youtube_dl / extractor / ndr.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     ExtractorError,
9     int_or_none,
10     qualities,
11     parse_duration,
12 )
13
14
15 class NDRBaseIE(InfoExtractor):
16     def _real_extract(self, url):
17         mobj = re.match(self._VALID_URL, url)
18         video_id = mobj.group('id')
19
20         page = self._download_webpage(url, video_id, 'Downloading page')
21
22         title = self._og_search_title(page).strip()
23         description = self._og_search_description(page)
24         if description:
25             description = description.strip()
26
27         duration = int_or_none(self._html_search_regex(r'duration: (\d+),\n', page, 'duration', default=None))
28         if not duration:
29             duration = parse_duration(self._html_search_regex(
30                 r'(<span class="min">\d+</span>:<span class="sec">\d+</span>)',
31                 page, 'duration', default=None))
32
33         formats = []
34
35         mp3_url = re.search(r'''\{src:'(?P<audio>[^']+)', type:"audio/mp3"},''', page)
36         if mp3_url:
37             formats.append({
38                 'url': mp3_url.group('audio'),
39                 'format_id': 'mp3',
40             })
41
42         thumbnail = None
43
44         video_url = re.search(r'''3: \{src:'(?P<video>.+?)\.(lo|hi|hq)\.mp4', type:"video/mp4"},''', page)
45         if video_url:
46             thumbnails = re.findall(r'''\d+: \{src: "([^"]+)"(?: \|\| '[^']+')?, quality: '([^']+)'}''', page)
47             if thumbnails:
48                 quality_key = qualities(['xs', 's', 'm', 'l', 'xl'])
49                 largest = max(thumbnails, key=lambda thumb: quality_key(thumb[1]))
50                 thumbnail = 'http://www.ndr.de' + largest[0]
51
52             for format_id in 'lo', 'hi', 'hq':
53                 formats.append({
54                     'url': '%s.%s.mp4' % (video_url.group('video'), format_id),
55                     'format_id': format_id,
56                 })
57
58         if not formats:
59             raise ExtractorError('No media links available for %s' % video_id)
60
61         return {
62             'id': video_id,
63             'title': title,
64             'description': description,
65             'thumbnail': thumbnail,
66             'duration': duration,
67             'formats': formats,
68         }
69
70
71 class NDRIE(NDRBaseIE):
72     IE_NAME = 'ndr'
73     IE_DESC = 'NDR.de - Mediathek'
74     _VALID_URL = r'https?://www\.ndr\.de/.+?(?P<id>\d+)\.html'
75
76     _TESTS = [
77         {
78             'url': 'http://www.ndr.de/fernsehen/sendungen/nordmagazin/Kartoffeltage-in-der-Lewitz,nordmagazin25866.html',
79             'md5': '5bc5f5b92c82c0f8b26cddca34f8bb2c',
80             'note': 'Video file',
81             'info_dict': {
82                 'id': '25866',
83                 'ext': 'mp4',
84                 'title': 'Kartoffeltage in der Lewitz',
85                 'description': 'md5:48c4c04dde604c8a9971b3d4e3b9eaa8',
86                 'duration': 166,
87             }
88         },
89         {
90             'url': 'http://www.ndr.de/info/audio51535.html',
91             'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
92             'note': 'Audio file',
93             'info_dict': {
94                 'id': '51535',
95                 'ext': 'mp3',
96                 'title': 'La Valette entgeht der Hinrichtung',
97                 'description': 'md5:22f9541913a40fe50091d5cdd7c9f536',
98                 'duration': 884,
99             }
100         }
101     ]
102
103
104 class NJoyIE(NDRBaseIE):
105     IE_NAME = 'N-JOY'
106     _VALID_URL = r'https?://www\.n-joy\.de/.+?(?P<id>\d+)\.html'
107
108     _TEST = {
109         'url': 'http://www.n-joy.de/entertainment/comedy/comedy_contest/Benaissa-beim-NDR-Comedy-Contest,comedycontest2480.html',
110         'md5': 'cb63be60cd6f9dd75218803146d8dc67',
111         'info_dict': {
112             'id': '2480',
113             'ext': 'mp4',
114             'title': 'Benaissa beim NDR Comedy Contest',
115             'description': 'Von seinem sehr "behaarten" Leben lässt sich Benaissa trotz aller Schwierigkeiten nicht unterkriegen.',
116             'duration': 654,
117         }
118     }