Last active
June 24, 2022 16:26
-
-
Save jaydeepkarale/736df12fa1a4935322bb03a107d340f1 to your computer and use it in GitHub Desktop.
Integrate Validators With Shorten Method
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
def validate_url_format(longurl: str): | |
"""This function returns True if the longurl is in a valid format""" | |
return validators.url(longurl) | |
def validate_url(longurl: str): | |
"""This function returns True if the longurl is a valid web address""" | |
try: | |
headers = {'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0'} | |
response = requests.get(longurl, headers=headers) | |
return response.status_code == HTTPStatus.OK | |
except Exception as ex: | |
print(ex) | |
return False | |
def shorten_url(longurl: str): | |
ngrok_url = get_ngrok_url() | |
size = 6 | |
chars = string.ascii_uppercase + string.digits | |
code = "".join(random.choice(chars) for _ in range(size)) | |
short_url = ngrok_url + "/" + code | |
try: | |
cursor.execute(f"INSERT INTO URLSHORTNER VALUES (?, ?, ?)",(longurl, short_url, code,)) | |
except Exception as ex: | |
print(ex) | |
return short_url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment