Spaces:
Sleeping
Sleeping
hussein2000
commited on
Commit
•
b722bd0
1
Parent(s):
0600101
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,61 @@
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
-
from urllib.parse import quote
|
3 |
import requests
|
4 |
from bs4 import BeautifulSoup
|
5 |
|
6 |
app = Flask(__name__)
|
7 |
|
8 |
-
|
9 |
-
@app.route('/search', methods=['GET'])
|
10 |
-
def search_duckduckgo():
|
11 |
-
query = request.args.get('query')
|
12 |
-
if not query:
|
13 |
-
return jsonify([]), 400
|
14 |
-
|
15 |
url = 'https://duckduckgo.com/html/'
|
16 |
params = {'q': query}
|
17 |
headers = {'User-Agent': 'Mozilla/5.0'}
|
18 |
-
|
19 |
response = requests.get(url, params=params, headers=headers)
|
20 |
if response.status_code != 200:
|
21 |
-
return
|
22 |
|
23 |
soup = BeautifulSoup(response.text, 'html.parser')
|
24 |
results = []
|
25 |
-
for result in soup.find_all('
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
results.append({
|
29 |
'title': title,
|
30 |
-
'link':
|
|
|
|
|
31 |
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
return jsonify(results)
|
33 |
|
34 |
# Google Image Search API
|
@@ -56,4 +83,4 @@ def get_image_urls():
|
|
56 |
return jsonify(image_urls)
|
57 |
|
58 |
if __name__ == '__main__':
|
59 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
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 |
|
8 |
+
def duckduckgo_search(query):
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
url = 'https://duckduckgo.com/html/'
|
10 |
params = {'q': query}
|
11 |
headers = {'User-Agent': 'Mozilla/5.0'}
|
12 |
+
|
13 |
response = requests.get(url, params=params, headers=headers)
|
14 |
if response.status_code != 200:
|
15 |
+
return []
|
16 |
|
17 |
soup = BeautifulSoup(response.text, 'html.parser')
|
18 |
results = []
|
19 |
+
for result in soup.find_all('div', class_='result'):
|
20 |
+
title_tag = result.find('a', class_='result__a')
|
21 |
+
if not title_tag:
|
22 |
+
continue
|
23 |
+
title = title_tag.get_text()
|
24 |
+
link = title_tag['href']
|
25 |
+
|
26 |
+
# Decode the actual URL from the DuckDuckGo redirect link
|
27 |
+
parsed_link = urlparse(link)
|
28 |
+
query_params = parse_qs(parsed_link.query)
|
29 |
+
actual_url = unquote(query_params.get('uddg', [''])[0])
|
30 |
+
|
31 |
+
description_tag = result.find('a', class_='result__snippet')
|
32 |
+
description = description_tag.get_text() if description_tag else 'No description available'
|
33 |
+
|
34 |
+
# Attempt to fetch the favicon
|
35 |
+
icon = 'No icon available'
|
36 |
+
if actual_url:
|
37 |
+
parsed_actual_url = urlparse(actual_url)
|
38 |
+
favicon_url = f"{parsed_actual_url.scheme}://{parsed_actual_url.netloc}/favicon.ico"
|
39 |
+
favicon_response = requests.get(favicon_url, headers=headers)
|
40 |
+
if favicon_response.status_code == 200:
|
41 |
+
icon = favicon_url
|
42 |
+
|
43 |
results.append({
|
44 |
'title': title,
|
45 |
+
'link': actual_url,
|
46 |
+
'description': description,
|
47 |
+
'icon': icon
|
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
|
|
|
83 |
return jsonify(image_urls)
|
84 |
|
85 |
if __name__ == '__main__':
|
86 |
+
app.run(host="0.0.0.0", port=7860)
|