codelion commited on
Commit
8cf055b
·
verified ·
1 Parent(s): a85394b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -1,9 +1,10 @@
1
- from flask import Flask, request, render_template_string, Response
2
  from openai import OpenAI
3
  import os
4
  import json
5
  from urllib.parse import quote
6
  import html
 
7
 
8
  app = Flask(__name__)
9
 
@@ -34,7 +35,7 @@ def fetch_search_results(query):
34
 
35
  try:
36
  response = client.chat.completions.create(
37
- model="gemini-2.0-flash-lite", # Updated model name
38
  messages=[
39
  {"role": "system", "content": "You are a helpful search engine."},
40
  {"role": "user", "content": prompt}
@@ -64,6 +65,21 @@ def fetch_search_results(query):
64
  else:
65
  return None, f"Error: {error_msg}"
66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  @app.route('/', methods=['GET'])
68
  def search_page():
69
  """Serve the initial page or process search with a progress bar and URL validation."""
@@ -362,22 +378,21 @@ def search_page():
362
  }}
363
  async function checkLinks() {{
364
  const links = document.querySelectorAll('.search-result a');
365
- for (let i = 0; i < links.length; i++) {{
366
- const url = links[i].href;
367
  try {{
368
- const controller = new AbortController();
369
- const timeoutId = setTimeout(() => controller.abort(), 5000); // 5s timeout
370
- const response = await fetch(url, {{ method: 'HEAD', signal: controller.signal }});
371
- clearTimeout(timeoutId);
372
- if (!response.ok) {{
373
- links[i].textContent += ' [Broken Link]';
374
- links[i].classList.add('broken');
375
  }}
376
  }} catch (error) {{
377
- links[i].textContent += ' [Broken Link]';
378
- links[i].classList.add('broken');
379
  }}
380
- }}
 
381
  }}
382
  window.onload = function() {{
383
  hideProgress();
 
1
+ from flask import Flask, request, render_template_string, Response, jsonify
2
  from openai import OpenAI
3
  import os
4
  import json
5
  from urllib.parse import quote
6
  import html
7
+ import requests
8
 
9
  app = Flask(__name__)
10
 
 
35
 
36
  try:
37
  response = client.chat.completions.create(
38
+ model="gemini-2.0-flash-lite", # Adjust model name as needed
39
  messages=[
40
  {"role": "system", "content": "You are a helpful search engine."},
41
  {"role": "user", "content": prompt}
 
65
  else:
66
  return None, f"Error: {error_msg}"
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
+ response = requests.head(url, timeout=5)
76
+ if response.status_code == 200:
77
+ return jsonify({'broken': False})
78
+ else:
79
+ return jsonify({'broken': True})
80
+ except requests.RequestException:
81
+ return jsonify({'broken': True})
82
+
83
  @app.route('/', methods=['GET'])
84
  def search_page():
85
  """Serve the initial page or process search with a progress bar and URL validation."""
 
378
  }}
379
  async function checkLinks() {{
380
  const links = document.querySelectorAll('.search-result a');
381
+ const promises = Array.from(links).map(async (link) => {{
382
+ const url = encodeURIComponent(link.href);
383
  try {{
384
+ const response = await fetch(`/check-url?url=${url}`);
385
+ const data = await response.json();
386
+ if (data.broken) {{
387
+ link.textContent += ' [Broken Link]';
388
+ link.classList.add('broken');
 
 
389
  }}
390
  }} catch (error) {{
391
+ link.textContent += ' [Broken Link]';
392
+ link.classList.add('broken');
393
  }}
394
+ }});
395
+ await Promise.all(promises);
396
  }}
397
  window.onload = function() {{
398
  hideProgress();