You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
1.7 KiB

7 years ago
#!/usr/bin/env bash
declare -r esc=$'\033'
declare -r c_reset="${esc}[0m"
declare -r c_red="${esc}[31m"
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
[[ -n "$1" ]] && err "$1"
7 years ago
exit 1
}
has() {
6 years ago
local verbose=0
if [[ $1 == '-v' ]]; then
6 years ago
verbose=1
shift
fi
6 years ago
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
6 years ago
(( verbose > 0 )) && err "$c not found"
return 1
fi
done
}
7 years ago
openurl() {
local url="$1"
local browser
case "$url" in
*youtube.com*|*youtu.be*|*vimeo.com*)
6 years ago
browser=$(select_from \
7 years ago
mpv mplayer2 mplayer) ;;
*)
6 years ago
browser=$(select_from \
7 years ago
-c "pgrep -u $USER -i" \
firefox chromium-browser chrome)
[[ ! $browser ]] && browser='w3m'
;;
esac
$browser "$url"
}
# http://stackoverflow.com/a/10660730
rawurlencode() {
local string="$*"
local strlen=${#string}
local encoded=""
for (( pos=0; pos < strlen; pos++ )); do
c="${string:$pos:1}"
case "$c" in
[-_.~a-zA-Z0-9])
o="$c" ;;
*)
printf -v o '%%%02x' "'$c"
esac
encoded+="${o}"
done
echo "${encoded}"
}
has -v curl jq fzf || die
if [[ -z "$*" ]]; then
die 'nothing to search for'
fi
response=$( curl -sfL "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=8&q=$(rawurlencode "$*")" ) || die 'error connecting to google, check connection'
json=$( jq -c '.["responseData"]["results"] | map("\(.unescapedUrl) \(.title) | \(.content)") | .[]' <<< "$response" ) || die 'error parsing results :('
url=$( fzf -e --ansi --cycle --inline-info <\
<( sed -r 's/<[^>]*>//g; s/\\n//g; s/^\s*"//; s/",?$//' <<< "$json" ) |
cut -d' ' -f1 )
if [[ -n "$url" ]]; then
openurl "$url"
fi