Skip to content
Merged
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
46 changes: 46 additions & 0 deletions commands/web-searches/bnf-search.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Required parameters:
# @raycast.schemaVersion 1
# @raycast.title BNF Search
# @raycast.mode silent

# Optional parameters:
# @raycast.icon 💊
# @raycast.packageName Medical
# @raycast.argument1 { "type": "dropdown", "placeholder": "Source", "data": [{"title": "BNF (Adults)", "value": "bnf"}, {"title": "BNFC (Children)", "value": "bnfc"}] }
# @raycast.argument2 { "type": "text", "placeholder": "Medication (e.g. Paracetamol)" }

# Documentation:
# @raycast.description Search the British National Formulary (BNF) or BNFC directly.
# @raycast.author Jack Smith
# @raycast.authorURL https://github.com/myusualonewastaken

SOURCE="$1"
INPUT="$2"

if [ "$SOURCE" == "bnfc" ]; then
BASE_DOMAIN="bnfc.nice.org.uk"
else
BASE_DOMAIN="bnf.nice.org.uk"
fi

# Clean up input: Lowercase, replace spaces with dashes
SLUG=$(echo "$INPUT" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')

DIRECT_URL="https://$BASE_DOMAIN/drugs/$SLUG/"
SEARCH_URL="https://$BASE_DOMAIN/search?q=$INPUT"

# Check URL with a fake User-Agent to avoid being blocked
# -o /dev/null: Ignore the page content
# --silent: Don't show loading bars
# --head: Only check the headers
# --write-out '%{http_code}': Tell us the status code (e.g., 200 or 404)
STATUS=$(curl -o /dev/null --silent --head --write-out '%{http_code}' -A "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)" "$DIRECT_URL")

# If the status is 200 (OK), open the direct page. Otherwise, search.
if [ "$STATUS" -eq 200 ]; then
open "$DIRECT_URL"
else
open "$SEARCH_URL"
fi