Skip to content

Commit 0a2470b

Browse files
deedy5AbdullahAlfaraj
authored andcommitted
recode image search to use AsyncDDGS
1 parent 7aeed21 commit 0a2470b

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

server/python_server/search.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,26 @@
1-
from itertools import islice
1+
import asyncio
22

33
try:
4-
from duckduckgo_search import DDGS
4+
from duckduckgo_search import AsyncDDGS
55
except ImportError:
66
raise ImportError(
77
"duckduckgo_search is required to image search. Please install it with `pip install --upgrade duckduckgo_search`."
88
)
99

1010

1111
async def imageSearch(keywords="cute cats"):
12-
with DDGS() as ddgs:
13-
return [x for x in islice(ddgs.images(keywords,safesearch='off'), 50)]
12+
async with AsyncDDGS() as ddgs:
13+
return [
14+
x async for x in ddgs.images(keywords, safesearch="off", max_results=50)
15+
]
1416

1517

16-
if __name__ == "__main__":
18+
async def main():
19+
result = await imageSearch()
20+
print("result: ", result)
21+
# result = await imageSearch2()
22+
# print(result)
1723

18-
async def main():
19-
result = await imageSearch()
20-
print("result: ",result)
21-
# result = await imageSearch2()
22-
# print(result)
23-
import asyncio
2424

25+
if __name__ == "__main__":
2526
asyncio.run(main())

0 commit comments

Comments
 (0)