[moviefap] Move flv videos to formats in the metadata
[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( \
82                 r'flashvars\.config = escape\("(.+?)"', webpage, 'player parameters')
83         xml = self._download_xml(info_url, video_id)
84
85         # find the video container
86         if xml.find('videoConfig') is not None:
87             ext = xml.find('videoConfig').find('type').text
88         else:
89             ext = 'flv'  # guess...
90
91         # work out the video URL(s)
92         formats = []
93         if xml.find('videoLink') is not None:
94             # single format available
95             formats.append({
96                 'url': xpath_text(xml, 'videoLink', 'url', True),
97                 'ext': ext
98             })
99         else:
100             # multiple formats available
101             for item in xml.find('quality').findall('item'):
102                 resolution = xpath_text(item, 'res', 'resolution', True)  # 480p etc.
103                 formats.append({
104                     'url': xpath_text(item, 'videoLink', 'url', True),
105                     'ext': ext,
106                     'resolution': resolution,
107                     'height': int(re.findall(r'\d+', resolution)[0])
108                 })
109
110             self._sort_formats(formats)
111
112         return {
113             'id': video_id,
114             'formats': formats,
115             'title': self._html_search_regex( \
116                     r'<div id="view_title"><h1>(.*?)</h1>', webpage, 'title'),
117             'display_id': re.compile(self._VALID_URL).match(url).group('name'),
118             'thumbnails': self.__get_thumbnail_data(xml),
119             'thumbnail': xpath_text(xml, 'startThumb', 'thumbnail'),
120             'description': self._html_search_regex( \
121                     r'name="description" value="(.*?)"', webpage, 'description', fatal=False),
122             'uploader_id': self._html_search_regex( \
123                     r'name="username" value="(.*?)"', webpage, 'uploader_id', fatal=False),
124             'view_count': str_to_int(self._html_search_regex( \
125                     r'<br>Views <strong>([0-9]+)</strong>', webpage, 'view_count, fatal=False')),
126             'average_rating': float(self._html_search_regex( \
127                     r'Current Rating<br> <strong>(.*?)</strong>', webpage, 'average_rating', fatal=False)),
128             'comment_count': str_to_int(self._html_search_regex( \
129                     r'<span id="comCount">([0-9]+)</span>', webpage, 'comment_count', fatal=False)),
130             'age_limit': 18,
131             'webpage_url': self._html_search_regex( \
132                     r'name="link" value="(.*?)"', webpage, 'webpage_url', fatal=False),
133             'categories': self._html_search_regex( \
134                     r'</div>\s*(.*?)\s*<br>', webpage, 'categories', fatal=False).split(', ')
135         }