Skip to content

Commit

Permalink
Added try excepts
Browse files Browse the repository at this point in the history
  • Loading branch information
onuratakan authored Jun 6, 2024
1 parent ef4198a commit ea1b7bf
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 40 deletions.
9 changes: 5 additions & 4 deletions tiger/tools/search/duckduckgo.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@

def search(query:str, max_number:int=20) -> list:
from duckduckgo_search import DDGS


return [result["href"] for result in DDGS().text(query, max_results=max_number)]
try:
from duckduckgo_search import DDGS
return [result["href"] for result in DDGS().text(query, max_results=max_number)]
except:
return "An exception occurred"


tool_name = "search.duckduckgo"
Expand Down
8 changes: 6 additions & 2 deletions tiger/tools/search/google.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@


def google(query:str, max_number:int=20) -> list:
from googlesearch import search as gsearch
return list(gsearch(query, stop=max_number))
try:
from googlesearch import search as gsearch
return list(gsearch(query, stop=max_number))
except:
return "An exception occurred"



tool_name = "search.google"
Expand Down
72 changes: 38 additions & 34 deletions tiger/tools/search/read_website.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@


def read_website(url: str, max_content_lenght: int = 5000) -> dict:
import requests

from bs4 import BeautifulSoup
import re

html = requests.get(url).text
soup = BeautifulSoup(html)
meta_properties = [
"og:description",
"og:site_name",
"og:title",
"og:type",
"og:url",
]
meta = {}
for property_name in meta_properties:
try:
tag = soup.find("meta", property=property_name)
if tag:
meta[property_name] = str(tag.get("content", None))
except AttributeError:
meta[property_name] = None
for ignore_tag in soup(["script", "style"]):
ignore_tag.decompose()
title = soup.title.string if soup.title else ""
content = soup.body.get_text() if soup.body else ""
links = []
for a in soup.find_all("a", href=True):
links.append({"title": a.text.strip(), "link": a["href"]})
content = re.sub(r"[\n\r\t]+", "\n", content)
content = re.sub(r" +", " ", content)
content = re.sub(r"[\n ]{3,}", "\n\n", content)
content = content.strip()
return {"meta": meta, "title": title, "content": content[:max_content_lenght], "sub_links": links}
try:
import requests

from bs4 import BeautifulSoup
import re

html = requests.get(url).text
soup = BeautifulSoup(html)
meta_properties = [
"og:description",
"og:site_name",
"og:title",
"og:type",
"og:url",
]
meta = {}
for property_name in meta_properties:
try:
tag = soup.find("meta", property=property_name)
if tag:
meta[property_name] = str(tag.get("content", None))
except AttributeError:
meta[property_name] = None
for ignore_tag in soup(["script", "style"]):
ignore_tag.decompose()
title = soup.title.string if soup.title else ""
content = soup.body.get_text() if soup.body else ""
links = []
for a in soup.find_all("a", href=True):
links.append({"title": a.text.strip(), "link": a["href"]})
content = re.sub(r"[\n\r\t]+", "\n", content)
content = re.sub(r" +", " ", content)
content = re.sub(r"[\n ]{3,}", "\n\n", content)
content = content.strip()
return {"meta": meta, "title": title, "content": content[:max_content_lenght], "sub_links": links}

except:
return "An exception occurred"



Expand Down

0 comments on commit ea1b7bf

Please sign in to comment.