]> jfr.im git - yt-dlp.git/blame - youtube_dl/extractor/xhamster.py
Merge remote-tracking branch 'Tithen-Firion/master'
[yt-dlp.git] / youtube_dl / extractor / xhamster.py
CommitLineData
35409e11
PH
1from __future__ import unicode_literals
2
cb10cded
PH
3import re
4
5from .common import InfoExtractor
6from ..utils import (
cb10cded 7 ExtractorError,
ccb079ee
S
8 unified_strdate,
9 str_to_int,
10 int_or_none,
11 parse_duration,
cb10cded
PH
12)
13
14
15class XHamsterIE(InfoExtractor):
16 """Information Extractor for xHamster"""
5b9aefef 17 _VALID_URL = r'(?P<proto>https?)://(?:.+?\.)?xhamster\.com/movies/(?P<id>[0-9]+)/(?P<seo>.+?)\.html(?:\?.*)?'
ccb079ee
S
18 _TESTS = [
19 {
20 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
ccb079ee
S
21 'info_dict': {
22 'id': '1509445',
23 'ext': 'mp4',
24 'title': 'FemaleAgent Shy beauty takes the bait',
25 'upload_date': '20121014',
26 'uploader_id': 'Ruseful2011',
27 'duration': 893,
28 'age_limit': 18,
29 }
30 },
31 {
32 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
ccb079ee
S
33 'info_dict': {
34 'id': '2221348',
35 'ext': 'mp4',
36 'title': 'Britney Spears Sexy Booty',
37 'upload_date': '20130914',
38 'uploader_id': 'jojo747400',
39 'duration': 200,
40 'age_limit': 18,
41 }
5b9aefef 42 },
43 {
44 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
d34f9828 45 'only_matching': True,
5b9aefef 46 },
ccb079ee 47 ]
cb10cded 48
5f6a1245 49 def _real_extract(self, url):
5d0c9754 50 def extract_video_url(webpage):
ccb079ee 51 mp4 = re.search(r'<video\s+.*?file="([^"]+)".*?>', webpage)
65d78112 52 if mp4 is None:
ccb079ee 53 raise ExtractorError('Unable to extract media URL')
65d78112
MC
54 else:
55 return mp4.group(1)
56
5d0c9754 57 def is_hd(webpage):
22ff1c4a 58 return '<div class=\'icon iconHD\'' in webpage
5d0c9754 59
cb10cded
PH
60 mobj = re.match(self._VALID_URL, url)
61
62 video_id = mobj.group('id')
1237c9a3 63 seo = mobj.group('seo')
5b9aefef 64 proto = mobj.group('proto')
65 mrss_url = '%s://xhamster.com/movies/%s/%s.html' % (proto, video_id, seo)
cb10cded
PH
66 webpage = self._download_webpage(mrss_url, video_id)
67
ccb079ee 68 title = self._html_search_regex(r'<title>(?P<title>.+?) - xHamster\.com</title>', webpage, 'title')
cb10cded 69
4353cf51 70 # Only a few videos have an description
22ff1c4a 71 mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
ccb079ee 72 description = mobj.group(1) if mobj else None
cb10cded 73
ccb079ee 74 upload_date = self._html_search_regex(r'hint=\'(\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}\'',
9e1a5b84 75 webpage, 'upload date', fatal=False)
ccb079ee
S
76 if upload_date:
77 upload_date = unified_strdate(upload_date)
cb10cded 78
ccb079ee 79 uploader_id = self._html_search_regex(r'<a href=\'/user/[^>]+>(?P<uploader_id>[^<]+)',
9e1a5b84 80 webpage, 'uploader id', default='anonymous')
cb10cded 81
ccb079ee
S
82 thumbnail = self._html_search_regex(r'<video\s+.*?poster="([^"]+)".*?>', webpage, 'thumbnail', fatal=False)
83
84 duration = parse_duration(self._html_search_regex(r'<span>Runtime:</span> (\d+:\d+)</div>',
9e1a5b84 85 webpage, 'duration', fatal=False))
ccb079ee
S
86
87 view_count = self._html_search_regex(r'<span>Views:</span> ([^<]+)</div>', webpage, 'view count', fatal=False)
88 if view_count:
89 view_count = str_to_int(view_count)
90
91 mobj = re.search(r"hint='(?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes'", webpage)
92 (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
93
94 mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
95 comment_count = mobj.group('commentcount') if mobj else 0
cb10cded 96
9d92015d
PH
97 age_limit = self._rta_search(webpage)
98
5d0c9754 99 hd = is_hd(webpage)
ccb079ee 100
65d78112 101 video_url = extract_video_url(webpage)
5d0c9754 102 formats = [{
103 'url': video_url,
5d0c9754 104 'format_id': 'hd' if hd else 'sd',
ccb079ee 105 'preference': 1,
5d0c9754 106 }]
65d78112 107
5d0c9754 108 if not hd:
b8e1471d 109 mrss_url = self._search_regex(r'<link rel="canonical" href="([^"]+)', webpage, 'mrss_url')
ccb079ee 110 webpage = self._download_webpage(mrss_url + '?hd', video_id, note='Downloading HD webpage')
5d0c9754 111 if is_hd(webpage):
112 video_url = extract_video_url(webpage)
113 formats.append({
114 'url': video_url,
5d0c9754 115 'format_id': 'hd',
22ff1c4a 116 'preference': 2,
5d0c9754 117 })
118
22ff1c4a
PH
119 self._sort_formats(formats)
120
5d0c9754 121 return {
122 'id': video_id,
ccb079ee
S
123 'title': title,
124 'description': description,
125 'upload_date': upload_date,
126 'uploader_id': uploader_id,
127 'thumbnail': thumbnail,
128 'duration': duration,
129 'view_count': view_count,
130 'like_count': int_or_none(like_count),
131 'dislike_count': int_or_none(dislike_count),
132 'comment_count': int_or_none(comment_count),
9d92015d 133 'age_limit': age_limit,
ccb079ee 134 'formats': formats,
5d0c9754 135 }