Refactor bin/{duckduckgo,urlencode}

This commit is contained in:
Henrik Lissner 2017-04-25 02:58:25 -04:00
parent 28ed7298a7
commit d16f0d14af
2 changed files with 10 additions and 16 deletions

View file

@ -1,16 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
urlencode() { query=$(urlencode <<< "$*")
local LANG=C
for ((i=0;i<${#1};i++)); do
if [[ ${1:$i:1} =~ ^[a-zA-Z0-9\.\~\_\-]$ ]]; then
printf "${1:$i:1}"
else
printf '%%%02X' "'${1:$i:1}"
fi
done
}
query=$(urlencode "$*")
echo "Opening https://duckduckgo.com/?q=$query" echo "Opening https://duckduckgo.com/?q=$query"
open "https://duckduckgo.com/?q=$query" open "https://duckduckgo.com/?q=$query"

View file

@ -1,7 +1,12 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, urllib import sys
if os.isatty(file.fileno(sys.stdin)): try:
text = sys.argv[1] from urllib.parse import quote_plus
except ImportError:
from urllib import quote_plus
if sys.argv[1:]:
text = " ".join(sys.argv[1:])
else: else:
text = "".join(sys.stdin.readlines()) text = "".join(sys.stdin.readlines())
print(urllib.quote_plus(text.strip())) print(quote_plus(text.strip()))