-
Notifications
You must be signed in to change notification settings - Fork 0
/
url_shrinker.py
69 lines (51 loc) · 1.98 KB
/
url_shrinker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import string
import validators
import secrets
import db
default_length = 6
def _generate_shrinked_hash():
alphabet = string.ascii_letters + string.digits
result = ''.join(secrets.choice(alphabet) for _ in range(default_length))
if db.get_url(result) is not None:
result = ''.join(secrets.choice(alphabet) for _ in range(default_length))
return result
def _validate_url(url: str):
# TODO: find better way; this ignores urls without https://
return validators.url(url)
def _validate_hash(shrinked_hash: str):
return shrinked_hash is not None and len(shrinked_hash) <= default_length and shrinked_hash.isalnum()
def shrink_url(url: str):
# Check if url is legal
if not _validate_url(url):
print('Url is not valid!')
return None
# Check if url exists in db and return hash if it does
query_result = db.get_shrinked_hash(url)
if query_result is not None:
return query_result
# Generate new short url
shrinked_hash = _generate_shrinked_hash()
# Insert into db
db.insert_url(url, shrinked_hash)
return shrinked_hash
def redirect_shrinked_hash(shrinked_hash: str):
# Check if shrinked_hash is legal
if not _validate_hash(shrinked_hash):
return None
return db.get_url(shrinked_hash)
if __name__ == '__main__':
for i in range(10):
print(_generate_shrinked_hash())
short_url_1 = shrink_url('https://google.com')
short_url_2 = shrink_url('https://facebook.com')
short_url_3 = shrink_url('https://piccollage.com')
short_url_4 = shrink_url('https://yahoo.com')
print(short_url_1, short_url_2, short_url_3, short_url_4)
print(redirect_shrinked_hash(short_url_1))
print(redirect_shrinked_hash(short_url_2))
print(redirect_shrinked_hash(short_url_3))
print(redirect_shrinked_hash(short_url_4))
short_url_5 = shrink_url('https://facebook.com')
print(short_url_5)
print(redirect_shrinked_hash(short_url_5))
print(redirect_shrinked_hash('errort'))