-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (68 loc) · 2.45 KB
/
main.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
81
82
83
84
85
86
87
88
from qrcode import QRCode
from requests import get
from requests import RequestException
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
from re import compile
# Cache for frequently accessed URLs
url_cache = {}
def shorten_url(url):
if url in url_cache:
return url_cache[url]
try:
response = get(
"http://is.gd/create.php", params={"format": "simple", "url": url}
)
if response.status_code == 200:
short_url = response.text
url_cache[url] = short_url
return short_url
except RequestException:
return f"Failed to shorten URL: {url}"
def read_file(file_path: str) -> list:
with open(file_path, "r") as f:
return [line.strip() for line in f]
def write_file(lines, file_path):
with open(file_path, "w") as f:
f.write("\n".join(lines))
def generate_qr_code(content, index, foldername):
qr = QRCode(version=1, box_size=10, border=5)
qr.add_data(content)
qr.make(fit=True)
filename = f"QR_{index}.png"
path = Path(foldername)
path.mkdir(parents=True, exist_ok=True)
filepath = path.joinpath(filename)
img = qr.make_image(fill_color="black", back_color="white")
img.save(filepath)
print(f"{filepath} generated successfully!")
def process_url(url):
url_pattern = compile(
r"^[(http(s)?):\/\/(www\.)?a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)"
)
if not url_pattern.match(url):
print(f"Invalid URL: {url}")
return None
short_url = shorten_url(url)
if short_url:
return short_url
def main():
input_file_path = "input.txt"
shortened_output_file_path = "shortened_urls.txt"
qr_foldername = "QR_codes"
urls = read_file(input_file_path)
# Shorten URLs
with ThreadPoolExecutor(max_workers=50) as executor:
shortened_urls = list(executor.map(process_url, urls))
shortened_urls = [url for url in shortened_urls if url is not None]
# Write shortened URLs to file
write_file(shortened_urls, shortened_output_file_path)
print(f"Shortened URLs written to {shortened_output_file_path}")
print("Shortened URLs:")
print("\n".join(shortened_urls))
# Generate QR codes
with ThreadPoolExecutor(max_workers=50) as executor:
for i, url in enumerate(shortened_urls):
executor.submit(generate_qr_code, url, i + 1, qr_foldername)
if __name__ == "__main__":
main()