[youtube] Fix extraction.
[youtube-dl] / youtube_dl / extractor / videopress.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_str
8 from ..utils import (
9     determine_ext,
10     float_or_none,
11     parse_age_limit,
12     qualities,
13     random_birthday,
14     try_get,
15     unified_timestamp,
16     urljoin,
17 )
18
19
20 class VideoPressIE(InfoExtractor):
21     _VALID_URL = r'https?://videopress\.com/embed/(?P<id>[\da-zA-Z]+)'
22     _TESTS = [{
23         'url': 'https://videopress.com/embed/kUJmAcSf',
24         'md5': '706956a6c875873d51010921310e4bc6',
25         'info_dict': {
26             'id': 'kUJmAcSf',
27             'ext': 'mp4',
28             'title': 'VideoPress Demo',
29             'thumbnail': r're:^https?://.*\.jpg',
30             'duration': 634.6,
31             'timestamp': 1434983935,
32             'upload_date': '20150622',
33             'age_limit': 0,
34         },
35     }, {
36         # 17+, requires birth_* params
37         'url': 'https://videopress.com/embed/iH3gstfZ',
38         'only_matching': True,
39     }]
40
41     @staticmethod
42     def _extract_urls(webpage):
43         return re.findall(
44             r'<iframe[^>]+src=["\']((?:https?://)?videopress\.com/embed/[\da-zA-Z]+)',
45             webpage)
46
47     def _real_extract(self, url):
48         video_id = self._match_id(url)
49
50         query = random_birthday('birth_year', 'birth_month', 'birth_day')
51         video = self._download_json(
52             'https://public-api.wordpress.com/rest/v1.1/videos/%s' % video_id,
53             video_id, query=query)
54
55         title = video['title']
56
57         def base_url(scheme):
58             return try_get(
59                 video, lambda x: x['file_url_base'][scheme], compat_str)
60
61         base_url = base_url('https') or base_url('http')
62
63         QUALITIES = ('std', 'dvd', 'hd')
64         quality = qualities(QUALITIES)
65
66         formats = []
67         for format_id, f in video['files'].items():
68             if not isinstance(f, dict):
69                 continue
70             for ext, path in f.items():
71                 if ext in ('mp4', 'ogg'):
72                     formats.append({
73                         'url': urljoin(base_url, path),
74                         'format_id': '%s-%s' % (format_id, ext),
75                         'ext': determine_ext(path, ext),
76                         'quality': quality(format_id),
77                     })
78         original_url = try_get(video, lambda x: x['original'], compat_str)
79         if original_url:
80             formats.append({
81                 'url': original_url,
82                 'format_id': 'original',
83                 'quality': len(QUALITIES),
84             })
85         self._sort_formats(formats)
86
87         return {
88             'id': video_id,
89             'title': title,
90             'description': video.get('description'),
91             'thumbnail': video.get('poster'),
92             'duration': float_or_none(video.get('duration'), 1000),
93             'timestamp': unified_timestamp(video.get('upload_date')),
94             'age_limit': parse_age_limit(video.get('rating')),
95             'formats': formats,
96         }