Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ud_perfectish ()
- {
- term="$@";
- sterm=$(echo $term | sed -e 's/ /%20/g');
- jq_icase_filter=$(generate_permutations "${sterm}" | tr '{}' '[]' | tr -d ',');
- function get_count ()
- {
- export num=0 check="init";
- until [[ -z "${check}" ]]; do
- let num++;
- export cpage="&page=${num}";
- export check="$(curl -skL "https://api.urbandictionary.com/v0/define?term=${sterm}${cpage}" | jq -M -r '.list[]')";
- done && export num=$((${num}-1))
- };
- get_count;
- ( p=0;
- l=-1;
- declare -a ud_list;
- for i in $(seq 1 1 ${num});
- do
- let p++;
- if [[ $p -gt 1 ]]; then
- export pg="&page=${i}";
- fi;
- curl "https://api.urbandictionary.com/v0/define?term=${sterm}${pg}" -s | jq -M -r '.list[] |select(.word|match("^'${jq_icase_filter}'$")) | (.defid|tostring) + " " + (.thumbs_up|tostring) + " " + (.thumbs_down|tostring) + " " + .word';
- done ) | cat -n | sort -nk 3 | egrep -i "\b${term}\b$" | tail -1 | while read pnum defid tup tdwn rterm; do
- let l++;
- ud_list[${l}]=$(declare -p pnum defid tup tdwn rterm);
- declare -p ud_list;
- if [[ $tup -gt $tdwn ]] && [[ -n $(echo "${term}" | grep -i "${rterm}") ]]; then
- curl -skL "https://api.urbandictionary.com/v0/define?defid=$defid" | jq -r -M '.list[].definition';
- declare -p pnum defid tup tdwn rterm;
- else
- if [[ ${tup} -gt ${tdwn} ]]; then
- echo "${Yellow}Warning:${White} logic caused definition selected based on number of thumbs ups not to be a perfect match. Definition for ${Cyan}${rterm}:${White}";
- curl -skL "https://api.urbandictionary.com/v0/define?defid=$defid" | jq -r -M '.list[].definition';
- else
- echo "Thumbsdown outweigh thumbs up or rterm doesnt match term, please verify manually.";
- echo -e "${pnum} ${defid} ${tup} ${tdwn} ${rterm}";
- echo -e "term is $(printf "%s\n" "${term}")\nrterm is $(printf "%s\n" "${rterm}")";
- echo -e "Here is the list of definitions you must sort through.";
- set +x;
- p=0;
- for fail in $(seq 1 1 ${num});
- do
- let p++;
- if [[ $p -gt 1 ]]; then
- export pg="&page=${fail}";
- fi;
- curl "https://api.urbandictionary.com/v0/define?term=${sterm}${pg}" -s | jq -M -r '.list[] |select(.word|match("^'${jq_icase_filter}'$")) | "https://api.urbandictionary.com/v0/define?defid=" + (.defid|tostring) + " " + (.thumbs_up|tostring) + " " + (.thumbs_down|tostring) + " " + .word';
- done;
- set +x;
- fi;
- fi;
- done;
- set +x
- }
Advertisement
Add Comment
Please, Sign In to add comment