Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions _legacy/skills/web_search.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
import logging
import asyncio
import io
import os
import re
from telegram.ext import ContextTypes

import config

def perform_web_search(query):
from ddgs import DDGS
try:
results = DDGS().text(query, max_results=5)
if not results:
return "No results found."

summary = ""
for r in results:
summary += f"- [{r['title']}]({r['href']}): {r['body']}\n"
return summary
except Exception as e:
return f"Error performing search: {e}"
tavily_api_key = os.environ.get("TAVILY_API_KEY")
if tavily_api_key:
try:
from tavily import TavilyClient
client = TavilyClient(api_key=tavily_api_key)
response = client.search(query=query, max_results=5, search_depth="basic")
results = response.get("results", [])
if not results:
return "No results found."
summary = ""
for r in results:
title = r.get("title", "No title")
url = r.get("url", "")
content = r.get("content", "")
summary += f"- [{title}]({url}): {content}\n"
return summary
except Exception as e:
return f"Error performing search with Tavily: {e}"
else:
from ddgs import DDGS
try:
results = DDGS().text(query, max_results=5)
if not results:
return "No results found."
summary = ""
for r in results:
summary += f"- [{r['title']}]({r['href']}): {r['body']}\n"
return summary
except Exception as e:
return f"Error performing search: {e}"
Comment on lines 10 to +40
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The perform_web_search function contains significant code duplication between the Tavily and DuckDuckGo search paths. Both branches implement nearly identical logic for checking results and formatting the markdown summary. Refactoring this to normalize the results into a common format before generating the summary would improve maintainability and reduce the risk of logic drift between providers.

def perform_web_search(query):
    tavily_api_key = os.environ.get("TAVILY_API_KEY")
    provider = "Tavily" if tavily_api_key else "DuckDuckGo"
    try:
        if tavily_api_key:
            from tavily import TavilyClient
            client = TavilyClient(api_key=tavily_api_key)
            response = client.search(query=query, max_results=5, search_depth="basic")
            results = [
                {"title": r.get("title", "No title"), "url": r.get("url", ""), "body": r.get("content", "")}
                for r in response.get("results", [])
            ]
        else:
            from ddgs import DDGS
            ddgs_results = DDGS().text(query, max_results=5)
            results = [
                {"title": r.get("title", "No title"), "url": r.get("href", ""), "body": r.get("body", "")}
                for r in (ddgs_results or [])
            ]

        if not results:
            return "No results found."

        summary = ""
        for r in results:
            summary += f"- [{r['title']}]({r['url']}): {r['body']}\n"
        return summary
    except Exception as e:
        return f"Error performing search with {provider}: {e}"


def get_youtube_video_id(url):
# Patterns: youtube.com/watch?v=ID, youtu.be/ID, youtube.com/embed/ID
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pyyaml
dateparser
pytz
ddgs
tavily-python
youtube-transcript-api
python-dateutil
playwright
Expand Down