add human estimation conditions

This commit is contained in:
ipu 2025-09-08 18:47:01 +03:00
parent 735ccb757c
commit c8e6474edd
3 changed files with 103 additions and 57 deletions

View file

@ -135,5 +135,5 @@ async def fetch_drug_with_dosage(drug_name: str, dosage: float) -> DrugFull | No
if drug_full:
return drug_full
raise Exception(f"Drug {drug_name} with dosage {dosage} not found")
return None

View file

@ -102,7 +102,7 @@ def fetch_drug(drug_name: str) -> DrugPriceResponse:
return result
def search_drug(drug_name: str) -> str:
def search_drug(drug_name: str) -> str | None:
cache_key = f"drug_search:{drug_name}"
redis_client = get_redis_client()
@ -113,15 +113,18 @@ def search_drug(drug_name: str) -> str:
except Exception:
pass
client = httpx.Client(
base_url="https://www.drugs.com/api/autocomplete",
# headers=headers,
timeout=httpx.Timeout(60.0, connect=10.0)
)
response = client.get(f"/?type=price-guide&s={drug_name}")
response_json = response.json()
try:
client = httpx.Client(
base_url="https://www.drugs.com/api/autocomplete",
# headers=headers,
timeout=httpx.Timeout(60.0, connect=10.0)
)
response = client.get(f"/?type=price-guide&s={drug_name}")
response_json = response.json()
result = response_json["categories"][0]["results"][0]["url"].replace("/price-guide/", "")
result = response_json["categories"][0]["results"][0]["url"].replace("/price-guide/", "")
except Exception:
return None
try:
redis_client.setex(cache_key, settings.REDIS_CACHE_TTL_DRUGS, result)