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