-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathurl.py
80 lines (61 loc) · 1.83 KB
/
url.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
70
71
72
73
74
75
76
77
78
79
80
# This code is made by MRayan Asim
# packages needed:
# pip install pyshorteners
# pip install requests
# pip install qrcode
import re
import pyshorteners
import requests
import qrcode
import time
def validate_url(url):
# Regular expression pattern for URL validation
pattern = re.compile(
r"^https?://" # http:// or https://
r"([A-Za-z0-9.-]+)" # domain
r"(:\d+)?" # optional port number
r"(/[A-Za-z0-9_\.-]*)*?$" # optional path
)
return bool(re.match(pattern, url))
def analyze_url(url):
if validate_url(url):
print("URL is valid.")
# Perform further analysis or processing here
else:
print("Invalid URL.")
def shorten_url(url):
# Initialize the URL shortener
shortener = pyshorteners.Shortener()
# Shorten the URL
shortened_url = shortener.tinyurl.short(url)
return shortened_url
def is_valid_url(url):
# Send a HEAD request to check if the URL exists
try:
response = requests.head(url)
return response.status_code == requests.codes.ok
except requests.exceptions.RequestException:
return False
print("This URL code is made by MRayan Asim. Hope you will like this! 😊")
time.sleep(3)
# Prompt the user to enter a URL
url = input("Enter a URL: ")
# Check if the URL is valid
if is_valid_url(url):
# Shorten the URL
shortened_url = shorten_url(url)
print("Shortened URL:", shortened_url)
else:
print("Invalid URL")
# Analyze the URL
analyze_url(url)
# Data to encode
data = url
# Creating an instance of QRCode class
qr = qrcode.QRCode(version=1, box_size=10, border=5)
# Adding data to the instance 'qr'
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="red", back_color="white")
print("The QR code image is saved to your device with the name 'QRCode.png'")
img.save("QRCode.png")