added
This commit is contained in:
BIN
Lnkshrt/main.zip
Normal file
BIN
Lnkshrt/main.zip
Normal file
Binary file not shown.
74
Lnkshrt/main/app.py
Normal file
74
Lnkshrt/main/app.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from flask import Flask, render_template, request
|
||||
import os
|
||||
import re # <--- FIX 1: Added missing import
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
# Ensure a directory exists to hold the generated links
|
||||
# This makes sure we don't clutter the root directory
|
||||
OUTPUT_DIR = "generated_links"
|
||||
if not os.path.exists(OUTPUT_DIR):
|
||||
os.makedirs(OUTPUT_DIR)
|
||||
|
||||
@app.route('/')
|
||||
def home():
|
||||
return render_template('index.html')
|
||||
|
||||
def is_valid_subdirectory(name):
|
||||
pattern = r"^[a-zA-Z0-9_-]+$"
|
||||
# Added check to ensure 'name' isn't None
|
||||
return bool(name and re.match(pattern, name))
|
||||
|
||||
def is_valid_link(url):
|
||||
pattern = r"^(https?:\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$"
|
||||
return bool(url and re.match(pattern, url.lower()))
|
||||
|
||||
def create_redirect_html(filename, target_url):
|
||||
# Ensure target_url has a protocol so the browser doesn't think it's a local file
|
||||
if not target_url.startswith(('http://', 'https://')):
|
||||
target_url = 'https://' + target_url
|
||||
|
||||
if not filename.endswith(".html"):
|
||||
filename += ".html"
|
||||
|
||||
# Save inside the output directory
|
||||
file_path = os.path.join(OUTPUT_DIR, filename)
|
||||
|
||||
html_content = f"""<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; url={target_url}">
|
||||
<title>Redirecting...</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>If you are not redirected, <a href="{target_url}">click here</a>.</p>
|
||||
</body>
|
||||
</html>"""
|
||||
|
||||
try:
|
||||
with open(file_path, "w", encoding="utf-8") as file:
|
||||
file.write(html_content)
|
||||
return filename
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
return None
|
||||
|
||||
@app.route('/submit', methods=['POST'])
|
||||
def submit():
|
||||
val1 = request.form.get('first_name') # The URL
|
||||
val2 = request.form.get('second_name') # The New Name
|
||||
|
||||
# FIX 2: Validate correctly and use the right order in the function call
|
||||
if is_valid_link(val1) and is_valid_subdirectory(val2):
|
||||
# We want: create_redirect_html(filename, destination)
|
||||
generated_file = create_redirect_html(val2, val1)
|
||||
|
||||
if generated_file:
|
||||
return f"Success! Created p.wholeworldcoding.com/{generated_file}"
|
||||
else:
|
||||
return "Error: Could not write file.", 500
|
||||
else:
|
||||
return "Invalid Input! Ensure the link is valid and the page name has no slashes.", 400
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run(debug=True)
|
||||
10
Lnkshrt/main/generated_links/hi.html
Normal file
10
Lnkshrt/main/generated_links/hi.html
Normal file
@@ -0,0 +1,10 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="refresh" content="0; url=https://gemini.google.com/app/b66596a12b8ee2a7">
|
||||
<title>Redirecting...</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>If you are not redirected, <a href="https://gemini.google.com/app/b66596a12b8ee2a7">click here</a>.</p>
|
||||
</body>
|
||||
</html>
|
||||
95
Lnkshrt/main/templates/index.html
Normal file
95
Lnkshrt/main/templates/index.html
Normal file
@@ -0,0 +1,95 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<style>
|
||||
body, html {
|
||||
margin: 0; padding: 0; height: 100%;
|
||||
display: flex; justify-content: center; align-items: center;
|
||||
background-color: #f0f2f5; font-family: sans-serif;
|
||||
}
|
||||
|
||||
/* This wrapper keeps the panel and the message stacked vertically */
|
||||
.main-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.panel {
|
||||
background: white; width: 100%; max-width: 900px;
|
||||
aspect-ratio: 16 / 9; padding: 20px; box-sizing: border-box;
|
||||
box-shadow: 0 10px 25px rgba(0,0,0,0.1); border-radius: 8px;
|
||||
display: flex; flex-direction: column; justify-content: center;
|
||||
}
|
||||
|
||||
.header { text-align: center; margin-bottom: 20px; }
|
||||
.input-group { margin-bottom: 15px; display: flex; flex-direction: column; }
|
||||
label { font-size: 0.85rem; margin-bottom: 5px; font-weight: bold; color: #555; }
|
||||
input { padding: 10px; border: 1px solid #ccc; border-radius: 4px; font-size: 1rem; }
|
||||
|
||||
/* Style for the success message */
|
||||
#responseMessage {
|
||||
margin-top: 20px;
|
||||
font-weight: bold;
|
||||
color: #27ae60;
|
||||
min-height: 1.2em; /* Prevents layout jump when text appears */
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="main-container">
|
||||
<form id="myForm" class="panel">
|
||||
<div class="header">
|
||||
<h1>Link Shortener</h1>
|
||||
<p>Insert a link and shorten it for sharing!</p>
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label for="input1">Original Link</label>
|
||||
<input type="text" id="input1" name="first_name" required>
|
||||
</div>
|
||||
|
||||
<div class="input-group">
|
||||
<label for="input2">New page (www.wholeworldcoding.com/p/)</label>
|
||||
<input type="text" id="input2" name="second_name" required>
|
||||
</div>
|
||||
|
||||
<button type="submit" style="margin-top: 10px; padding: 10px; cursor: pointer; background: #3498db; color: white; border: none; border-radius: 4px;">
|
||||
Generate Link!
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div id="responseMessage"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
const form = document.getElementById('myForm');
|
||||
const responseDiv = document.getElementById('responseMessage');
|
||||
|
||||
form.addEventListener('submit', function(e) {
|
||||
e.preventDefault(); // Stop page reload
|
||||
|
||||
const formData = new FormData(form);
|
||||
|
||||
fetch('/submit', {
|
||||
method: 'POST',
|
||||
body: formData
|
||||
})
|
||||
.then(response => response.text())
|
||||
.then(data => {
|
||||
// Display the Python response in the div
|
||||
responseDiv.innerText = data;
|
||||
form.reset(); // Clear the inputs
|
||||
})
|
||||
.catch(error => {
|
||||
responseDiv.innerText = "Error communicating with server.";
|
||||
console.error(error);
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user