[moviefap] Add new extractor
[youtube-dl] / youtube_dl / extractor / moviefap.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import str_to_int
7
8
9 class MovieFapIE(InfoExtractor):
10     _VALID_URL = r'https?://(?:www\.)?moviefap\.com/videos/(?P<id>[0-9a-f]+)/(?P<name>[a-z-_]+)'
11     _TESTS = [{
12         'url': 'http://www.moviefap.com/videos/e5da0d3edce5404418f5/jeune-couple-russe.html',
13         'md5': 'fa56683e291fc80635907168a743c9ad',
14         'info_dict': {
15             'id': 'e5da0d3edce5404418f5',
16             'ext': 'flv',
17             'title': 'Jeune Couple Russe',
18             'description': 'Amateur',
19             'thumbnail': 'http://pic.moviefap.com/thumbs/e5/949-18l.jpg',
20             'uploader_id': 'whiskeyjar',
21             'display_id': 'jeune-couple-russe'
22         }
23     }, {
24         'url': 'http://www.moviefap.com/videos/3080837f6712355015c2/busty-british-blonde-takes-backdoor-in-fake-taxi.html',
25         'md5': 'bedef72cb23d27a20755fc430a6d7a0e',
26         'info_dict': {
27             'id': '3080837f6712355015c2',
28             'ext': 'mp4',
29             'title': 'Busty British blonde takes backdoor in fake taxi',
30             'description': 'Big boobs British blonde flashing in fake taxi then giving titsjob and rimjob in the back seat before getting big cock up her tight ass',
31             'thumbnail': 'http://img.moviefap.com/a16:9w990r/thumbs/30/322021-18l.jpg',
32             'uploader_id': 'momcikoper',
33             'display_id': 'busty-british-blonde-takes-backdoor-in-fake-taxi'
34         }
35     }]
36
37     @staticmethod
38     def __get_thumbnail_data(xml):
39
40         """
41         Constructs a list of video thumbnails from timeline preview images.
42         :param xml: the information XML document to parse
43         """
44
45         timeline = xml.find('timeline')
46         if timeline is None:
47             # not all videos have the data - ah well
48             return []
49
50         # get the required information from the XML
51         attrs = {attr: str_to_int(timeline.find(attr).text)
52                  for attr in ['imageWidth', 'imageHeight', 'imageFirst', 'imageLast']}
53         pattern = timeline.find('imagePattern').text
54
55         # generate the list of thumbnail information dicts
56         thumbnails = []
57         for i in range(attrs['imageFirst'], attrs['imageLast'] + 1):
58             thumbnails.append({
59                 'url': pattern.replace('#', str(i)),
60                 'width': attrs['imageWidth'],
61                 'height': attrs['imageHeight']
62             })
63         return thumbnails
64
65     def _real_extract(self, url):
66
67         # find the video ID
68         video_id = self._match_id(url)
69
70         # retrieve the page HTML
71         webpage = self._download_webpage(url, video_id)
72
73         # find the URL of the XML document detailing video download URLs
74         info_url = self._html_search_regex(r'flashvars\.config = escape\("(.+?)"', webpage, 'player parameters')
75
76         # download that XML
77         xml = self._download_xml(info_url, video_id)
78
79         # create dictionary of properties we know so far, or can find easily
80         info = {
81             'id': video_id,
82             'title': self._html_search_regex(r'<div id="view_title"><h1>(.*?)</h1>', webpage, 'title'),
83             'display_id': re.compile(self._VALID_URL).match(url).group('name'),
84             'thumbnails': self.__get_thumbnail_data(xml),
85             'thumbnail': xml.find('startThumb').text,
86             'description': self._html_search_regex(r'name="description" value="(.*?)"', webpage, 'description'),
87             'uploader_id': self._html_search_regex(r'name="username" value="(.*?)"', webpage, 'uploader_id'),
88             'view_count': str_to_int(self._html_search_regex(r'<br>Views <strong>([0-9]+)</strong>', webpage, 'view_count')),
89             'average_rating': float(self._html_search_regex(r'Current Rating<br> <strong>(.*?)</strong>', webpage, 'average_rating')),
90             'comment_count': str_to_int(self._html_search_regex(r'<span id="comCount">([0-9]+)</span>', webpage, 'comment_count')),
91             'age_limit': 18,
92             'webpage_url': self._html_search_regex(r'name="link" value="(.*?)"', webpage, 'webpage_url'),
93             'categories': self._html_search_regex(r'</div>\s*(.*?)\s*<br>', webpage, 'categories').split(', ')
94         }
95
96         # find and add the format
97         if xml.find('videoConfig') is not None:
98             info['ext'] = xml.find('videoConfig').find('type').text
99         else:
100             info['ext'] = 'flv'  # guess...
101
102         # work out the video URL(s)
103         if xml.find('videoLink') is not None:
104             # single format available
105             info['url'] = xml.find('videoLink').text
106         else:
107             # multiple formats available
108             info['formats'] = []
109
110             # N.B. formats are already in ascending order of quality
111             for item in xml.find('quality').findall('item'):
112                 info['formats'].append({
113                     'url': item.find('videoLink').text,
114                     'resolution': item.find('res').text  # 480p etc.
115                 })
116
117         return info