[videopress] Add extractor
[youtube-dl] / youtube_dl / extractor / videopress.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 import random
5 import re
6
7 from .common import InfoExtractor
8 from ..compat import compat_str
9 from ..utils import (
10     determine_ext,
11     float_or_none,
12     parse_age_limit,
13     qualities,
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         video = self._download_json(
51             'https://public-api.wordpress.com/rest/v1.1/videos/%s' % video_id,
52             video_id, query={
53                 'birth_month': random.randint(1, 12),
54                 'birth_day': random.randint(1, 31),
55                 'birth_year': random.randint(1950, 1995),
56             })
57
58         title = video['title']
59
60         def base_url(scheme):
61             return try_get(
62                 video, lambda x: x['file_url_base'][scheme], compat_str)
63
64         base_url = base_url('https') or base_url('http')
65
66         QUALITIES = ('std', 'dvd', 'hd')
67         quality = qualities(QUALITIES)
68
69         formats = []
70         for format_id, f in video['files'].items():
71             if not isinstance(f, dict):
72                 continue
73             for ext, path in f.items():
74                 if ext in ('mp4', 'ogg'):
75                     formats.append({
76                         'url': urljoin(base_url, path),
77                         'format_id': '%s-%s' % (format_id, ext),
78                         'ext': determine_ext(path, ext),
79                         'quality': quality(format_id),
80                     })
81         original_url = try_get(video, lambda x: x['original'], compat_str)
82         if original_url:
83             formats.append({
84                 'url': original_url,
85                 'format_id': 'original',
86                 'quality': len(QUALITIES),
87             })
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'title': title,
93             'description': video.get('description'),
94             'thumbnail': video.get('poster'),
95             'duration': float_or_none(video.get('duration'), 1000),
96             'timestamp': unified_timestamp(video.get('upload_date')),
97             'age_limit': parse_age_limit(video.get('rating')),
98             'formats': formats,
99         }