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.

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