[pornflip] Add extractor (closes #11556)
[youtube-dl] / youtube_dl / extractor / pornflip.py
1 # coding: utf-8
2 from __future__ import unicode_literals
3
4 from .common import InfoExtractor
5 from ..compat import (
6     compat_parse_qs,
7 )
8 from ..utils import (
9     int_or_none,
10     try_get,
11     RegexNotFoundError,
12 )
13
14
15 class PornFlipIE(InfoExtractor):
16     _VALID_URL = r'https?://(?:www\.)?pornflip\.com/v/(?P<id>[0-9A-Za-z]{11})'
17     _TEST = {
18         'url': 'https://www.pornflip.com/v/wz7DfNhMmep',
19         'md5': '98c46639849145ae1fd77af532a9278c',
20         'info_dict': {
21             'id': 'wz7DfNhMmep',
22             'ext': 'mp4',
23             'title': '2 Amateurs swallow make his dream cumshots true',
24             'uploader': 'figifoto',
25             'thumbnail': r're:^https?://.*\.jpg$',
26             'age_limit': 18,
27         }
28     }
29
30     def _real_extract(self, url):
31         video_id = self._match_id(url)
32         webpage = self._download_webpage(url, video_id)
33         uploader = self._html_search_regex(
34             r'<span class="name">\s+<a class="ajax" href=".+>\s+<strong>([^<]+)<', webpage, 'uploader', fatal=False)
35         flashvars = compat_parse_qs(self._html_search_regex(
36             r'<embed.+?flashvars="([^"]+)"',
37             webpage, 'flashvars'))
38         title = flashvars['video_vars[title]'][0]
39         thumbnail = try_get(flashvars, lambda x: x['video_vars[big_thumb]'][0])
40         formats = []
41         for k, v in flashvars.items():
42             height = self._search_regex(r'video_vars\[video_urls\]\[(\d+).+?\]', k, 'height', default=None)
43             if height:
44                 url = v[0]
45                 formats.append({
46                     'height': int_or_none(height),
47                     'url': url
48                 })
49
50         self._sort_formats(formats)
51
52         return {
53             'id': video_id,
54             'formats': formats,
55             'title': title,
56             'uploader': uploader,
57             'thumbnail': thumbnail,
58             'age_limit': 18,
59         }