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