1+ from duckduckgo_search import DDGS
2+ # The DDGS and AsyncDDGS classes are used to retrieve search results from DuckDuckGo.com.
3+ from typing import List , Optional
4+
5+
6+ class WebSearch :
7+ @staticmethod
8+ def retrieve_web_search_results (query : str , max_results : Optional [int ] = 5 ) -> List :
9+ """
10+ Retrieve search results from duckduckgo.com.
11+
12+ Args:
13+ query (str): The search query to retrieve results for.
14+ max_results Optional[int]: The maximum number of search results to retrieve (default 5).
15+
16+ Returns:
17+ List of dictionaries containing the title, URL, and description of each search result.
18+ """
19+
20+ with DDGS () as ddgs :
21+ results = [r for r in ddgs .text (query , max_results = max_results )]
22+ return results
23+
24+ @staticmethod
25+ def web_search_text (query : str , max_results : Optional [int ] = 5 ) -> List :
26+ """
27+ Search for text on duckduckgo.com.
28+
29+ Args:
30+ query (str): The text to search for.
31+ max_results Optional[int]: The maximum number of search results to retrieve (default 10).
32+
33+ Returns:
34+ List of search results as strings.
35+ """
36+
37+ with DDGS () as ddgs :
38+ results = [r for r in ddgs .text (
39+ query , region = 'wt-wt' , safesearch = 'off' , timelimit = 'y' , max_results = max_results )]
40+ return results
41+
42+ @staticmethod
43+ def web_search_pdf (query : str , max_results : Optional [int ] = 5 ) -> List :
44+ """
45+ Search for PDF files on duckduckgo.com.
46+
47+ Args:
48+ query (str): The text to search for.
49+ max_results Optional[int]: The maximum number of search results to retrieve (default 10).
50+
51+ Returns:
52+ List of search results as dictionaries containing the title, URL, and description of each PDF file.
53+ """
54+ # Searching for pdf files
55+ with DDGS () as ddgs :
56+ results = [r for r in ddgs .text (
57+ f'{ query } :pdf' , region = 'wt-wt' , safesearch = 'off' , timelimit = 'y' , max_results = max_results )]
58+ return results
59+
60+ @staticmethod
61+ def get_instant_web_answer (query : str ) -> List :
62+ """
63+ Retrieve instant answers from DuckDuckGo.com.
64+
65+ Args:
66+ query (str): The text to search for.
67+
68+ Returns:
69+ List of instant answers as strings.
70+ """
71+ with DDGS () as ddgs :
72+ results = [r for r in ddgs .answers (query )]
73+ return results
74+
75+ @staticmethod
76+ def web_search_image (keywords : str , max_results : Optional [int ] = 5 ) -> List :
77+ """
78+ Search for images on DuckDuckGo.com.
79+
80+ Args:
81+ keywords (str): The keywords to search for.
82+ max_results Optional[int]: The maximum number of search results to retrieve (default 100).
83+
84+ Returns:
85+ List of search results as dictionaries containing the title, URL, and image URL of each image.
86+
87+ """
88+
89+ with DDGS () as ddgs :
90+ ddgs_images_gen = ddgs .images (
91+ keywords ,
92+ region = "us-en" ,
93+ safesearch = "on" ,
94+ size = None ,
95+ color = None ,
96+ type_image = None ,
97+ layout = None ,
98+ license_image = None ,
99+ max_results = max_results ,
100+ )
101+ # print(ddgs_images_gen)
102+ results = [r for r in ddgs_images_gen ]
103+ return results
104+
105+ @staticmethod
106+ def web_search_video (keywords : str , max_results : Optional [int ] = 5 ) -> List :
107+ """
108+ Search for videos on DuckDuckGo.com.
109+
110+ Args:
111+ keywords (str): The keywords to search for.
112+ max_results Optional[int]: The maximum number of search results to retrieve (default 100).
113+
114+ Returns:
115+ List of search results as dictionaries containing the title, URL, and thumbnail URL of each video.
116+ """
117+ with DDGS () as ddgs :
118+ ddgs_videos_gen = ddgs .videos (
119+ keywords ,
120+ region = "wt-wt" ,
121+ safesearch = "off" ,
122+ timelimit = "w" ,
123+ resolution = "high" ,
124+ duration = "medium" ,
125+ max_results = max_results ,
126+ )
127+ results = [r for r in ddgs_videos_gen ]
128+ return results
129+
130+ @staticmethod
131+ def web_search_news (keywords : str , max_results : Optional [int ] = 5 ) -> List :
132+ """
133+ Search for news articles on DuckDuckGo.com.
134+
135+ Args:
136+ keywords (str): The keywords to search for.
137+ max_results Optional[int]: The maximum number of search results to retrieve (default 20).
138+
139+ Returns:
140+ List of search results as dictionaries containing the title, URL, and snippet of each news article.
141+ """
142+
143+ with DDGS () as ddgs :
144+ ddgs_news_gen = ddgs .news (
145+ keywords ,
146+ region = "wt-wt" ,
147+ safesearch = "off" ,
148+ timelimit = "m" ,
149+ max_results = max_results
150+ )
151+ results = [r for r in ddgs_news_gen ]
152+ return results
153+
154+ @staticmethod
155+ def web_search_map (query : str , place : str = "Ottawa" , max_results : Optional [int ] = 5 ):
156+ """
157+ Search for maps on DuckDuckGo.com.
158+
159+ Args:
160+ query (str): The text to search for.
161+ place (str): The location to search for maps of (default "ottawa").
162+ max_results Optional[int]: The maximum number of search results to retrieve (default 50).
163+
164+ Returns:
165+ List of search results as dictionaries containing the title, URL, and image URL of each map.
166+ """
167+ with DDGS () as ddgs :
168+ results = [r for r in ddgs .maps (
169+ query , place = place , max_results = max_results )]
170+ return results
171+
172+ @staticmethod
173+ def give_web_search_suggestion (query ):
174+ """
175+ Retrieve search suggestions from DuckDuckGo.com.
176+
177+ Args:
178+ query (str): The text to retrieve suggestions for.
179+
180+ Returns:
181+ List of search suggestions as strings.
182+ """
183+ with DDGS () as ddgs :
184+ results = [r for r in ddgs .suggestions (query )]
185+ return results
186+
187+ @staticmethod
188+ def user_proxy_for_text_web_search (query : str , timeout : Optional [int ] = 20 , max_results : Optional [int ] = 5 ):
189+ """
190+ Search for text on DuckDuckGo.com using a user-defined proxy.
191+
192+ Args:
193+ query (str): The text to search for.
194+ timeout Optional[int]: The timeout for the request in seconds (default 20).
195+ max_results Optional[int]: The maximum number of search results to retrieve (default 50).
196+
197+ Returns:
198+ List of search results as strings.
199+ """
200+ with DDGS (proxies = "socks5://localhost:9150" , timeout = timeout ) as ddgs :
201+ results = [r for r in ddgs .text (query , max_results = max_results )]
202+ return results
203+
204+ if __name__ == "__main__" :
205+ response = WebSearch .retrieve_web_search_results (query = "Who win the ipl 2025?" )
206+ print (response )
0 commit comments