You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

324 lines
12 KiB

10 years ago
  1. from __future__ import unicode_literals
  2. import re
  3. from .common import InfoExtractor
  4. from ..compat import compat_str
  5. from ..utils import (
  6. clean_html,
  7. determine_ext,
  8. dict_get,
  9. ExtractorError,
  10. int_or_none,
  11. parse_duration,
  12. try_get,
  13. unified_strdate,
  14. url_or_none,
  15. )
  16. class XHamsterIE(InfoExtractor):
  17. _VALID_URL = r'''(?x)
  18. https?://
  19. (?:.+?\.)?xhamster\.(?:com|one)/
  20. (?:
  21. movies/(?P<id>\d+)/(?P<display_id>[^/]*)\.html|
  22. videos/(?P<display_id_2>[^/]*)-(?P<id_2>\d+)
  23. )
  24. '''
  25. _TESTS = [{
  26. 'url': 'http://xhamster.com/movies/1509445/femaleagent_shy_beauty_takes_the_bait.html',
  27. 'md5': '8281348b8d3c53d39fffb377d24eac4e',
  28. 'info_dict': {
  29. 'id': '1509445',
  30. 'display_id': 'femaleagent_shy_beauty_takes_the_bait',
  31. 'ext': 'mp4',
  32. 'title': 'FemaleAgent Shy beauty takes the bait',
  33. 'timestamp': 1350194821,
  34. 'upload_date': '20121014',
  35. 'uploader': 'Ruseful2011',
  36. 'duration': 893,
  37. 'age_limit': 18,
  38. 'categories': ['Fake Hub', 'Amateur', 'MILFs', 'POV', 'Beauti', 'Beauties', 'Beautiful', 'Boss', 'Office', 'Oral', 'Reality', 'Sexy', 'Taking'],
  39. },
  40. }, {
  41. 'url': 'http://xhamster.com/movies/2221348/britney_spears_sexy_booty.html?hd',
  42. 'info_dict': {
  43. 'id': '2221348',
  44. 'display_id': 'britney_spears_sexy_booty',
  45. 'ext': 'mp4',
  46. 'title': 'Britney Spears Sexy Booty',
  47. 'timestamp': 1379123460,
  48. 'upload_date': '20130914',
  49. 'uploader': 'jojo747400',
  50. 'duration': 200,
  51. 'age_limit': 18,
  52. 'categories': ['Britney Spears', 'Celebrities', 'HD Videos', 'Sexy', 'Sexy Booty'],
  53. },
  54. 'params': {
  55. 'skip_download': True,
  56. },
  57. }, {
  58. # empty seo
  59. 'url': 'http://xhamster.com/movies/5667973/.html',
  60. 'info_dict': {
  61. 'id': '5667973',
  62. 'ext': 'mp4',
  63. 'title': '....',
  64. 'timestamp': 1454948101,
  65. 'upload_date': '20160208',
  66. 'uploader': 'parejafree',
  67. 'duration': 72,
  68. 'age_limit': 18,
  69. 'categories': ['Amateur', 'Blowjobs'],
  70. },
  71. 'params': {
  72. 'skip_download': True,
  73. },
  74. }, {
  75. # mobile site
  76. 'url': 'https://m.xhamster.com/videos/cute-teen-jacqueline-solo-masturbation-8559111',
  77. 'only_matching': True,
  78. }, {
  79. 'url': 'https://xhamster.com/movies/2272726/amber_slayed_by_the_knight.html',
  80. 'only_matching': True,
  81. }, {
  82. # This video is visible for marcoalfa123456's friends only
  83. 'url': 'https://it.xhamster.com/movies/7263980/la_mia_vicina.html',
  84. 'only_matching': True,
  85. }, {
  86. # new URL schema
  87. 'url': 'https://pt.xhamster.com/videos/euro-pedal-pumping-7937821',
  88. 'only_matching': True,
  89. }, {
  90. 'url': 'https://xhamster.one/videos/femaleagent-shy-beauty-takes-the-bait-1509445',
  91. 'only_matching': True,
  92. }]
  93. def _real_extract(self, url):
  94. mobj = re.match(self._VALID_URL, url)
  95. video_id = mobj.group('id') or mobj.group('id_2')
  96. display_id = mobj.group('display_id') or mobj.group('display_id_2')
  97. desktop_url = re.sub(r'^(https?://(?:.+?\.)?)m\.', r'\1', url)
  98. webpage = self._download_webpage(desktop_url, video_id)
  99. error = self._html_search_regex(
  100. r'<div[^>]+id=["\']videoClosed["\'][^>]*>(.+?)</div>',
  101. webpage, 'error', default=None)
  102. if error:
  103. raise ExtractorError(error, expected=True)
  104. age_limit = self._rta_search(webpage)
  105. def get_height(s):
  106. return int_or_none(self._search_regex(
  107. r'^(\d+)[pP]', s, 'height', default=None))
  108. initials = self._parse_json(
  109. self._search_regex(
  110. r'window\.initials\s*=\s*({.+?})\s*;\s*\n', webpage, 'initials',
  111. default='{}'),
  112. video_id, fatal=False)
  113. if initials:
  114. video = initials['videoModel']
  115. title = video['title']
  116. formats = []
  117. for format_id, formats_dict in video['sources'].items():
  118. if not isinstance(formats_dict, dict):
  119. continue
  120. for quality, format_item in formats_dict.items():
  121. if format_id == 'download':
  122. # Download link takes some time to be generated,
  123. # skipping for now
  124. continue
  125. if not isinstance(format_item, dict):
  126. continue
  127. format_url = format_item.get('link')
  128. filesize = int_or_none(
  129. format_item.get('size'), invscale=1000000)
  130. else:
  131. format_url = format_item
  132. filesize = None
  133. format_url = url_or_none(format_url)
  134. if not format_url:
  135. continue
  136. formats.append({
  137. 'format_id': '%s-%s' % (format_id, quality),
  138. 'url': format_url,
  139. 'ext': determine_ext(format_url, 'mp4'),
  140. 'height': get_height(quality),
  141. 'filesize': filesize,
  142. })
  143. self._sort_formats(formats)
  144. categories_list = video.get('categories')
  145. if isinstance(categories_list, list):
  146. categories = []
  147. for c in categories_list:
  148. if not isinstance(c, dict):
  149. continue
  150. c_name = c.get('name')
  151. if isinstance(c_name, compat_str):
  152. categories.append(c_name)
  153. else:
  154. categories = None
  155. return {
  156. 'id': video_id,
  157. 'display_id': display_id,
  158. 'title': title,
  159. 'description': video.get('description'),
  160. 'timestamp': int_or_none(video.get('created')),
  161. 'uploader': try_get(
  162. video, lambda x: x['author']['name'], compat_str),
  163. 'thumbnail': video.get('thumbURL'),
  164. 'duration': int_or_none(video.get('duration')),
  165. 'view_count': int_or_none(video.get('views')),
  166. 'like_count': int_or_none(try_get(
  167. video, lambda x: x['rating']['likes'], int)),
  168. 'dislike_count': int_or_none(try_get(
  169. video, lambda x: x['rating']['dislikes'], int)),
  170. 'comment_count': int_or_none(video.get('views')),
  171. 'age_limit': age_limit,
  172. 'categories': categories,
  173. 'formats': formats,
  174. }
  175. # Old layout fallback
  176. title = self._html_search_regex(
  177. [r'<h1[^>]*>([^<]+)</h1>',
  178. r'<meta[^>]+itemprop=".*?caption.*?"[^>]+content="(.+?)"',
  179. r'<title[^>]*>(.+?)(?:,\s*[^,]*?\s*Porn\s*[^,]*?:\s*xHamster[^<]*| - xHamster\.com)</title>'],
  180. webpage, 'title')
  181. formats = []
  182. format_urls = set()
  183. sources = self._parse_json(
  184. self._search_regex(
  185. r'sources\s*:\s*({.+?})\s*,?\s*\n', webpage, 'sources',
  186. default='{}'),
  187. video_id, fatal=False)
  188. for format_id, format_url in sources.items():
  189. format_url = url_or_none(format_url)
  190. if not format_url:
  191. continue
  192. if format_url in format_urls:
  193. continue
  194. format_urls.add(format_url)
  195. formats.append({
  196. 'format_id': format_id,
  197. 'url': format_url,
  198. 'height': get_height(format_id),
  199. })
  200. video_url = self._search_regex(
  201. [r'''file\s*:\s*(?P<q>["'])(?P<mp4>.+?)(?P=q)''',
  202. r'''<a\s+href=(?P<q>["'])(?P<mp4>.+?)(?P=q)\s+class=["']mp4Thumb''',
  203. r'''<video[^>]+file=(?P<q>["'])(?P<mp4>.+?)(?P=q)[^>]*>'''],
  204. webpage, 'video url', group='mp4', default=None)
  205. if video_url and video_url not in format_urls:
  206. formats.append({
  207. 'url': video_url,
  208. })
  209. self._sort_formats(formats)
  210. # Only a few videos have an description
  211. mobj = re.search(r'<span>Description: </span>([^<]+)', webpage)
  212. description = mobj.group(1) if mobj else None
  213. upload_date = unified_strdate(self._search_regex(
  214. r'hint=["\'](\d{4}-\d{2}-\d{2}) \d{2}:\d{2}:\d{2} [A-Z]{3,4}',
  215. webpage, 'upload date', fatal=False))
  216. uploader = self._html_search_regex(
  217. r'<span[^>]+itemprop=["\']author[^>]+><a[^>]+><span[^>]+>([^<]+)',
  218. webpage, 'uploader', default='anonymous')
  219. thumbnail = self._search_regex(
  220. [r'''["']thumbUrl["']\s*:\s*(?P<q>["'])(?P<thumbnail>.+?)(?P=q)''',
  221. r'''<video[^>]+"poster"=(?P<q>["'])(?P<thumbnail>.+?)(?P=q)[^>]*>'''],
  222. webpage, 'thumbnail', fatal=False, group='thumbnail')
  223. duration = parse_duration(self._search_regex(
  224. [r'<[^<]+\bitemprop=["\']duration["\'][^<]+\bcontent=["\'](.+?)["\']',
  225. r'Runtime:\s*</span>\s*([\d:]+)'], webpage,
  226. 'duration', fatal=False))
  227. view_count = int_or_none(self._search_regex(
  228. r'content=["\']User(?:View|Play)s:(\d+)',
  229. webpage, 'view count', fatal=False))
  230. mobj = re.search(r'hint=[\'"](?P<likecount>\d+) Likes / (?P<dislikecount>\d+) Dislikes', webpage)
  231. (like_count, dislike_count) = (mobj.group('likecount'), mobj.group('dislikecount')) if mobj else (None, None)
  232. mobj = re.search(r'</label>Comments \((?P<commentcount>\d+)\)</div>', webpage)
  233. comment_count = mobj.group('commentcount') if mobj else 0
  234. categories_html = self._search_regex(
  235. r'(?s)<table.+?(<span>Categories:.+?)</table>', webpage,
  236. 'categories', default=None)
  237. categories = [clean_html(category) for category in re.findall(
  238. r'<a[^>]+>(.+?)</a>', categories_html)] if categories_html else None
  239. return {
  240. 'id': video_id,
  241. 'display_id': display_id,
  242. 'title': title,
  243. 'description': description,
  244. 'upload_date': upload_date,
  245. 'uploader': uploader,
  246. 'thumbnail': thumbnail,
  247. 'duration': duration,
  248. 'view_count': view_count,
  249. 'like_count': int_or_none(like_count),
  250. 'dislike_count': int_or_none(dislike_count),
  251. 'comment_count': int_or_none(comment_count),
  252. 'age_limit': age_limit,
  253. 'categories': categories,
  254. 'formats': formats,
  255. }
  256. class XHamsterEmbedIE(InfoExtractor):
  257. _VALID_URL = r'https?://(?:.+?\.)?xhamster\.com/xembed\.php\?video=(?P<id>\d+)'
  258. _TEST = {
  259. 'url': 'http://xhamster.com/xembed.php?video=3328539',
  260. 'info_dict': {
  261. 'id': '3328539',
  262. 'ext': 'mp4',
  263. 'title': 'Pen Masturbation',
  264. 'timestamp': 1406581861,
  265. 'upload_date': '20140728',
  266. 'uploader': 'ManyakisArt',
  267. 'duration': 5,
  268. 'age_limit': 18,
  269. }
  270. }
  271. @staticmethod
  272. def _extract_urls(webpage):
  273. return [url for _, url in re.findall(
  274. r'<iframe[^>]+?src=(["\'])(?P<url>(?:https?:)?//(?:www\.)?xhamster\.com/xembed\.php\?video=\d+)\1',
  275. webpage)]
  276. def _real_extract(self, url):
  277. video_id = self._match_id(url)
  278. webpage = self._download_webpage(url, video_id)
  279. video_url = self._search_regex(
  280. r'href="(https?://xhamster\.com/(?:movies/{0}/[^"]*\.html|videos/[^/]*-{0})[^"]*)"'.format(video_id),
  281. webpage, 'xhamster url', default=None)
  282. if not video_url:
  283. vars = self._parse_json(
  284. self._search_regex(r'vars\s*:\s*({.+?})\s*,\s*\n', webpage, 'vars'),
  285. video_id)
  286. video_url = dict_get(vars, ('downloadLink', 'homepageLink', 'commentsLink', 'shareUrl'))
  287. return self.url_result(video_url, 'XHamster')