Skip to content

Instantly share code, notes, and snippets.

@sheepla
Last active March 18, 2024 03:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheepla/205a5608de97fd3f4e9b342129ce74aa to your computer and use it in GitHub Desktop.
Save sheepla/205a5608de97fd3f4e9b342129ce74aa to your computer and use it in GitHub Desktop.
Web検索のサジェストキーワードを取得する.sh
#!/bin/sh
# suggest.sh -- get suggestion keywords from several site
#
# Usage:
# suggest SITE KEYWORDS...
# SITE:
# youtube(yt), archwiki(aw), duckduckgo(d), wikipedia(w), amazon(a)
# Example:
# suggest wikipedia Linux
# Dependences:
# curl, jq
# License: WTFPL
_urlencode() {
printf "%s" "${*}" | od -tx1 -An | tr ' ' % | tr -d \\n
}
_duckduckgo() {
keyword="${*}"
lang="ja"
curl -s "https://ac.duckduckgo.com/ac/?q=$(_urlencode "${keyword}")&type=list&kl=${lang}" | jq -r '.[1][]'
}
_youtube() {
keyword="${*}"
lang="ja"
curl -s "https://suggestqueries.google.com/complete/search?client=chrome&ds=yt&q=$(_urlencode "${keyword}")" |
jq -r '.[1] | .[]'
}
_archwiki() {
keyword="${*}"
limit=100
curl -s "https://wiki.archlinux.org/api.php?action=opensearch&format=json&formatversion=2&search=$(_urlencode "${keyword}")&namespace=0%7C3000&limit=${limit}&suggest=true" |
jq -r '.[1][]'
}
_wikipedia() {
keyword="${*}"
lang="ja"
limit=100
curl -s "https://${lang}.wikipedia.org/w/api.php?action=query&format=json&generator=prefixsearch&prop=pageprops|pageimages|description&redirects=&ppprop=displaytitle&piprop=thumbnail&pithumbsize=80&pilimit=6&gpssearch=$(_urlencode "${keyword}")&gpsnamespace=0&gpslimit=${limit}" |
jq -r '.query["pages"][] | "\(.title) - \(.description) "'
}
_amazon() {
keyword="${*}"
curl -s "https://completion.amazon.com/search/complete?search-alias=aps&mkt=1&q=$(_urlencode "${keyword}")" |
jq -r '.[1][]'
}
case "${1}" in
"youtube" | "yt")
shift 1
_youtube "${@}"
exit 0
;;
"archwiki" | "aw")
shift 1
_archwiki "${@}"
exit 0
;;
"duckduckgo" | "ddg")
shift 1
_duckduckgo "${@}"
exit 0
;;
"wikipedia" | "w")
shift 1
_wikipedia "${@}"
exit 0
;;
"amazon" | "a")
shift 1
_amazon "${@}"
exit 1
;;
*)
echo "Site not found" >&2
exit 1
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment