-
Notifications
You must be signed in to change notification settings - Fork 0
/
searx
executable file
·94 lines (76 loc) · 2.12 KB
/
searx
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
#!/usr/bin/tclsh
package require cmdline
package require http
package require tls
package require json
package require term::ansi::send
term::ansi::send::import vt
vt::init
set options {
{p.arg 1 "Search page number"}
{e.arg "" "Comma-separated list of engines to query"}
{c.arg 72 "Wrap column for description"}
{nd "Do not display descriptions"}
{n.arg 10 "Limit number of displayed results"}
}
set usage ": searx \[options] query ...\noptions:"
try {
array set params [::cmdline::getoptions argv $options $usage]
} trap {CMDLINE USAGE} {msg o} {
vt::wr "$msg\n"
exit 1
}
if {[info exists ::env(SEARX_ENDPOINT)]} {
set searxEndpoint $env(SEARX_ENDPOINT)
} {
vt::wr "Enviromental variable SEARX_ENDPOINT must be defined:\n\n"
vt::wr " export SEARX_ENDPOINT=https://example.com/search\n\n"
exit 1
}
set query "$argv"
if { $query == "" } {
vt::wr "The query is empty\n\n"
vt::wr "[::cmdline::usage $options $usage]"
exit 1
}
set wrap $params(c)
set engines $params(e)
set maxResults $params(n)
set showDesc [expr {$params(nd) !=1}]
proc wordwrap {max msg} {
if { [string length $msg] > $max } {
regsub -all "(.{1,$max})( +|$)" $msg "\\1\\3\n" msg
}
return $msg
}
set queryArgs [list q ${query} format json]
if {$engines != ""} {
lappend queryArgs engines $engines
}
http::register https 443 tls::socket
set httpToken [::http::config -urlencoding utf-8]
set httpToken [::http::geturl "${searxEndpoint}?[::http::formatQuery {*}${queryArgs}]"]
set reply [::json::json2dict [::http::data $httpToken]]
http::cleanup $httpToken
http::unregister https
set resultN 1
foreach result [dict get $reply results] {
vt::sda_bold
vt::wr "$resultN: [dict get $result title]\n"
vt::sda_nobold
if {$showDesc &&
[dict exists $result content] &&
[dict get $result content] != ""} {
vt::wr "[wordwrap $wrap [dict get $result content]]"
}
vt::sda_underline
vt::wr "[dict get $result url]"
vt::sda_nounderline
vt::wr "\n\n"
if { $resultN >= $maxResults } {
break
} {
incr resultN
}
}
vt::sda_reset