[motherless] Detect non-existing videos
[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 ..utils import (
8     ExtractorError,
9     str_to_int,
10     unified_strdate,
11 )
12
13
14 class MotherlessIE(InfoExtractor):
15     _VALID_URL = r'http://(?:www\.)?motherless\.com/(?:g/[a-z0-9_]+/)?(?P<id>[A-Z0-9]+)'
16     _TESTS = [
17         {
18             'url': 'http://motherless.com/AC3FFE1',
19             'md5': '310f62e325a9fafe64f68c0bccb6e75f',
20             'info_dict': {
21                 'id': 'AC3FFE1',
22                 'ext': 'mp4',
23                 'title': 'Fucked in the ass while playing PS3',
24                 'categories': ['Gaming', 'anal', 'reluctant', 'rough', 'Wife'],
25                 'upload_date': '20100913',
26                 'uploader_id': 'famouslyfuckedup',
27                 'thumbnail': 're:http://.*\.jpg',
28                 'age_limit': 18,
29             }
30         },
31         {
32             'url': 'http://motherless.com/532291B',
33             'md5': 'bc59a6b47d1f958e61fbd38a4d31b131',
34             'info_dict': {
35                 'id': '532291B',
36                 'ext': 'mp4',
37                 'title': 'Amazing girl playing the omegle game, PERFECT!',
38                 'categories': ['Amateur', 'webcam', 'omegle', 'pink', 'young', 'masturbate', 'teen', 'game', 'hairy'],
39                 'upload_date': '20140622',
40                 'uploader_id': 'Sulivana7x',
41                 'thumbnail': 're:http://.*\.jpg',
42                 'age_limit': 18,
43             }
44         },
45         {
46             'url': 'http://motherless.com/g/cosplay/633979F',
47             'md5': '0b2a43f447a49c3e649c93ad1fafa4a0',
48             'info_dict': {
49                 'id': '633979F',
50                 'ext': 'mp4',
51                 'title': 'Turtlette',
52                 'categories': ['superheroine heroine  superher'],
53                 'upload_date': '20140827',
54                 'uploader_id': 'shade0230',
55                 'thumbnail': 're:http://.*\.jpg',
56                 'age_limit': 18,
57             }
58         },
59         {
60             # no keywords
61             'url': 'http://motherless.com/8B4BBC1',
62             'only_matching': True,
63         }
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         title = self._html_search_regex(
76             r'id="view-upload-title">\s+([^<]+)<', webpage, 'title')
77         video_url = self._html_search_regex(
78             r'setup\(\{\s+"file".+: "([^"]+)",', webpage, 'video URL')
79         age_limit = self._rta_search(webpage)
80         view_count = str_to_int(self._html_search_regex(
81             r'<strong>Views</strong>\s+([^<]+)<',
82             webpage, 'view count', fatal=False))
83         like_count = str_to_int(self._html_search_regex(
84             r'<strong>Favorited</strong>\s+([^<]+)<',
85             webpage, 'like count', fatal=False))
86
87         upload_date = self._html_search_regex(
88             r'<strong>Uploaded</strong>\s+([^<]+)<', webpage, 'upload date')
89         if 'Ago' in upload_date:
90             days = int(re.search(r'([0-9]+)', upload_date).group(1))
91             upload_date = (datetime.datetime.now() - datetime.timedelta(days=days)).strftime('%Y%m%d')
92         else:
93             upload_date = unified_strdate(upload_date)
94
95         comment_count = webpage.count('class="media-comment-contents"')
96         uploader_id = self._html_search_regex(
97             r'"thumb-member-username">\s+<a href="/m/([^"]+)"',
98             webpage, 'uploader_id')
99
100         categories = self._html_search_meta('keywords', webpage, default=None)
101         if categories:
102             categories = [cat.strip() for cat in categories.split(',')]
103
104         return {
105             'id': video_id,
106             'title': title,
107             'upload_date': upload_date,
108             'uploader_id': uploader_id,
109             'thumbnail': self._og_search_thumbnail(webpage),
110             'categories': categories,
111             'view_count': view_count,
112             'like_count': like_count,
113             'comment_count': comment_count,
114             'age_limit': age_limit,
115             'url': video_url,
116         }