-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
59 lines (43 loc) · 1.41 KB
/
Copy pathmain.py
File metadata and controls
59 lines (43 loc) · 1.41 KB
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
"""
URL Shortener
Shortens long URLs using various services
"""
import pyshorteners
def shorten_url(long_url, service='tinyurl'):
"""Shorten URL"""
try:
shortener = pyshorteners.Shortener()
if service == 'tinyurl':
short_url = shortener.tinyurl.short(long_url)
elif service == 'isgd':
short_url = shortener.isgd.short(long_url)
else:
short_url = shortener.tinyurl.short(long_url)
return short_url
except Exception as e:
return f"Error: {e}"
def main():
"""Main function"""
print("\n" + "="*60)
print(" URL SHORTENER")
print("="*60 + "\n")
try:
while True:
url = input("Enter URL to shorten (or 'quit'): ").strip()
if url.lower() in ['quit', 'exit', 'q']:
print("\nGoodbye!")
break
if not url:
continue
if not url.startswith(('http://', 'https://')):
url = 'https://' + url
print("\nShortening URL...")
short = shorten_url(url)
print("\n" + "="*60)
print(f"Original: {url}")
print(f"Shortened: {short}")
print("="*60 + "\n")
except KeyboardInterrupt:
print("\n\nGoodbye!")
if __name__ == "__main__":
main()