Repository navigation
Expand file tree
/
Copy pathsearch.sh
More file actions
executable file
·156 lines (137 loc) · 4.73 KB
/
Copy pathsearch.sh
File metadata and controls
executable file
·156 lines (137 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env bash
# Usage: search.sh "query" [max_results]
# Returns JSON array of {title, url, snippet} from DuckDuckGo Lite
# Uses lightpanda (headless browser) with curl fallback
set -uo pipefail
QUERY="${1:?Usage: search.sh QUERY [max_results]}"
MAX="${2:-3}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
LIGHTPANDA="$SCRIPT_DIR/lightpanda"
DDG_URL="https://lite.duckduckgo.com/lite/"
UA="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36"
# ── Fetch DDG results ─────────────────────────────────
encoded_query="$(printf '%s' "$QUERY" | sed 's/ /+/g')"
if [ -x "$LIGHTPANDA" ]; then
raw="$("$LIGHTPANDA" fetch --dump markdown "${DDG_URL}?q=${encoded_query}" 2>/dev/null)" || raw=""
use_markdown=true
else
raw="$(curl -s --max-time 10 -A "$UA" -d "q=$QUERY" "$DDG_URL" 2>/dev/null)"
use_markdown=false
fi
if [ -z "$raw" ]; then
echo '[]'
exit 0
fi
# ── Parse markdown output (lightpanda) ────────────────
if [ "$use_markdown" = true ]; then
# Replace non-breaking spaces (U+00A0) with regular spaces
NBSP="$(printf '\xc2\xa0')"
echo "$raw" | sed "s/$NBSP/ /g" | awk -v max="$MAX" '
BEGIN { n=0; ORS=""; waiting_snippet=0 }
# Match numbered result lines: | 1. | [Title](url) |
/^\| [0-9]+\./ {
s = $0
if (match(s, /\[([^\]]+)\]\(([^)]+)\)/)) {
m = substr(s, RSTART, RLENGTH)
split_pos = index(m, "](")
title = substr(m, 2, split_pos - 2)
url = substr(m, split_pos + 2, length(m) - split_pos - 2)
}
# Extract real URL from DDG redirect
if (match(url, /uddg=[^&]+/)) {
encoded = substr(url, RSTART+5, RLENGTH-5)
gsub(/%2F/, "/", encoded)
gsub(/%3A/, ":", encoded)
gsub(/%23/, "#", encoded)
gsub(/%3F/, "?", encoded)
gsub(/%3D/, "=", encoded)
gsub(/%26/, "\\&", encoded)
url = encoded
}
# Strip markdown escapes from title
gsub(/\\-/, "-", title)
gsub(/\\\|/, "|", title)
# Skip DDG ads
if (url ~ /duckduckgo\.com\/y\.js/) { url=""; title=""; next }
waiting_snippet = 1
next
}
# Snippet: first content line after title (skip separator and domain lines)
waiting_snippet && /^\|/ {
# Skip separator lines |---|---|
if ($0 ~ /^[|[:space:]-]+$/) next
s = $0
# Remove table markup
gsub(/^\|[^|]*\|/, "", s)
gsub(/\|[[:space:]]*$/, "", s)
gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
# Skip empty lines
if (s == "") next
# Skip domain-only lines (no spaces = just a URL)
if (s !~ / / && s ~ /\./) next
# Strip markdown bold and escapes
gsub(/\*\*/, "", s)
gsub(/\\-/, "-", s)
gsub(/\\\|/, "|", s)
snippet = s
waiting_snippet = 0
# JSON-escape
gsub(/\\/, "\\\\", title); gsub(/"/, "\\\"", title)
gsub(/\\/, "\\\\", snippet); gsub(/"/, "\\\"", snippet)
gsub(/\\/, "\\\\", url); gsub(/"/, "\\\"", url)
if (n == 0) print "["
else print ","
printf "{\"title\":\"%s\",\"url\":\"%s\",\"snippet\":\"%s\"}", title, url, snippet
n++
title=""; url=""; snippet=""
if (n >= max) exit
}
END {
if (n == 0) print "[]"
else print "]"
}
' 2>/dev/null
exit 0
fi
# ── Parse HTML output (curl fallback) ─────────────────
echo "$raw" | awk -v max="$MAX" '
BEGIN { n=0; in_sponsored=0; title=""; url=""; ORS="" }
/<tr class="result-sponsored">/ { in_sponsored=1; next }
/class=.result-link./ && !in_sponsored {
s = $0
if (match(s, /href="[^"]+"/)) {
url = substr(s, RSTART+6, RLENGTH-7)
}
if (match(s, /result-link.>[^<]*/)) {
t = substr(s, RSTART, RLENGTH)
idx = index(t, ">")
if (idx > 0) title = substr(t, idx+1)
}
if (url ~ /duckduckgo\.com/ || url ~ /^$/) { url=""; title=""; next }
}
/class=.result-snippet./ && url != "" {
getline snippet_line
gsub(/^[[:space:]]+|[[:space:]]+$/, "", snippet_line)
gsub(/<[^>]+>/, "", snippet_line)
gsub(/'/, "'\''", snippet_line); gsub(/'/, "'\''", title)
gsub(/\/, "", snippet_line); gsub(/\/, "", title)
gsub(/&/, "\\&", snippet_line); gsub(/&/, "\\&", title)
gsub(/</, "<", snippet_line); gsub(/</, "<", title)
gsub(/>/, ">", snippet_line); gsub(/>/, ">", title)
gsub(/\\/, "\\\\", snippet_line); gsub(/\\/, "\\\\", title)
gsub(/"/, "\\\"", snippet_line); gsub(/"/, "\\\"", title); gsub(/"/, "\\\"", url)
if (n == 0) print "["
else print ","
printf "{\"title\":\"%s\",\"url\":\"%s\",\"snippet\":\"%s\"}", title, url, snippet_line
n++
url=""; title=""
in_sponsored=0
if (n >= max) exit
}
/<\/tr>/ { in_sponsored=0 }
END {
if (n == 0) print "[]"
else print "]"
}
' 2>/dev/null
exit 0