[extremetube] Modernize
[youtube-dl] / youtube_dl / extractor / extremetube.py
1 from __future__ import unicode_literals
2
3 import os
4 import re
5
6 from .common import InfoExtractor
7 from ..utils import (
8     compat_urllib_parse_urlparse,
9     compat_urllib_request,
10     compat_urllib_parse,
11 )
12
13
14 class ExtremeTubeIE(InfoExtractor):
15     _VALID_URL = r'^(?:https?://)?(?:www\.)?(?P<url>extremetube\.com/.*?video/.+?(?P<videoid>[0-9]+))(?:[/?&]|$)'
16     _TESTS = [{
17         'url': 'http://www.extremetube.com/video/music-video-14-british-euro-brit-european-cumshots-swallow-652431',
18         'md5': '1fb9228f5e3332ec8c057d6ac36f33e0',
19         'info_dict': {
20             'id': '652431',
21             'ext': 'mp4',
22             'title': 'Music Video 14 british euro brit european cumshots swallow',
23             'uploader': 'unknown',
24             'age_limit': 18,
25         }
26     }, {
27         'url': 'http://www.extremetube.com/gay/video/abcde-1234',
28         'only_matching': True,
29     }]
30
31     def _real_extract(self, url):
32         mobj = re.match(self._VALID_URL, url)
33         video_id = mobj.group('videoid')
34         url = 'http://www.' + mobj.group('url')
35
36         req = compat_urllib_request.Request(url)
37         req.add_header('Cookie', 'age_verified=1')
38         webpage = self._download_webpage(req, video_id)
39
40         video_title = self._html_search_regex(
41             r'<h1 [^>]*?title="([^"]+)"[^>]*>\1<', webpage, 'title')
42         uploader = self._html_search_regex(
43             r'>Posted by:(?=<)(?:\s|<[^>]*>)*(.+?)\|', webpage, 'uploader',
44             fatal=False)
45         video_url = compat_urllib_parse.unquote(self._html_search_regex(
46             r'video_url=(.+?)&amp;', webpage, 'video_url'))
47         path = compat_urllib_parse_urlparse(video_url).path
48         format = path.split('/')[5].split('_')[:2]
49         format = "-".join(format)
50
51         return {
52             'id': video_id,
53             'title': video_title,
54             'uploader': uploader,
55             'url': video_url,
56             'format': format,
57             'format_id': format,
58             'age_limit': 18,
59         }