[porn91] Extract more info
[youtube-dl] / youtube_dl / extractor / porn91.py
1 # encoding: utf-8
2 from __future__ import unicode_literals
3
4 from ..compat import compat_urllib_parse
5 from .common import InfoExtractor
6 from ..utils import (
7     parse_duration,
8     int_or_none,
9 )
10
11
12 class Porn91IE(InfoExtractor):
13     IE_NAME = '91porn'
14     _VALID_URL = r'(?:https?://)(?:www\.|)91porn\.com/.+?\?viewkey=(?P<id>[\w\d]+)'
15
16     _TEST = {
17         'url': 'http://91porn.com/view_video.php?viewkey=7e42283b4f5ab36da134',
18         'md5': '6df8f6d028bc8b14f5dbd73af742fb20',
19         'info_dict': {
20             'id': '7e42283b4f5ab36da134',
21             'title': '18岁大一漂亮学妹,水嫩性感,再爽一次!',
22             'ext': 'mp4',
23             'duration': 431,
24         }
25     }
26
27     def _real_extract(self, url):
28         video_id = self._match_id(url)
29         url = 'http://91porn.com/view_video.php?viewkey=%s' % video_id
30         self._set_cookie('91porn.com', 'language', 'cn_CN')
31         webpage = self._download_webpage(url, video_id, "get HTML content")
32         title = self._search_regex(
33             r'<div id="viewvideo-title">([^<]+)</div>', webpage, 'title')
34         title = title.replace('\n', '')
35
36         # get real url
37         file_id = self._search_regex(
38             r'so.addVariable\(\'file\',\'(\d+)\'', webpage, 'file id')
39         sec_code = self._search_regex(
40             r'so.addVariable\(\'seccode\',\'([^\']+)\'', webpage, 'sec code')
41         max_vid = self._search_regex(
42             r'so.addVariable\(\'max_vid\',\'(\d+)\'', webpage, 'max vid')
43         url_params = compat_urllib_parse.urlencode({
44             'VID': file_id,
45             'mp4': '1',
46             'seccode': sec_code,
47             'max_vid': max_vid,
48         })
49         info_cn = self._download_webpage(
50             'http://91porn.com/getfile.php?' + url_params, video_id,
51             "get real video url")
52         video_url = self._search_regex(r'file=([^&]+)&', info_cn, 'url')
53
54         duration = parse_duration(self._search_regex(
55             r'时长:\s*</span>\s*(\d+:\d+)', webpage, 'duration', fatal=False))
56
57         comment_count = int_or_none(self._search_regex(
58             r'留言:\s*</span>\s*(\d+)', webpage, 'comment count', fatal=False))
59
60         return {
61             'id': video_id,
62             'title': title,
63             'url': video_url,
64             'duration': duration,
65             'comment_count': comment_count,
66         }