[youtube] fix extraction for embed restricted live streams(fixes #16433)
[youtube-dl] / youtube_dl / extractor / iwara.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import compat_urllib_parse_urlparse
6 from ..utils import (
7     int_or_none,
8     mimetype2ext,
9     remove_end,
10 )
11
12
13 class IwaraIE(InfoExtractor):
14     _VALID_URL = r'https?://(?:www\.|ecchi\.)?iwara\.tv/videos/(?P<id>[a-zA-Z0-9]+)'
15     _TESTS = [{
16         'url': 'http://iwara.tv/videos/amVwUl1EHpAD9RD',
17         # md5 is unstable
18         'info_dict': {
19             'id': 'amVwUl1EHpAD9RD',
20             'ext': 'mp4',
21             'title': '【MMD R-18】ガールフレンド carry_me_off',
22             'age_limit': 18,
23         },
24     }, {
25         'url': 'http://ecchi.iwara.tv/videos/Vb4yf2yZspkzkBO',
26         'md5': '7e5f1f359cd51a027ba4a7b7710a50f0',
27         'info_dict': {
28             'id': '0B1LvuHnL-sRFNXB1WHNqbGw4SXc',
29             'ext': 'mp4',
30             'title': '[3D Hentai] Kyonyu × Genkai × Emaki Shinobi Girls.mp4',
31             'age_limit': 18,
32         },
33         'add_ie': ['GoogleDrive'],
34     }, {
35         'url': 'http://www.iwara.tv/videos/nawkaumd6ilezzgq',
36         # md5 is unstable
37         'info_dict': {
38             'id': '6liAP9s2Ojc',
39             'ext': 'mp4',
40             'age_limit': 18,
41             'title': '[MMD] Do It Again Ver.2 [1080p 60FPS] (Motion,Camera,Wav+DL)',
42             'description': 'md5:590c12c0df1443d833fbebe05da8c47a',
43             'upload_date': '20160910',
44             'uploader': 'aMMDsork',
45             'uploader_id': 'UCVOFyOSCyFkXTYYHITtqB7A',
46         },
47         'add_ie': ['Youtube'],
48     }]
49
50     def _real_extract(self, url):
51         video_id = self._match_id(url)
52
53         webpage, urlh = self._download_webpage_handle(url, video_id)
54
55         hostname = compat_urllib_parse_urlparse(urlh.geturl()).hostname
56         # ecchi is 'sexy' in Japanese
57         age_limit = 18 if hostname.split('.')[0] == 'ecchi' else 0
58
59         video_data = self._download_json('http://www.iwara.tv/api/video/%s' % video_id, video_id)
60
61         if not video_data:
62             iframe_url = self._html_search_regex(
63                 r'<iframe[^>]+src=([\'"])(?P<url>[^\'"]+)\1',
64                 webpage, 'iframe URL', group='url')
65             return {
66                 '_type': 'url_transparent',
67                 'url': iframe_url,
68                 'age_limit': age_limit,
69             }
70
71         title = remove_end(self._html_search_regex(
72             r'<title>([^<]+)</title>', webpage, 'title'), ' | Iwara')
73
74         formats = []
75         for a_format in video_data:
76             format_id = a_format.get('resolution')
77             height = int_or_none(self._search_regex(
78                 r'(\d+)p', format_id, 'height', default=None))
79             formats.append({
80                 'url': a_format['uri'],
81                 'format_id': format_id,
82                 'ext': mimetype2ext(a_format.get('mime')) or 'mp4',
83                 'height': height,
84                 'width': int_or_none(height / 9.0 * 16.0 if height else None),
85                 'quality': 1 if format_id == 'Source' else 0,
86             })
87
88         self._sort_formats(formats)
89
90         return {
91             'id': video_id,
92             'title': title,
93             'age_limit': age_limit,
94             'formats': formats,
95         }