[heise] Simplify (#3842)
[youtube-dl] / youtube_dl / extractor / heise.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..utils import (
6     get_meta_content,
7     parse_iso8601,
8 )
9
10
11 class HeiseIE(InfoExtractor):
12     _VALID_URL = r'''(?x)
13         https?://(?:www\.)?heise\.de/video/artikel/
14         .+?(?P<id>[0-9]+)\.html(?:$|[?#])
15     '''
16     _TEST = {
17         'url': (
18             'http://www.heise.de/video/artikel/Podcast-c-t-uplink-3-3-Owncloud-Tastaturen-Peilsender-Smartphone-2404147.html'
19         ),
20         'md5': 'ffed432483e922e88545ad9f2f15d30e',
21         'info_dict': {
22             'id': '2404147',
23             'ext': 'mp4',
24             'title': (
25                 "Podcast: c't uplink 3.3 – Owncloud / Tastaturen / Peilsender Smartphone"
26             ),
27             'format_id': 'mp4_720',
28             'timestamp': 1411812600,
29             'upload_date': '20140927',
30         }
31     }
32
33     def _real_extract(self, url):
34         video_id = self._match_id(url)
35
36         webpage = self._download_webpage(url, video_id)
37         json_url = self._search_regex(
38             r'json_url:\s*"([^"]+)"', webpage, 'json URL')
39         config = self._download_json(json_url, video_id)
40
41         info = {
42             'id': video_id,
43             'thumbnail': config.get('poster'),
44             'timestamp': parse_iso8601(get_meta_content('date', webpage)),
45         }
46
47         title = get_meta_content('fulltitle', webpage)
48         if title:
49             info['title'] = title
50         elif config.get('title'):
51             info['title'] = config['title']
52         else:
53             info['title'] = self._og_search_title(webpage)
54
55         formats = []
56         for t, rs in config['formats'].items():
57             if not rs or not hasattr(rs, 'items'):
58                 self._downloader.report_warning(
59                     'formats: {0}: no resolutions'.format(t))
60                 continue
61
62             for height_str, obj in rs.items():
63                 format_id = '{0}_{1}'.format(t, height_str)
64
65                 if not obj or not obj.get('url'):
66                     self._downloader.report_warning(
67                         'formats: {0}: no url'.format(format_id))
68                     continue
69
70                 formats.append({
71                     'url': obj['url'],
72                     'format_id': format_id,
73                     'height': self._int(height_str, 'height'),
74                 })
75
76         self._sort_formats(formats)
77         info['formats'] = formats
78
79         return info