Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -67,12 +67,13 @@ def fetch_search_results(query):
|
|
67 |
|
68 |
@app.route('/check-url', methods=['GET'])
|
69 |
def check_url():
|
70 |
-
"""Check if a URL is valid (returns 200) or broken."""
|
71 |
url = request.args.get('url', '')
|
72 |
if not url:
|
73 |
return jsonify({'broken': True})
|
74 |
try:
|
75 |
-
|
|
|
76 |
if response.status_code == 200:
|
77 |
return jsonify({'broken': False})
|
78 |
else:
|
@@ -378,9 +379,9 @@ def search_page():
|
|
378 |
}}
|
379 |
async function checkLinks() {{
|
380 |
const links = document.querySelectorAll('.search-result a');
|
381 |
-
const
|
382 |
-
const url = link.href;
|
383 |
-
return fetch('/check-url?url=' +
|
384 |
.then(response => response.json())
|
385 |
.then(data => {{
|
386 |
if (data.broken) {{
|
@@ -393,7 +394,7 @@ def search_page():
|
|
393 |
link.classList.add('broken');
|
394 |
}});
|
395 |
}});
|
396 |
-
await Promise.all(
|
397 |
}}
|
398 |
window.onload = function() {{
|
399 |
hideProgress();
|
|
|
67 |
|
68 |
@app.route('/check-url', methods=['GET'])
|
69 |
def check_url():
|
70 |
+
"""Check if a URL is valid (returns 200) or broken, following redirects."""
|
71 |
url = request.args.get('url', '')
|
72 |
if not url:
|
73 |
return jsonify({'broken': True})
|
74 |
try:
|
75 |
+
# Follow redirects and check final status
|
76 |
+
response = requests.head(url, allow_redirects=True, timeout=5)
|
77 |
if response.status_code == 200:
|
78 |
return jsonify({'broken': False})
|
79 |
else:
|
|
|
379 |
}}
|
380 |
async function checkLinks() {{
|
381 |
const links = document.querySelectorAll('.search-result a');
|
382 |
+
const promises = Array.from(links).map(link => {{
|
383 |
+
const url = encodeURIComponent(link.href);
|
384 |
+
return fetch('/check-url?url=' + url)
|
385 |
.then(response => response.json())
|
386 |
.then(data => {{
|
387 |
if (data.broken) {{
|
|
|
394 |
link.classList.add('broken');
|
395 |
}});
|
396 |
}});
|
397 |
+
await Promise.all(promises);
|
398 |
}}
|
399 |
window.onload = function() {{
|
400 |
hideProgress();
|