[dcn] make m3u8 formats extraction non fatal
[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             'skip': '404 Not found',
89         },
90         {
91             'url': 'http://www.ndr.de/fernsehen/Party-Poette-und-Parade,hafengeburtstag988.html',
92             'md5': 'dadc003c55ae12a5d2f6bd436cd73f59',
93             'info_dict': {
94                 'id': '988',
95                 'ext': 'mp4',
96                 'title': 'Party, Pötte und Parade',
97                 'description': 'Hunderttausende feiern zwischen Speicherstadt und St. Pauli den 826. Hafengeburtstag. Die NDR Sondersendung zeigt die schönsten und spektakulärsten Bilder vom Auftakt.',
98                 'duration': 3498,
99             },
100         },
101         {
102             'url': 'http://www.ndr.de/info/audio51535.html',
103             'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
104             'note': 'Audio file',
105             'info_dict': {
106                 'id': '51535',
107                 'ext': 'mp3',
108                 'title': 'La Valette entgeht der Hinrichtung',
109                 'description': 'md5:22f9541913a40fe50091d5cdd7c9f536',
110                 'duration': 884,
111             }
112         }
113     ]
114
115
116 class NJoyIE(NDRBaseIE):
117     IE_NAME = 'N-JOY'
118     _VALID_URL = r'https?://www\.n-joy\.de/.+?(?P<id>\d+)\.html'
119
120     _TEST = {
121         'url': 'http://www.n-joy.de/entertainment/comedy/comedy_contest/Benaissa-beim-NDR-Comedy-Contest,comedycontest2480.html',
122         'md5': 'cb63be60cd6f9dd75218803146d8dc67',
123         'info_dict': {
124             'id': '2480',
125             'ext': 'mp4',
126             'title': 'Benaissa beim NDR Comedy Contest',
127             'description': 'Von seinem sehr "behaarten" Leben lässt sich Benaissa trotz aller Schwierigkeiten nicht unterkriegen.',
128             'duration': 654,
129         }
130     }