86 lines
2.1 KiB
Python
86 lines
2.1 KiB
Python
from urllib.parse import urlparse
|
|
|
|
|
|
class TwLink:
|
|
"""
|
|
Disclaimer.
|
|
|
|
I have no relation with fxtwitter, nor vxtwitter, or anything else being used there.
|
|
"""
|
|
_user: str
|
|
_post_id: str
|
|
_url: str
|
|
_prefix: [str] # List of valid fxshit
|
|
_valid_url = False
|
|
|
|
def __init__(self, url: str):
|
|
self._url = url
|
|
self.__review_url()
|
|
self._prefix = [
|
|
"https://fxtwitter.com",
|
|
"https://vxtwitter.com"
|
|
]
|
|
|
|
pass
|
|
|
|
def __review_url(self) -> None:
|
|
"""
|
|
Checks if twitter URL is a valid post.
|
|
|
|
It requires:
|
|
- Twitter domain.
|
|
- /status/
|
|
- Username
|
|
- Post ID
|
|
|
|
:return: STR, returns "fixed" URL if given URL is valid.
|
|
Returns nothing if isn't valid.
|
|
"""
|
|
|
|
valid_twitter_hosts = ["twitter.com"]
|
|
parsed_url = urlparse(url=self._url)
|
|
host = parsed_url.netloc
|
|
path: str | [str] = parsed_url.path
|
|
if path.startswith("/"):
|
|
# Remove initial /
|
|
path = path[1:]
|
|
path = path.split("/")
|
|
|
|
if host not in valid_twitter_hosts \
|
|
or len(path) > 3 \
|
|
or path[1].lower() != "status":
|
|
print(f"URL '{self._url}' is NOT valid!")
|
|
else:
|
|
print(f"URL '{self._url}' is valid")
|
|
self._valid_url = True
|
|
self._user = path[0]
|
|
self._post_id = path[2]
|
|
|
|
@property
|
|
def __return_url(self) -> str:
|
|
"""
|
|
Returns the URL if the class been flagged as valid URL.
|
|
|
|
Retrusn an empty string if it's not flagged as valid URL.
|
|
:return:
|
|
"""
|
|
|
|
if self._valid_url:
|
|
return f"{self._prefix[0]}/{self._user}/status/{self._post_id}"
|
|
else:
|
|
return ""
|
|
|
|
def __str__(self) -> str:
|
|
return self.__return_url
|
|
|
|
def __bool__(self) -> bool:
|
|
if self.__return_url is not None:
|
|
return True
|
|
|
|
def __call__(self, *args, **kwargs) -> str:
|
|
return self.__str__()
|
|
|
|
def __int__(self):
|
|
return len(self.__str__())
|
|
|