Merge pull request #2939 from codesparkle/upload-date-fix
[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 )
11
12
13 class NDRIE(InfoExtractor):
14     IE_NAME = 'ndr'
15     IE_DESC = 'NDR.de - Mediathek'
16     _VALID_URL = r'https?://www\.ndr\.de/.+?(?P<id>\d+)\.html'
17
18     _TESTS = [
19         {
20             'url': 'http://www.ndr.de/fernsehen/sendungen/markt/markt7959.html',
21             'md5': 'e7a6079ca39d3568f4996cb858dd6708',
22             'note': 'Video file',
23             'info_dict': {
24                 'id': '7959',
25                 'ext': 'mp4',
26                 'title': 'Markt - die ganze Sendung',
27                 'description': 'md5:af9179cf07f67c5c12dc6d9997e05725',
28                 'duration': 2655,
29             },
30         },
31         {
32             'url': 'http://www.ndr.de/info/audio51535.html',
33             'md5': 'bb3cd38e24fbcc866d13b50ca59307b8',
34             'note': 'Audio file',
35             'info_dict': {
36                 'id': '51535',
37                 'ext': 'mp3',
38                 'title': 'La Valette entgeht der Hinrichtung',
39                 'description': 'md5:22f9541913a40fe50091d5cdd7c9f536',
40                 'duration': 884,
41             }
42         }
43     ]
44
45     def _real_extract(self, url):
46         mobj = re.match(self._VALID_URL, url)
47         video_id = mobj.group('id')
48
49         page = self._download_webpage(url, video_id, 'Downloading page')
50
51         title = self._og_search_title(page).strip()
52         description = self._og_search_description(page)
53         if description:
54             description = description.strip()
55
56         duration = int_or_none(self._html_search_regex(r'duration: (\d+),\n', page, 'duration', fatal=False))
57
58         formats = []
59
60         mp3_url = re.search(r'''{src:'(?P<audio>[^']+)', type:"audio/mp3"},''', page)
61         if mp3_url:
62             formats.append({
63                 'url': mp3_url.group('audio'),
64                 'format_id': 'mp3',
65             })
66
67         thumbnail = None
68
69         video_url = re.search(r'''3: {src:'(?P<video>.+?)\.hi\.mp4', type:"video/mp4"},''', page)
70         if video_url:
71             thumbnails = re.findall(r'''\d+: {src: "([^"]+)"(?: \|\| '[^']+')?, quality: '([^']+)'}''', page)
72             if thumbnails:
73                 QUALITIES = ['xs', 's', 'm', 'l', 'xl']
74                 thumbnails.sort(key=lambda thumb: QUALITIES.index(thumb[1]) if thumb[1] in QUALITIES else -1)
75                 thumbnail = 'http://www.ndr.de' + thumbnails[-1][0]
76
77             for format_id in ['lo', 'hi', 'hq']:
78                 formats.append({
79                     'url': '%s.%s.mp4' % (video_url.group('video'), format_id),
80                     'format_id': format_id,
81                 })
82
83         if not formats:
84             raise ExtractorError('No media links available for %s' % video_id)
85
86         return {
87             'id': video_id,
88             'title': title,
89             'description': description,
90             'thumbnail': thumbnail,
91             'duration': duration,
92             'formats': formats,
93         }