From d385ca0de2a683798b85bc0ca1256d2c99973d77 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 24 Jan 2018 00:56:01 -0500 Subject: [PATCH] Replace bin/duckduckgo with more general bin/sup --- bin/duckduckgo | 9 -- bin/sup | 251 +++++-------------------------------------------- 2 files changed, 26 insertions(+), 234 deletions(-) delete mode 100755 bin/duckduckgo mode change 100644 => 100755 bin/sup diff --git a/bin/duckduckgo b/bin/duckduckgo deleted file mode 100755 index 81877e5c..00000000 --- a/bin/duckduckgo +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env bash - -# Runs a duckduckgo.com search. -# -# Requires: uri (script in the same folder) - -query=$(uri <<< "$*") -echo "Opening https://duckduckgo.com/?q=$query" -open "https://duckduckgo.com/?q=$query" diff --git a/bin/sup b/bin/sup old mode 100644 new mode 100755 index 1663c99c..bce09803 --- a/bin/sup +++ b/bin/sup @@ -1,233 +1,34 @@ #!/usr/bin/env zsh -# Upload to various file hosts -# colors -c0=$(tput sgr0) -c1=$(tput setaf 1) -c2=$(tput setaf 2) -# filename to use for screenshots -local_temp=$(mktemp) +# Performs an online search. Uses the last used provider, otherwise prompts for +# one with fzf. -# screenshot functions -cap_fullscreen() { - local_file="$local_temp.png" - mv $local_temp $local_file - maim -u $local_file -} +provider="$1"; shift +query="$*" -cap_selection() { - local_file="$local_temp.png" - mv $local_temp $local_file - maim -s -u $local_file -} +typeset -A providers +providers+=('DuckDuckGo' 'https://duckduckgo.com/?q=%s') +providers+=('DevDocs.io' 'http://devdocs.io/#q=%s') +providers+=('Google search' 'https://google.com/search?q=%s') +providers+=('Google images' 'https://google.com/images?q=%s') +providers+=('Google maps' 'https://maps.google.com/maps?q=%s') +providers+=('Project Gutenberg' 'http://www.gutenberg.org/ebooks/search/?query=%s') +providers+=('StackOverflow' 'https://stackoverflow.com/search?q=%s') +providers+=('Github' 'https://github.com/search?ref=simplesearch&q=%s') +providers+=('Youtube' 'https://youtube.com/results?aq=f&oq=&search_query=%s') +providers+=('Wolfram alpha' 'https://wolframalpha.com/input/?i=%s') +providers+=('Wikipedia' 'https://wikipedia.org/search-redirect.php?language=en&go=Go&search=%s') -cap_window() { - mv $local_temp $local_temp.png - maim -i $(xprop -root _NET_ACTIVE_WINDOW | grep -o '0x.*') --hidecursor -} - -# delay -delay_seconds='0' -# hosts and shorteners -host='uguu' -shortener= -hosts='uguu teknik 0x0 mixtape ptpb lewd' -shorteners='waaai 0x0' - -## -usage() { - cat <0; --i )); do - echo "$i..." - sleep 1 - done -} - -screenshot() { - if [[ -n $doFullscreen ]]; then - cap_fullscreen - elif [[ -n $doSelection ]]; then - cap_selection &>/dev/null - if ! [[ -s $local_file ]]; then - $(rm "$local_file" 2> /dev/null) - exit - fi - elif [[ -n $doWindow ]]; then - cap_window - elif [[ -n $doURL ]]; then - local urlext - if [[ -f /usr/share/mime/globs ]]; then - urlext=$(curl -sf --head "${remoteURL}" | grep 'Content-Type: ' | head -1 | grep -Po '(?<=\ )[^\;]*') - urlext=$(echo "${urlext}" | sed -e "s/\\r//") - urlext=$(cat /usr/share/mime/globs | grep "${urlext}" | sort -r | head -1 | grep -Po '(?<=\.)[^\n]*') - else - urlext="$(basename ${remoteURL})" - urlext=${urlext#*.} - fi - local_file="${local_temp}.${urlext}" - $(curl -sf "${remoteURL}" > "${local_file}") - fi -} - -upload() { - for (( i = 1; i <= 3; i++ )); do - echo -n "Try #${i}... " - - case "${host}" in - teknik) hostURL='https://api.teknik.io/v1/Upload' ;; - 0x0) hostURL='https://0x0.st/' ;; - uguu) hostURL='https://uguu.se/api.php?d=upload-tool' ;; - ptpb) hostURL='https://ptpb.pw/' ;; - mixtape) hostURL='https://mixtape.moe/upload.php' ;; - lewd) hostURL='https://lewd.se/api.php?d=upload-tool' ;; - esac - - case "${shortener}" in - waaai) shortenerURL='https://api.waa.ai/shorten' ;; - 0x0) shortenerURL='http://0x0.st/' ;; - ptpb) shortenerURL='https://ptpb.pw/u' - esac - - if [ "${host}" = 'uguu' ]; then - uploadResult="$(curl -sf -F file="@${local_file}" "${hostURL}")" - elif [ "${host}" = '0x0' ]; then - uploadResult="$(curl -sf -F file="@${local_file}" "${hostURL}")" - elif [ "${host}" = 'teknik' ]; then - uploadResult="$(curl -sf -F file="@${local_file}" "${hostURL}")" - uploadResult="${uploadResult##*url\":\"}" - uploadResult="${uploadResult%%\"*}" - elif [ "${host}" = 'mixtape' ]; then - uploadResult="$(curl -sf -F files[]="@${local_file}" "${hostURL}")" - uploadResult="$(echo "${uploadResult}" | grep -Eo '"url":"[A-Za-z0-9]+.*",' | sed 's/"url":"//;s/",//')" - uploadResult="$(echo "${uploadResult//\\\//\/}")" - elif [ "${host}" = 'ptpb' ]; then - uploadResult="$(curl -sf -F c="@${local_file}" "${hostURL}")" - uploadResult="${uploadResult##*url: }" - uploadResult="${uploadResult%%$'\n'*}" - elif [ "${host}" = 'lewd' ]; then - uploadResult="$(curl -sf -F file="@${local_file}" "${hostURL}")" - fi - - if [ "${shortener}" = 'waaai' ]; then - tempResult="$(curl -sf -F url="${uploadResult}" "${shortenerURL}")" - shortCode="${tempResult##*short_code\":\"}" - shortCode="${shortCode%%\"*}" - shortenerResult="https://waa.ai/${shortCode}" - shortenerExtension="${tempResult##*extension\":}" - shortenerExtension="${shortenerExtension%%\}*}" - if [[ $shortenerExtension == true ]]; then - shortenerExtension=${shortenerExtension##\"} - shortenerExtension=${shortenerExtension%%\"} - shortenerResult="${shortenerResult}.${shortenerExtension}" - fi - elif [[ $shortener == '0x0' ]]; then - shortenerResult="$(curl -sf -F shorten="${uploadResult}" "${shortenerURL}")" - elif [[ $shortener == 'ptpb' ]]; then - shortenerResult="$(curl -sf -F c=@- "${shortenerURL}" <<< "${uploadResult}")" - fi - - if [[ -n $shortener ]]; then - finalResult="$shortenerResult" - else - finalResult="$uploadResult" - fi - - if [[ $? == 0 ]]; then - # copy to clipboard, log, and notify (unless $noComms is set) - if [ ! "${noComms}" ]; then - echo -n "${finalResult}" | xclip -selection primary - echo -n "${finalResult}" | xclip -selection clipboard - echo "$(date +"%D %R") | "${file}" | "${finalResult}"" >> ~/.uguush.log - notify-send 'uguu~' "${finalResult}" - fi - fi - - echo "[${c2}OK${c0}]" - echo "File has been uploaded: ${finalResult}" - - # if we took a screenshot, remove the temporary file - if [[ -z $doFile ]]; then - rm $local_file - fi - - exit - done -} - -path() { - if [[ $saveToPath == true ]]; then - cp $local_file "$pathToSave/$(basename "$local_file")" - fi -} - - -## -while getopts :d:fho:p:su:wxS:l: opt; do - case $opt in - d) # set delay value - delay_seconds="$OPTARG" ;; - f) # take fullscreen shot - doFullscreen='true' ;; - h) # print help - usage - exit 0 ;; - o) # set host - [[ "${hosts}" =~ "$OPTARG" ]] && host="$OPTARG" || exit 1 ;; - s) # take shot of selection - doSelection='true' ;; - u) # change $file to the specified file with -u - doFile='true' - local_file="$OPTARG" ;; - w) # take shot of current window - doWindow='true' ;; - x) # do not notify dbus, update log, or modify clipboard - noComms='true' ;; - S) # set shortener - [[ $shorteners =~ "$OPTARG" ]] && shortener="$OPTARG" || exit 1 ;; - l) # set url to upload - doURL='true' - remoteURL="${OPTARG}" ;; - p) # set path to save file - saveToPath='true' - pathToSave="${OPTARG}" ;; - *) # print help and EXIT_FAILURE - usage - exit 1 ;; - esac -done - -# show usage if no arguments are given -if (( $# < 1 )); then - usage - exit 1 +pick=$(printf "%s\n" "${(@k)providers}" | fzf --query="$provider" --prompt='provider > ' -1 -0) +if [[ -z $pick ]]; then + >&2 echo 'No provider selected. Aborting.' + exit 1 +elif [[ -z ${providers[$pick]} ]]; then + >&2 echo 'No provider matched that query. Aborting.' + exit 2 fi -delay -screenshot -path -upload +url=$(printf "${providers[$pick]}\n" $(uri<<<"$query")) -# if the program doesn't exit at the for-loop, the upload failed ->&2 echo 'File was not uploaded, did you specify a valid filename?' -exit 1 +echo "Opening $url" +open $url