Skip to content

Commit

Permalink
v0.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
deedy5 committed Mar 16, 2024
1 parent 451096f commit 21bcc0a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 46 deletions.
35 changes: 14 additions & 21 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
# first stage
FROM python:3.11.5-slim AS builder
FROM python:3.12.2-slim-bookworm

ENV DEBIAN_FRONTEND=noninteractive
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

COPY requirements.txt .

RUN apt-get update && apt-get install -y --no-install-recommends --no-install-suggests \
&& pip install --no-cache-dir --user -r requirements.txt \
&& rm -rf /var/lib/apt/lists/*
# Create appuser and switch to it
RUN useradd --create-home appuser
USER appuser

# Include the directory where python packages are installed in the PATH
ENV PATH="/home/appuser/.local/bin:${PATH}"

# final stage
FROM python:3.11.5-slim
# Set the working directory to a folder inside appuser's home directory
WORKDIR /home/appuser/app

# copy only the dependencies installation from the 1st stage image
COPY --from=builder /root/.local /root/.local

# update PATH environment variable
ENV PATH=/root/.local/bin:$PATH

WORKDIR /code
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir --user -r requirements.txt

COPY ./duckduckgo_search_api /code/duckduckgo_search_api
# Copy the application code
COPY ./duckduckgo_search_api /home/appuser/app/duckduckgo_search_api

# Run the application
CMD ["uvicorn", "duckduckgo_search_api.main:app", "--host", "0.0.0.0", "--port", "8000", "--log-level", "warning"]
34 changes: 13 additions & 21 deletions duckduckgo_search_api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from fastapi.responses import ORJSONResponse
from pydantic import BaseModel

__version__ = "0.6.0"
__version__ = "0.7.0"


TIMEOUT = 10
Expand Down Expand Up @@ -144,21 +144,19 @@ async def ddg_search(
] = None,
) -> list[DdgTextOut]:
"""DuckDuckGo text search. Query params: https://duckduckgo.com/params"""
results = []
try:
async with AsyncDDGS(proxies=PROXY, timeout=TIMEOUT) as ddgs:
async for r in ddgs.text(
results = await ddgs.text(
q,
region,
safesearch,
timelimit,
backend,
max_results,
):
results.append(r)
)
return results
except Exception as ex:
logging.warning(ex)
return results


@app.get("/images")
Expand Down Expand Up @@ -202,10 +200,9 @@ async def ddg_images_search(
) -> list[DdgImagesOut]:
"""DuckDuckGo images search."""

results = []
try:
async with AsyncDDGS(proxies=PROXY, timeout=TIMEOUT) as ddgs:
async for r in ddgs.images(
results = await ddgs.images(
q,
region,
safesearch,
Expand All @@ -216,11 +213,10 @@ async def ddg_images_search(
layout,
license_image,
max_results,
):
results.append(r)
)
return results
except Exception as ex:
logging.warning(ex)
return results


@app.get("/videos")
Expand All @@ -244,10 +240,9 @@ async def ddg_videos_search(
) -> list[DdgVideosOut]:
"""DuckDuckGo videos search."""

results = []
try:
async with AsyncDDGS(proxies=PROXY, timeout=TIMEOUT) as ddgs:
async for r in ddgs.videos(
results = await ddgs.videos(
q,
region,
safesearch,
Expand All @@ -256,11 +251,10 @@ async def ddg_videos_search(
duration,
license_videos,
max_results,
):
results.append(r)
)
return results
except Exception as ex:
logging.warning(ex)
return results


@app.get("/news")
Expand All @@ -279,20 +273,18 @@ async def ddg_news_search(
) -> list[DdgNewsOut]:
"""DuckDuckGo news search"""

results = []
try:
async with AsyncDDGS(proxies=PROXY, timeout=TIMEOUT) as ddgs:
async for r in ddgs.news(
results = await ddgs.news(
q,
region,
safesearch,
timelimit,
max_results,
):
results.append(r)
)
return results
except Exception as ex:
logging.warning(ex)
return results


if __name__ == "__main__":
Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
duckduckgo_search==3.9.9
fastapi==0.104.1
orjson==3.9.10
uvicorn[standard]==0.24.0
duckduckgo_search==5.1.0
fastapi==0.110.0
orjson==3.9.15
uvicorn[standard]==0.28.0

0 comments on commit 21bcc0a

Please sign in to comment.