ddgo
This commit is contained in:
1 parent
374c01458d
commit
b00c27da00
1 file changed
+45
-13
+45
-13
@@ -1,7 +1,9 @@
|
|||||||
import re
|
import re
|
||||||
|
import os
|
||||||
import time
|
import time
|
||||||
import shelve
|
import shelve
|
||||||
import requests
|
import requests
|
||||||
|
import html as _html
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from aiogram.methods import AnswerGuestQuery, SendPoll
|
from aiogram.methods import AnswerGuestQuery, SendPoll
|
||||||
@@ -582,22 +584,52 @@ def apply_time_tags(text):
|
|||||||
|
|
||||||
|
|
||||||
def web_search(query):
|
def web_search(query):
|
||||||
r = requests.get(
|
headers = {"User-Agent": "Mozilla/5.0"}
|
||||||
"https://lite.duckduckgo.com/lite/",
|
links = []
|
||||||
params={"q": query},
|
snippets = []
|
||||||
headers={"User-Agent": "Mozilla/5.0"},
|
|
||||||
timeout=10
|
try:
|
||||||
)
|
r = requests.get("https://lite.duckduckgo.com/lite/", params={"q": query}, headers=headers, timeout=10)
|
||||||
r.raise_for_status()
|
r.raise_for_status()
|
||||||
links = re.findall(r"<a[^>]*class=['\"]result-link['\"][^>]*>(.*?)</a>", r.text, re.DOTALL)
|
links = re.findall(r"<a[^>]*class=['\"]result-link['\"][^>]*>(.*?)</a>", r.text, re.DOTALL)
|
||||||
snippets = re.findall(r"<td[^>]*class=['\"]result-snippet['\"][^>]*>(.*?)</td>", r.text, re.DOTALL)
|
snippets = re.findall(r"<td[^>]*class=['\"]result-snippet['\"][^>]*>(.*?)</td>", r.text, re.DOTALL)
|
||||||
|
if not links:
|
||||||
|
print(f" -- search lite get пусто: {r.status_code} len={len(r.text)}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" -- search lite get - {e}")
|
||||||
|
|
||||||
|
if not links:
|
||||||
|
try:
|
||||||
|
r = requests.post("https://lite.duckduckgo.com/lite/", data={"q": query}, headers=headers, timeout=10)
|
||||||
|
r.raise_for_status()
|
||||||
|
links = re.findall(r"<a[^>]*class=['\"]result-link['\"][^>]*>(.*?)</a>", r.text, re.DOTALL)
|
||||||
|
snippets = re.findall(r"<td[^>]*class=['\"]result-snippet['\"][^>]*>(.*?)</td>", r.text, re.DOTALL)
|
||||||
|
if not links:
|
||||||
|
print(f" -- search lite post пусто: {r.status_code} len={len(r.text)}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" -- search lite post - {e}")
|
||||||
|
|
||||||
|
if not links:
|
||||||
|
try:
|
||||||
|
r = requests.post("https://html.duckduckgo.com/html/", data={"q": query}, headers=headers, timeout=10)
|
||||||
|
r.raise_for_status()
|
||||||
|
links = re.findall(r"<a[^>]*class=['\"]result__a['\"][^>]*>(.*?)</a>", r.text, re.DOTALL)
|
||||||
|
snippets = re.findall(r"<a[^>]*class=['\"]result__snippet['\"][^>]*>(.*?)</a>", r.text, re.DOTALL)
|
||||||
|
if not links:
|
||||||
|
print(f" -- search html post пусто: {r.status_code} len={len(r.text)}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f" -- search html post - {e}")
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
for i in range(min(5, len(links))):
|
for i in range(min(5, len(links))):
|
||||||
title = re.sub(r"<[^>]+>", "", links[i]).strip()
|
title = _html.unescape(re.sub(r"<[^>]+>", "", links[i])).strip()
|
||||||
snippet = re.sub(r"<[^>]+>", "", snippets[i]).strip()[:300] if i < len(snippets) else ""
|
snippet = _html.unescape(re.sub(r"<[^>]+>", "", snippets[i])).strip()[:300] if i < len(snippets) else ""
|
||||||
results.append(f"{title}\n{snippet}")
|
results.append(f"{title}\n{snippet}")
|
||||||
if not results:
|
if not results:
|
||||||
return "ничего не нашлось"
|
try:
|
||||||
|
return wiki_search(query)
|
||||||
|
except Exception:
|
||||||
|
return "ничего не нашлось"
|
||||||
return "\n\n".join(results)
|
return "\n\n".join(results)
|
||||||
|
|
||||||
|
|
||||||
@@ -612,7 +644,7 @@ def wiki_search(query):
|
|||||||
results = []
|
results = []
|
||||||
for p in r.json().get("pages", []):
|
for p in r.json().get("pages", []):
|
||||||
title = p.get("title", "")
|
title = p.get("title", "")
|
||||||
excerpt = re.sub(r"<[^>]+>", "", p.get("excerpt", ""))
|
excerpt = _html.unescape(re.sub(r"<[^>]+>", "", p.get("excerpt", "")))
|
||||||
results.append(f"{title}\n{excerpt[:400]}")
|
results.append(f"{title}\n{excerpt[:400]}")
|
||||||
if not results:
|
if not results:
|
||||||
return "ничего не нашлось в википедии"
|
return "ничего не нашлось в википедии"
|
||||||
|
|||||||
Reference in new issue
Block a user