# -*- coding: utf-8 -*- # Copyright 2019-2023 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as # published by the Free Software Foundation. """Extractors for https://www.pornhub.com/""" from .common import Extractor, Message from .. import text, exception BASE_PATTERN = r"(?:https?://)?(?:[\w-]+\.)?pornhub\.com" class PornhubExtractor(Extractor): """Base class for pornhub extractors""" category = "pornhub" root = "https://www.pornhub.com" def _init(self): self.cookies.set( "accessAgeDisclaimerPH", "1", domain=".pornhub.com") def _pagination(self, user, path): if "/" not in path: path += "/public" url = "{}/{}/{}/ajax".format(self.root, user, path) params = {"page": 1} headers = { "Referer": url[:-5], "X-Requested-With": "XMLHttpRequest", } while True: response = self.request( url, method="POST", headers=headers, params=params, allow_redirects=False) if 300 <= response.status_code < 400: url = "{}{}/{}/ajax".format( self.root, response.headers["location"], path) continue yield response.text params["page"] += 1 class PornhubGalleryExtractor(PornhubExtractor): """Extractor for image galleries on pornhub.com""" subcategory = "gallery" directory_fmt = ("{category}", "{user}", "{gallery[id]} {gallery[title]}") filename_fmt = "{num:>03}_{id}.{extension}" archive_fmt = "{id}" pattern = BASE_PATTERN + r"/album/(\d+)" example = "https://www.pornhub.com/album/12345" def __init__(self, match): PornhubExtractor.__init__(self, match) self.gallery_id = match.group(1) self._first = None def items(self): data = self.metadata() yield Message.Directory, data for num, image in enumerate(self.images(), 1): url = image["url"] image.update(data) image["num"] = num yield Message.Url, url, text.nameext_from_url(url, image) def metadata(self): url = "{}/album/{}".format( self.root, self.gallery_id) extr = text.extract_from(self.request(url).text) title = extr("", "") score = extr('
', '<') tags = extr('
")), } yield Message.Directory, gif yield Message.Url, gif["url"], text.nameext_from_url(gif["url"], gif) class PornhubUserExtractor(PornhubExtractor): """Extractor for a pornhub user""" subcategory = "user" pattern = BASE_PATTERN + r"/((?:users|model|pornstar)/[^/?#]+)/?$" example = "https://www.pornhub.com/model/USER" def __init__(self, match): PornhubExtractor.__init__(self, match) self.user = match.group(1) def initialize(self): pass def items(self): base = "{}/{}/".format(self.root, self.user) return self._dispatch_extractors(( (PornhubPhotosExtractor, base + "photos"), (PornhubGifsExtractor , base + "gifs"), ), ("photos",)) class PornhubPhotosExtractor(PornhubExtractor): """Extractor for all galleries of a pornhub user""" subcategory = "photos" pattern = (BASE_PATTERN + r"/((?:users|model|pornstar)/[^/?#]+)" "/(photos(?:/[^/?#]+)?)") example = "https://www.pornhub.com/model/USER/photos" def __init__(self, match): PornhubExtractor.__init__(self, match) self.user, self.path = match.groups() def items(self): data = {"_extractor": PornhubGalleryExtractor} for page in self._pagination(self.user, self.path): gid = None for gid in text.extract_iter(page, 'id="albumphoto', '"'): yield Message.Queue, self.root + "/album/" + gid, data if gid is None: return class PornhubGifsExtractor(PornhubExtractor): """Extractor for a pornhub user's gifs""" subcategory = "gifs" pattern = (BASE_PATTERN + r"/((?:users|model|pornstar)/[^/?#]+)" "/(gifs(?:/[^/?#]+)?)") example = "https://www.pornhub.com/model/USER/gifs" def __init__(self, match): PornhubExtractor.__init__(self, match) self.user, self.path = match.groups() def items(self): data = {"_extractor": PornhubGifExtractor} for page in self._pagination(self.user, self.path): gid = None for gid in text.extract_iter(page, 'id="gif', '"'): yield Message.Queue, self.root + "/gif/" + gid, data if gid is None: return