aboutsummaryrefslogtreecommitdiffstats
path: root/tools/search_wikipedia.sh
blob: 6e08614ce3bacd2faef6daf081e0f5ac0f900854 (plain) (blame)
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
#!/usr/bin/env bash
set -e

# @describe Search Wikipedia for a query.
# Uses it to get detailed information about a public figure, interpretation of a complex scientific concept or in-depth connectivity of a significant historical event,.

# @option --query! The query to search for.

main() {
    encoded_query="$(jq -nr --arg q "$argc_query" '$q|@uri')"
    search_url="https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=$encoded_query&format=json"
    title="$(curl -fsSL "$search_url" | jq -r '.query.search[0].title' 2>/dev/null)"
    if [[ -z "$title" ]]; then
        echo "Error: No results found for '$argc_query'"
        exit 1
    fi
    title="$(echo "$title" | tr ' ' '_')"
    page_url="https://en.wikipedia.org/api/rest_v1/page/summary/$title"
    summary="$(curl -fsSL "$page_url"  | jq -r '.extract')"
    echo '{
    "link": "https://en.wikipedia.org/wiki/'"$title"'",
    "summary": "'"$summary"'"
}' >> "$LLM_OUTPUT"
}

eval "$(argc --argc-eval "$0" "$@")"