Switch codebase to use sanitized_Request instead of
[youtube-dl] / youtube_dl / extractor / xvideos.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..compat import compat_urllib_parse_unquote
7 from ..utils import (
8     clean_html,
9     ExtractorError,
10     determine_ext,
11     sanitized_Request,
12 )
13
14
15 class XVideosIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?xvideos\.com/video(?P<id>[0-9]+)(?:.*)'
17     _TEST = {
18         'url': 'http://www.xvideos.com/video4588838/biker_takes_his_girl',
19         'md5': '4b46ae6ea5e6e9086e714d883313c0c9',
20         'info_dict': {
21             'id': '4588838',
22             'ext': 'flv',
23             'title': 'Biker Takes his Girl',
24             'age_limit': 18,
25         }
26     }
27
28     _ANDROID_USER_AGENT = 'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19'
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32         webpage = self._download_webpage(url, video_id)
33
34         mobj = re.search(r'<h1 class="inlineError">(.+?)</h1>', webpage)
35         if mobj:
36             raise ExtractorError('%s said: %s' % (self.IE_NAME, clean_html(mobj.group(1))), expected=True)
37
38         video_url = compat_urllib_parse_unquote(
39             self._search_regex(r'flv_url=(.+?)&', webpage, 'video URL'))
40         video_title = self._html_search_regex(
41             r'<title>(.*?)\s+-\s+XVID', webpage, 'title')
42         video_thumbnail = self._search_regex(
43             r'url_bigthumb=(.+?)&amp', webpage, 'thumbnail', fatal=False)
44
45         formats = [{
46             'url': video_url,
47         }]
48
49         android_req = sanitized_Request(url)
50         android_req.add_header('User-Agent', self._ANDROID_USER_AGENT)
51         android_webpage = self._download_webpage(android_req, video_id, fatal=False)
52
53         if android_webpage is not None:
54             player_params_str = self._search_regex(
55                 'mobileReplacePlayerDivTwoQual\(([^)]+)\)',
56                 android_webpage, 'player parameters', default='')
57             player_params = list(map(lambda s: s.strip(' \''), player_params_str.split(',')))
58             if player_params:
59                 formats.extend([{
60                     'url': param,
61                     'preference': -10,
62                 } for param in player_params if determine_ext(param) == 'mp4'])
63
64         self._sort_formats(formats)
65
66         return {
67             'id': video_id,
68             'formats': formats,
69             'title': video_title,
70             'ext': 'flv',
71             'thumbnail': video_thumbnail,
72             'age_limit': 18,
73         }