Skip to content

Commit 68341e8

Browse files
myusualonewastakendehesa
authored andcommitted
Create bnf-search.sh
1 parent 273f2e3 commit 68341e8

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
3+
# Required parameters:
4+
# @raycast.schemaVersion 1
5+
# @raycast.title BNF Search
6+
# @raycast.mode silent
7+
8+
# Optional parameters:
9+
# @raycast.icon 💊
10+
# @raycast.packageName Medical
11+
# @raycast.argument1 { "type": "dropdown", "placeholder": "Source", "data": [{"title": "BNF (Adults)", "value": "bnf"}, {"title": "BNFC (Children)", "value": "bnfc"}] }
12+
# @raycast.argument2 { "type": "text", "placeholder": "Medication (e.g. Paracetamol)" }
13+
14+
# Documentation:
15+
# @raycast.description Search the British National Formulary (BNF) or BNFC directly.
16+
# @raycast.author Jack Smith
17+
# @raycast.authorURL https://github.com/myusualonewastaken
18+
19+
SOURCE="$1"
20+
INPUT="$2"
21+
22+
if [ "$SOURCE" == "bnfc" ]; then
23+
BASE_DOMAIN="bnfc.nice.org.uk"
24+
else
25+
BASE_DOMAIN="bnf.nice.org.uk"
26+
fi
27+
28+
# Clean up input: Lowercase, replace spaces with dashes
29+
SLUG=$(echo "$INPUT" | tr '[:upper:]' '[:lower:]' | tr ' ' '-')
30+
31+
DIRECT_URL="https://$BASE_DOMAIN/drugs/$SLUG/"
32+
SEARCH_URL="https://$BASE_DOMAIN/search?q=$INPUT"
33+
34+
# Check URL with a fake User-Agent to avoid being blocked
35+
# -o /dev/null: Ignore the page content
36+
# --silent: Don't show loading bars
37+
# --head: Only check the headers
38+
# --write-out '%{http_code}': Tell us the status code (e.g., 200 or 404)
39+
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")
40+
41+
# If the status is 200 (OK), open the direct page. Otherwise, search.
42+
if [ "$STATUS" -eq 200 ]; then
43+
open "$DIRECT_URL"
44+
else
45+
open "$SEARCH_URL"
46+
fi

0 commit comments

Comments
 (0)