[youtube] fix hd720 format position
[youtube-dl] / youtube_dl / extractor / motherless.py
1 from __future__ import unicode_literals
2
3 import datetime
4 import re
5
6 from .common import InfoExtractor
7 from ..compat import compat_urlparse
8 from ..utils import (
9     ExtractorError,
10     InAdvancePagedList,
11     orderedSet,
12     str_to_int,
13     unified_strdate,
14 )
15
16
17 class MotherlessIE(InfoExtractor):
18     _VALID_URL = r'https?://(?:www\.)?motherless\.com/(?:g/[a-z0-9_]+/)?(?P<id>[A-Z0-9]+)'
19     _TESTS = [{
20         'url': 'http://motherless.com/AC3FFE1',
21         'md5': '310f62e325a9fafe64f68c0bccb6e75f',
22         'info_dict': {
23             'id': 'AC3FFE1',
24             'ext': 'mp4',
25             'title': 'Fucked in the ass while playing PS3',
26             'categories': ['Gaming', 'anal', 'reluctant', 'rough', 'Wife'],
27             'upload_date': '20100913',
28             'uploader_id': 'famouslyfuckedup',
29             'thumbnail': r're:http://.*\.jpg',
30             'age_limit': 18,
31         }
32     }, {
33         'url': 'http://motherless.com/532291B',
34         'md5': 'bc59a6b47d1f958e61fbd38a4d31b131',
35         'info_dict': {
36             'id': '532291B',
37             'ext': 'mp4',
38             'title': 'Amazing girl playing the omegle game, PERFECT!',
39             'categories': ['Amateur', 'webcam', 'omegle', 'pink', 'young', 'masturbate', 'teen',
40                            'game', 'hairy'],
41             'upload_date': '20140622',
42             'uploader_id': 'Sulivana7x',
43             'thumbnail': r're:http://.*\.jpg',
44             'age_limit': 18,
45         },
46         'skip': '404',
47     }, {
48         'url': 'http://motherless.com/g/cosplay/633979F',
49         'md5': '0b2a43f447a49c3e649c93ad1fafa4a0',
50         'info_dict': {
51             'id': '633979F',
52             'ext': 'mp4',
53             'title': 'Turtlette',
54             'categories': ['superheroine heroine  superher'],
55             'upload_date': '20140827',
56             'uploader_id': 'shade0230',
57             'thumbnail': r're:http://.*\.jpg',
58             'age_limit': 18,
59         }
60     }, {
61         # no keywords
62         'url': 'http://motherless.com/8B4BBC1',
63         'only_matching': True,
64     }]
65
66     def _real_extract(self, url):
67         video_id = self._match_id(url)
68         webpage = self._download_webpage(url, video_id)
69
70         if any(p in webpage for p in (
71                 '<title>404 - MOTHERLESS.COM<',
72                 ">The page you're looking for cannot be found.<")):
73             raise ExtractorError('Video %s does not exist' % video_id, expected=True)
74
75         if '>The content you are trying to view is for friends only.' in webpage:
76             raise ExtractorError('Video %s is for friends only' % video_id, expected=True)
77
78         title = self._html_search_regex(
79             r'id="view-upload-title">\s+([^<]+)<', webpage, 'title')
80         video_url = self._html_search_regex(
81             r'setup\(\{\s+"file".+: "([^"]+)",', webpage, 'video URL')
82         age_limit = self._rta_search(webpage)
83         view_count = str_to_int(self._html_search_regex(
84             r'<strong>Views</strong>\s+([^<]+)<',
85             webpage, 'view count', fatal=False))
86         like_count = str_to_int(self._html_search_regex(
87             r'<strong>Favorited</strong>\s+([^<]+)<',
88             webpage, 'like count', fatal=False))
89
90         upload_date = self._html_search_regex(
91             r'<strong>Uploaded</strong>\s+([^<]+)<', webpage, 'upload date')
92         if 'Ago' in upload_date:
93             days = int(re.search(r'([0-9]+)', upload_date).group(1))
94             upload_date = (datetime.datetime.now() - datetime.timedelta(days=days)).strftime('%Y%m%d')
95         else:
96             upload_date = unified_strdate(upload_date)
97
98         comment_count = webpage.count('class="media-comment-contents"')
99         uploader_id = self._html_search_regex(
100             r'"thumb-member-username">\s+<a href="/m/([^"]+)"',
101             webpage, 'uploader_id')
102
103         categories = self._html_search_meta('keywords', webpage, default=None)
104         if categories:
105             categories = [cat.strip() for cat in categories.split(',')]
106
107         return {
108             'id': video_id,
109             'title': title,
110             'upload_date': upload_date,
111             'uploader_id': uploader_id,
112             'thumbnail': self._og_search_thumbnail(webpage),
113             'categories': categories,
114             'view_count': view_count,
115             'like_count': like_count,
116             'comment_count': comment_count,
117             'age_limit': age_limit,
118             'url': video_url,
119         }
120
121
122 class MotherlessGroupIE(InfoExtractor):
123     _VALID_URL = 'https?://(?:www\.)?motherless\.com/gv?/(?P<id>[a-z0-9_]+)'
124     _TESTS = [{
125         'url': 'http://motherless.com/g/movie_scenes',
126         'info_dict': {
127             'id': 'movie_scenes',
128             'title': 'Movie Scenes',
129             'description': 'Hot and sexy scenes from "regular" movies... '
130                            'Beautiful actresses fully nude... A looot of '
131                            'skin! :)Enjoy!',
132         },
133         'playlist_mincount': 662,
134     }, {
135         'url': 'http://motherless.com/gv/sex_must_be_funny',
136         'info_dict': {
137             'id': 'sex_must_be_funny',
138             'title': 'Sex must be funny',
139             'description': 'Sex can be funny. Wide smiles,laugh, games, fun of '
140                            'any kind!'
141         },
142         'playlist_mincount': 9,
143     }]
144
145     @classmethod
146     def suitable(cls, url):
147         return (False if MotherlessIE.suitable(url)
148                 else super(MotherlessGroupIE, cls).suitable(url))
149
150     def _extract_entries(self, webpage, base):
151         entries = []
152         for mobj in re.finditer(
153                 r'href="(?P<href>/[^"]+)"[^>]*>(?:\s*<img[^>]+alt="[^-]+-\s(?P<title>[^"]+)")?',
154                 webpage):
155             video_url = compat_urlparse.urljoin(base, mobj.group('href'))
156             if not MotherlessIE.suitable(video_url):
157                 continue
158             video_id = MotherlessIE._match_id(video_url)
159             title = mobj.group('title')
160             entries.append(self.url_result(
161                 video_url, ie=MotherlessIE.ie_key(), video_id=video_id,
162                 video_title=title))
163         # Alternative fallback
164         if not entries:
165             entries = [
166                 self.url_result(
167                     compat_urlparse.urljoin(base, '/' + video_id),
168                     ie=MotherlessIE.ie_key(), video_id=video_id)
169                 for video_id in orderedSet(re.findall(
170                     r'data-codename=["\']([A-Z0-9]+)', webpage))]
171         return entries
172
173     def _real_extract(self, url):
174         group_id = self._match_id(url)
175         page_url = compat_urlparse.urljoin(url, '/gv/%s' % group_id)
176         webpage = self._download_webpage(page_url, group_id)
177         title = self._search_regex(
178             r'<title>([\w\s]+\w)\s+-', webpage, 'title', fatal=False)
179         description = self._html_search_meta(
180             'description', webpage, fatal=False)
181         page_count = self._int(self._search_regex(
182             r'(\d+)</(?:a|span)><(?:a|span)[^>]+>\s*NEXT',
183             webpage, 'page_count'), 'page_count')
184         PAGE_SIZE = 80
185
186         def _get_page(idx):
187             webpage = self._download_webpage(
188                 page_url, group_id, query={'page': idx + 1},
189                 note='Downloading page %d/%d' % (idx + 1, page_count)
190             )
191             for entry in self._extract_entries(webpage, url):
192                 yield entry
193
194         playlist = InAdvancePagedList(_get_page, page_count, PAGE_SIZE)
195
196         return {
197             '_type': 'playlist',
198             'id': group_id,
199             'title': title,
200             'description': description,
201             'entries': playlist
202         }