]> gitweb @ CieloNegro.org - youtube-dl.git/blob - youtube_dl/extractor/moviefap.py
880ea224764d93fa98c5de3a3e5196115f372b6b
[youtube-dl.git] / 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/be9867c9416c19f54a4a/experienced-milf-amazing-handjob.html',
25         'md5': '26624b4e2523051b550067d547615906',
26         'info_dict': {
27             'id': 'be9867c9416c19f54a4a',
28             'ext': 'mp4',
29             'title': 'Experienced MILF Amazing Handjob',
30             'description': 'Experienced MILF giving an Amazing Handjob',
31             'thumbnail': 'http://img.moviefap.com/a16:9w990r/thumbs/be/322032-20l.jpg',
32             'uploader_id': 'darvinfred06',
33             'display_id': 'experienced-milf-amazing-handjob'
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         width = str_to_int(timeline.find('imageWidth').text)
52         height = str_to_int(timeline.find('imageHeight').text)
53         first = str_to_int(timeline.find('imageFirst').text)
54         last = str_to_int(timeline.find('imageLast').text)
55         pattern = timeline.find('imagePattern').text
56
57         # generate the list of thumbnail information dicts
58         thumbnails = []
59         for i in range(first, last + 1):
60             thumbnails.append({
61                 'url': pattern.replace('#', str(i)),
62                 'width': width,
63                 'height': height
64             })
65         return thumbnails
66
67     def _real_extract(self, url):
68
69         # find the video ID
70         video_id = self._match_id(url)
71
72         # retrieve the page HTML
73         webpage = self._download_webpage(url, video_id)
74
75         # find the URL of the XML document detailing video download URLs
76         info_url = self._html_search_regex(r'flashvars\.config = escape\("(.+?)"', webpage, 'player parameters')
77
78         # download that XML
79         xml = self._download_xml(info_url, video_id)
80
81         # create dictionary of properties we know so far, or can find easily
82         info = {
83             'id': video_id,
84             'title': self._html_search_regex(r'<div id="view_title"><h1>(.*?)</h1>', webpage, 'title'),
85             'display_id': re.compile(self._VALID_URL).match(url).group('name'),
86             'thumbnails': self.__get_thumbnail_data(xml),
87             'thumbnail': xml.find('startThumb').text,
88             'description': self._html_search_regex(r'name="description" value="(.*?)"', webpage, 'description'),
89             'uploader_id': self._html_search_regex(r'name="username" value="(.*?)"', webpage, 'uploader_id'),
90             'view_count': str_to_int(self._html_search_regex(r'<br>Views <strong>([0-9]+)</strong>', webpage, 'view_count')),
91             'average_rating': float(self._html_search_regex(r'Current Rating<br> <strong>(.*?)</strong>', webpage, 'average_rating')),
92             'comment_count': str_to_int(self._html_search_regex(r'<span id="comCount">([0-9]+)</span>', webpage, 'comment_count')),
93             'age_limit': 18,
94             'webpage_url': self._html_search_regex(r'name="link" value="(.*?)"', webpage, 'webpage_url'),
95             'categories': self._html_search_regex(r'</div>\s*(.*?)\s*<br>', webpage, 'categories').split(', ')
96         }
97
98         # find and add the format
99         if xml.find('videoConfig') is not None:
100             info['ext'] = xml.find('videoConfig').find('type').text
101         else:
102             info['ext'] = 'flv'  # guess...
103
104         # work out the video URL(s)
105         if xml.find('videoLink') is not None:
106             # single format available
107             info['url'] = xml.find('videoLink').text
108         else:
109             # multiple formats available
110             info['formats'] = []
111
112             # N.B. formats are already in ascending order of quality
113             for item in xml.find('quality').findall('item'):
114                 info['formats'].append({
115                     'url': item.find('videoLink').text,
116                     'resolution': item.find('res').text  # 480p etc.
117                 })
118
119         return info