6da93dbc9b71bc5dbd4233dcc8344f7a11e27523
[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 (
7     xpath_text,
8     str_to_int
9 )
10
11
12 class MovieFapIE(InfoExtractor):
13     _VALID_URL = r'https?://(?:www\.)?moviefap\.com/videos/(?P<id>[0-9a-f]+)/(?P<name>[a-z-_]+)'
14     _TESTS = [{
15         # normal, multi-format video
16         'url': 'http://www.moviefap.com/videos/be9867c9416c19f54a4a/experienced-milf-amazing-handjob.html',
17         'md5': '26624b4e2523051b550067d547615906',
18         'info_dict': {
19             'id': 'be9867c9416c19f54a4a',
20             'ext': 'mp4',
21             'title': 'Experienced MILF Amazing Handjob',
22             'description': 'Experienced MILF giving an Amazing Handjob',
23             'thumbnail': 'http://img.moviefap.com/a16:9w990r/thumbs/be/322032-20l.jpg',
24             'uploader_id': 'darvinfred06',
25             'display_id': 'experienced-milf-amazing-handjob',
26             'categories': ['Amateur', 'Masturbation', 'Mature', 'Flashing']
27         }
28     }, {
29         # quirky single-format case where the extension is given as fid, but the video is really an flv
30         'url': 'http://www.moviefap.com/videos/e5da0d3edce5404418f5/jeune-couple-russe.html',
31         'md5': 'fa56683e291fc80635907168a743c9ad',
32         'info_dict': {
33             'id': 'e5da0d3edce5404418f5',
34             'ext': 'flv',
35             'title': 'Jeune Couple Russe',
36             'description': 'Amateur',
37             'thumbnail': 'http://pic.moviefap.com/thumbs/e5/949-18l.jpg',
38             'uploader_id': 'whiskeyjar',
39             'display_id': 'jeune-couple-russe',
40             'categories': ['Amateur', 'Teen']
41         }
42     }]
43
44     @staticmethod
45     def __get_thumbnail_data(xml):
46
47         """
48         Constructs a list of video thumbnails from timeline preview images.
49         :param xml: the information XML document to parse
50         """
51
52         timeline = xml.find('timeline')
53         if timeline is None:
54             # not all videos have the data - ah well
55             return []
56
57         # get the required information from the XML
58         width = str_to_int(timeline.find('imageWidth').text)
59         height = str_to_int(timeline.find('imageHeight').text)
60         first = str_to_int(timeline.find('imageFirst').text)
61         last = str_to_int(timeline.find('imageLast').text)
62         pattern = timeline.find('imagePattern').text
63
64         # generate the list of thumbnail information dicts
65         thumbnails = []
66         for i in range(first, last + 1):
67             thumbnails.append({
68                 'url': pattern.replace('#', str(i)),
69                 'width': width,
70                 'height': height
71             })
72         return thumbnails
73
74     def _real_extract(self, url):
75
76         video_id = self._match_id(url)
77         webpage = self._download_webpage(url, video_id)
78
79         # find and retrieve the XML document detailing video download URLs
80         info_url = self._html_search_regex(r'flashvars\.config = escape\("(.+?)"', webpage, 'player parameters')
81         xml = self._download_xml(info_url, video_id)
82
83         info = {
84             'id': video_id,
85             'title': self._html_search_regex(r'<div id="view_title"><h1>(.*?)</h1>', webpage, 'title'),
86             'display_id': re.compile(self._VALID_URL).match(url).group('name'),
87             'thumbnails': self.__get_thumbnail_data(xml),
88             'thumbnail': xpath_text(xml, 'startThumb', 'thumbnail'),
89             'description': self._html_search_regex(r'name="description" value="(.*?)"', webpage, 'description', fatal=False),
90             'uploader_id': self._html_search_regex(r'name="username" value="(.*?)"', webpage, 'uploader_id', fatal=False),
91             'view_count': str_to_int(self._html_search_regex(r'<br>Views <strong>([0-9]+)</strong>', webpage, 'view_count, fatal=False')),
92             'average_rating': float(self._html_search_regex(r'Current Rating<br> <strong>(.*?)</strong>', webpage, 'average_rating', fatal=False)),
93             'comment_count': str_to_int(self._html_search_regex(r'<span id="comCount">([0-9]+)</span>', webpage, 'comment_count', fatal=False)),
94             'age_limit': 18,
95             'webpage_url': self._html_search_regex(r'name="link" value="(.*?)"', webpage, 'webpage_url', fatal=False),
96             'categories': self._html_search_regex(r'</div>\s*(.*?)\s*<br>', webpage, 'categories', fatal=False).split(', ')
97         }
98
99         # find and add the format
100         if xml.find('videoConfig') is not None:
101             info['ext'] = xml.find('videoConfig').find('type').text
102         else:
103             info['ext'] = 'flv'  # guess...
104
105         # work out the video URL(s)
106         if xml.find('videoLink') is not None:
107             # single format available
108             info['url'] = xpath_text(xml, 'videoLink', 'url', True)
109         else:
110             # multiple formats available
111             info['formats'] = []
112
113             # N.B. formats are already in ascending order of quality
114             for item in xml.find('quality').findall('item'):
115                 info['formats'].append({
116                     'url': xpath_text(item, 'videoLink', 'url', True),
117                     'resolution': xpath_text(item, 'res', 'resolution', True)  # 480p etc.
118                 })
119
120         return info