hussein2000 commited on
Commit
976388a
1 Parent(s): b722bd0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -30
app.py CHANGED
@@ -1,7 +1,7 @@
1
- from flask import Flask, request, jsonify
2
- from urllib.parse import quote, urlparse, parse_qs, unquote
3
  import requests
4
  from bs4 import BeautifulSoup
 
5
 
6
  app = Flask(__name__)
7
 
@@ -48,39 +48,16 @@ def duckduckgo_search(query):
48
  })
49
  return results
50
 
51
- # DuckDuckGo search API
 
52
  @app.route('/search', methods=['GET'])
53
- def search_duckduckgo():
54
  query = request.args.get('query')
55
  if not query:
56
- return jsonify([]), 400
57
 
58
  results = duckduckgo_search(query)
59
  return jsonify(results)
60
 
61
- # Google Image Search API
62
- @app.route('/image_search', methods=['GET'])
63
- def get_image_urls():
64
- query = request.args.get('query')
65
- num_images = int(request.args.get('num_images', 10))
66
-
67
- if not query:
68
- return jsonify([]), 400
69
-
70
- query = quote(query)
71
- url = f"https://www.google.com/search?hl=en&tbm=isch&q={query}"
72
- headers = {"User-Agent": "Mozilla/5.0"}
73
-
74
- response = requests.get(url, headers=headers)
75
- soup = BeautifulSoup(response.text, 'html.parser')
76
-
77
- image_urls = []
78
- for img_tag in soup.find_all("img", limit=num_images):
79
- img_url = img_tag.get("src")
80
- if img_url and img_url.startswith("http"):
81
- image_urls.append(img_url)
82
-
83
- return jsonify(image_urls)
84
-
85
  if __name__ == '__main__':
86
- app.run(host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, request, jsonify, render_template
 
2
  import requests
3
  from bs4 import BeautifulSoup
4
+ from urllib.parse import urlparse, parse_qs, unquote
5
 
6
  app = Flask(__name__)
7
 
 
48
  })
49
  return results
50
 
51
+
52
+
53
  @app.route('/search', methods=['GET'])
54
+ def search():
55
  query = request.args.get('query')
56
  if not query:
57
+ return jsonify({'error': 'No query provided'}), 400
58
 
59
  results = duckduckgo_search(query)
60
  return jsonify(results)
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  if __name__ == '__main__':
63
+ app.run(host="0.0.0.0", port=7860)