-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) + "/" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 : ... |