From 9964f710d006c9e23283173b7c00a298f562a3a0 Mon Sep 17 00:00:00 2001 From: Johannes Habel Date: Mon, 12 Aug 2024 15:38:55 +0200 Subject: [PATCH] - fixing the subdomain URLs to use www.pornhub.com/org format --- src/phub/core.py | 2 +- src/phub/objects/video.py | 1 + src/phub/utils.py | 21 ++++++++++++++++++++- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/phub/core.py b/src/phub/core.py index 639f7c5..4e45218 100644 --- a/src/phub/core.py +++ b/src/phub/core.py @@ -140,7 +140,7 @@ def call(self, ConnectionError: If the request was blocked by Pornhub. HTTPError: If the request failed, for any reason. ''' - + func = utils.fix_url(func) logger.log(logging.DEBUG if silent else logging.INFO, 'Fetching %s', func or '/') # Delay mechanism diff --git a/src/phub/objects/video.py b/src/phub/objects/video.py index 25763c9..ba9c60e 100644 --- a/src/phub/objects/video.py +++ b/src/phub/objects/video.py @@ -39,6 +39,7 @@ def __init__(self, client: Client, url: str, change_title_language: bool = True, url (str): The video URL. ''' + url = utils.fix_url(url) if not consts.re.is_video_url(url): raise errors.URLError('Invalid video URL:', url) diff --git a/src/phub/utils.py b/src/phub/utils.py index 7f8436a..25dd5f7 100644 --- a/src/phub/utils.py +++ b/src/phub/utils.py @@ -7,6 +7,7 @@ from typing import Generator, Iterable, Iterator, Union from . import errors +from . import consts logger = logging.getLogger(__name__) @@ -241,4 +242,22 @@ def head(client: object, url: str) -> Union[str, bool]: return res.url return False -# EOF \ No newline at end of file + +def fix_url(url: str): + """ + Changes a URL which includes the specific language site of PornHub to the default www.pornhub.com/org format, + so that PornHub respects the accept-language header, so that we can apply our own custom language into the Client. + + Args: + url: (str): The video URL to be changed + + Returns: + str: The video URL without the language site. + """ + + for language, root_url in consts.LANGUAGE_MAPPING.items(): + if f"{language}.pornhub" in url: + url = url.replace(language, "www", 1) + return url + + return url # Sometimes URL doesn't need to be changed. In this case, just return it.