Skip to content

Instantly share code, notes, and snippets.

@jaydeepkarale
Last active June 24, 2022 16:26
Show Gist options
  • Save jaydeepkarale/736df12fa1a4935322bb03a107d340f1 to your computer and use it in GitHub Desktop.
Save jaydeepkarale/736df12fa1a4935322bb03a107d340f1 to your computer and use it in GitHub Desktop.
Integrate Validators With Shorten Method
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