From 06c8ba4c38e8ab16e8afaef49a7d5d9d88a345ea Mon Sep 17 00:00:00 2001 From: Julien Pontoire Date: Mon, 7 Oct 2024 16:49:53 +0200 Subject: [PATCH] Adding reddit url functions --- ural/reddit.py | 39 +++++++++++++++++++++++++++++++++++++++ ural/reddit.pyi | 7 +++++++ 2 files changed, 46 insertions(+) create mode 100644 ural/reddit.py create mode 100644 ural/reddit.pyi diff --git a/ural/reddit.py b/ural/reddit.py new file mode 100644 index 0000000..c4beba5 --- /dev/null +++ b/ural/reddit.py @@ -0,0 +1,39 @@ +import re + +from ural.patterns import DOMAIN_TEMPLATE +from ural.utils import SplitResult +from ural import get_domain_name, urlpathsplit + + +REDDIT_DOMAIN_RE = re.compile(r"(?:reddit\.[^.]+$|redd\.it$)", re.I) +REDDIT_URL_RE = re.compile(DOMAIN_TEMPLATE % r"(?:[^.]+\.)*(?:reddit\.[^.]+|redd\.it)", re.I) + + +def is_reddit_url(url): + if isinstance(url, SplitResult): + return bool(re.search(REDDIT_DOMAIN_RE, url.hostname)) + + return bool(re.match(REDDIT_URL_RE, url)) + + +def is_reddit_post_url(url): + if not is_reddit_url(url): + return False + + return ( + '/r/' in url and '/comments/' in url + ) + + +def convert_reddit_url_to_old_url(url): + domain = get_domain_name(url) + path = urlpathsplit(url) + return f"https://old.{domain}/" + "/".join(path) + "/" + + +def convert_old_reddit_url_to_new_url(url): + domain = get_domain_name(url) + path = urlpathsplit(url) + return f"https://www.{domain}/" + "/".join(path) + "/" + + diff --git a/ural/reddit.pyi b/ural/reddit.pyi new file mode 100644 index 0000000..07569e4 --- /dev/null +++ b/ural/reddit.pyi @@ -0,0 +1,7 @@ +from typing import Optional +from ural.types import AnyUrlTarget + +def is_reddit_url(url: str) -> bool : ... +def is_reddit_post_url(url: str) -> bool : ... +def convert_reddit_url_to_old_url(url: str) -> bool : ... +def convert_old_reddit_url_to_new_url(url: str) -> bool : ... \ No newline at end of file