Refactor all uri handling to be more extendable and readable. - annna - Annna the nice friendly bot. HTML git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/annna/ DIR Log DIR Files DIR Refs DIR Tags DIR README --- DIR commit 0f7e3893de03907ca5ba18f8462895a6055ff82c DIR parent c3d834f28259c7860e41365fb87cfcc3777cf973 HTML Author: Annna Robert-Houdin <annna@bitreich.org> Date: Fri, 5 Jun 2020 21:23:35 +0200 Refactor all uri handling to be more extendable and readable. Diffstat: M annna-add-hashtag-mass | 4 ++-- R annna-get-hashtag-url -> annna-get… | 0 M annna-start-services | 183 ++++++++++++++++--------------- M curl-grabtitle | 8 +++++--- A fetch-uri | 15 +++++++++++++++ D fetch-url | 14 -------------- M screenshot-paste | 4 ++-- A screenshot-uri | 29 +++++++++++++++++++++++++++++ D screenshot-url | 29 ----------------------------- M subtitle-paste | 6 +++--- 10 files changed, 151 insertions(+), 141 deletions(-) --- DIR diff --git a/annna-add-hashtag-mass b/annna-add-hashtag-mass @@ -13,7 +13,7 @@ do key="$(printf "%s\n" "${line}" | cut -d' ' -f 1)" value="$(printf "%s\n" "${line}" | cut -d' ' -f 2)" - taguri="$(annna-get-hashtag-url "${key}")" + taguri="$(annna-get-hashtag-uri "${key}")" if [ -z "${taguri}" ]; then @@ -24,7 +24,7 @@ do git commit -a -m "Adding ${key} tag." >&2 >/dev/null git push >&2 >/dev/null - taguri="$(annna-get-hashtag-url "${key}")" + taguri="$(annna-get-hashtag-uri "${key}")" if [ -n "${taguri}" ]; then annna-say -c '#bitreich-en' "Enjoy this new meme tag! ${key}: ${taguri}" DIR diff --git a/annna-get-hashtag-url b/annna-get-hashtag-uri DIR diff --git a/annna-start-services b/annna-start-services @@ -45,12 +45,28 @@ annna_common() { *\#nospoil*) ;; *) - if [ -n "$uri" ]; + if [ -n "${uri}" ] then tmpf=$(mktemp) - fetch-url "${uri}" > "${tmpf}" + fetch-uri "${uri}" > "${tmpf}" urititle="$(grabtitle < "${tmpf}")" - # TODO: Replace all of this with a better logic for parsing global domains. + + # Set below and annna will concatenate at the end. + # Subtitle URI. + sturi="" + # Replacement URI. + nuri="" + # html2text content. + curi="" + # Set to 1, if the URI is not proper for html2text. + nocuri=0 + # Screenshot. + suri="" + + # titleend=1 will not output any further. + titleend=0 + outputstr="" + if [ -n "$urititle" ]; then case "${urititle}" in @@ -59,76 +75,67 @@ annna_common() { ;; *\|\ Cloudflare*) annna-say -c "${channel}" "Mind your own business! We got clownflared: gopher://bitreich.org/0/ascii/clownflare.vt" + titleend=1 ;; - *) - if expr "${uri}" : '^https*://[^/]*youtu\(\.be\|be\..\{2,3\}\)/' + esac + fi + + if expr "${uri}" : '^https*://[^/]*youtu\(\.be\|be\..\{2,3\}\)/' + then + sturi="$(subtitle-paste "${uri}")" + fi + + case "${uri}" in + *twitter.com*) + nuri="$(printf "%s\n" "${uri}" | sed 's;\(mobile\.\)\{0,1\}twitter\.com;nitter\.net;')" + suri="$(screenshot-paste "${nuri}")" + nuri="nitter: ${nuri} " + ;; + *en.wikipedia.org/wiki*) + if ! regeximatch "$uri" '\.(jpg,bmp,gif,png,tiff,avi,mp4,webm,mkv,pdf,ps,eps)$'; + then + nuri="$(printf 'gopherpedia: %s \n' "${uri}" | sed "s;.*en.wikipedia.org/wiki;gopher://gopherpedia.com/0;")" + curi="$(9 htmlfmt < "${tmpf}" | /br/bin/bitreich-paste)" + else + nocuri=1 + fi + ;; + *www.reddit.com*) + nuri="$(printf 'old.reddit: %s \n' "${uri}" | sed "s;www.reddit.com;old.reddit.com;")" + curi="$(9 htmlfmt < "${tmpf}" | /br/bin/bitreich-paste)" + if [ -n "${uri#*www.reddit.com/r/*}" ]; + then + subreddit="$(printf '%s\n' "${uri}" | cut -d / -f 5)" + post="$(printf '%s\n' "${uri}" | cut -d / -f 7)" + if [ -n "${post}" ]; then - spurl="$(subtitle-paste "${uri}")" - if [ -n "${spurl}" ]; - then - annna-say -c "${channel}" "HTML title: ${urititle} ; subtitles: ${spurl}" - else - annna-say -c "${channel}" "HTML title: ${urititle}" - fi + guri="gopher://gopherddit.com/1/cgi-bin/reddit.cgi?view&${post}&${subreddit}&10" else - set -x - wikipedia=0 - case "${uri}" in - *twitter.com*) - nurl="$(printf "%s\n" "${uri}" | sed 's;\(mobile\.\)\{0,1\}twitter\.com;nitter\.net;')" - spurl="$(screenshot-paste "${nurl}")" - annna-say -c "${channel}" "nitter: ${nurl}" - - ;; - *en.wikipedia.org/wiki*) - if ! regeximatch "$uri" '\.(jpg,bmp,gif,png,tiff,avi,mp4,webm,mkv,pdf,ps,eps)$'; then - nurl="$(printf '%s\n' "${uri}" | sed "s;.*en.wikipedia.org/wiki;gopher://gopherpedia.com/0;")" - purl="$(9 htmlfmt < "${tmpf}" | /br/bin/bitreich-paste)" - annna-say -c "${channel}" "gopherpedia: ${nurl} ; content: ${purl}" - # Hack, Hack, Hack. - wikipedia=1 - fi - ;; - *www.reddit.com*) - nurl="$(printf '%s\n' "${uri}" | sed "s;www.reddit.com;old.reddit.com;")" - purl="$(9 htmlfmt < "${tmpf}" | /br/bin/bitreich-paste)" - if [ -n "${uri#*www.reddit.com/r/*}" ]; then - subreddit="$(printf '%s\n' "${uri}" | cut -d / -f 5)" - post="$(printf '%s\n' "${uri}" | cut -d / -f 7)" - if [ -n "${post}" ]; then - gurl="gopher://gopherddit.com/1/cgi-bin/reddit.cgi?view&${post}&${subreddit}&10" - else - gurl="gopher://gopherddit.com/1/cgi-bin/reddit.cgi?menu&${subreddit}&10" - fi - annna-say -c "${channel}" "old.reddit: ${nurl} ; gopherddit: ${gurl} ; content: ${purl}" - else - annna-say -c "${channel}" "old.reddit: ${nurl} ; content: ${purl}" - fi - # Hack, Hack, Hack. - wikipedia=1 - ;; - *) - spurl="" - ;; - esac - set +x - - if [ $wikipedia -eq 0 ]; - then - purl="$(9 htmlfmt < "${tmpf}" | /br/bin/bitreich-paste)" - if [ -n "${spurl}" ]; - then - annna-say -c "${channel}" "content: ${purl} ; screen: ${spurl} ; HTML title: ${urititle}" - else - annna-say -c "${channel}" "content: ${purl} ; HTML title: ${urititle}" - fi - fi + guri="gopher://gopherddit.com/1/cgi-bin/reddit.cgi?menu&${subreddit}&10" fi - ;; - esac + nuri="${nuri}; gopherdit: ${guri} " + fi + ;; + esac + + if [ $titleend -eq 0 ]; + then + if [ $nocuri -eq 0 ]; + then + if [ -z "${curi}" ]; + then + curi="$(9 htmlfmt < "${tmpf}" | /br/bin/bitreich-paste)" + fi + outputstr="${outputstr}content: ${curi} " + fi + + [ -n "${nuri}" ] && outputstr="${outputstr}; ${nuri}" + [ -n "${urititle}" ] && outputstr="${outputstr}; title: ${urititle} " + [ -n "${suri}" ] && outputstr="${outputstr}; screen: ${suri} " + [ -n "${sturi}" ] && outputstr="${outputstr}; subtitles: ${sturi} " + annna-say -c "${channel}" "${outputstr}" fi rm -f "${tmpf}" - return fi ;; esac @@ -144,16 +151,16 @@ annna_common() { case "${word}" in "my horoscope") - purl="$(clippy-say "Did you mean _what's my horoscope?_?")" + puri="$(clippy-say "Did you mean _what's my horoscope?_?")" ;; "my horoscope") - purl="$(clippy-say "Did you mean _what's my future?_?")" + puri="$(clippy-say "Did you mean _what's my future?_?")" ;; "up") - purl="$(clippy-say "Did you mean _what's up?_?")" + puri="$(clippy-say "Did you mean _what's up?_?")" ;; *) - purl="" + puri="" dresult="$(dict "dict://parazyd.org/d:${word}")" derr=$? if [ $derr -eq 20 ]; @@ -164,20 +171,20 @@ annna_common() { fi case "$derr" in 0|21) - purl="$(printf "%s" "${dresult}" | /br/bin/bitreich-paste)" + puri="$(printf "%s" "${dresult}" | /br/bin/bitreich-paste)" ;; 20) - purl="No matches found." + puri="No matches found." ;; 1) - purl="Parse error." + puri="Parse error." ;; *) - purl="Parazyd destroyed the server. Nothing to see here." + puri="Parazyd destroyed the server. Nothing to see here." ;; esac esac - annna-say -c "${channel}" "${purl}" + annna-say -c "${channel}" "${puri}" ;; "${botname}, wolfram is "*) word="$(printf "%s\n" "${text}" | cut -c 19- | sed 's,\t, ,g')" @@ -187,7 +194,7 @@ annna_common() { ;; esac - purl="" + puri="" dresult="$(printf "/wolfram\t%s\r\n" "${word}" \ | nc magical.fish 70 \ | awk '/iAnswer:/,/iAsk/' \ @@ -218,14 +225,14 @@ annna_common() { if [ $wcl -gt 1 ]; then - purl="$(printf "%s" "${dresult}" | /br/bin/bitreich-paste)" + puri="$(printf "%s" "${dresult}" | /br/bin/bitreich-paste)" else - purl="${dresult}" + puri="${dresult}" fi else - purl="No matches found." + puri="No matches found." fi - annna-say -c "${channel}" "${purl}" + annna-say -c "${channel}" "${puri}" ;; "${botname}, textsynth is "*) word="$(printf "%s\n" "${text}" | cut -c 21- | sed 's,\t, ,g')" @@ -240,16 +247,16 @@ annna_common() { dresult="$(textsynth-complete "${word}")" if [ -n "${dresult}" ]; then - purl="$({ + puri="$({ # First the old words. printf "%s" "${word}"; # then the generated text. printf "%s" "${dresult}"; } | /br/bin/bitreich-paste)" else - purl="Textsynth did not answer." + puri="Textsynth did not answer." fi - annna-say -c "${channel}" "textsynth result: ${purl}" + annna-say -c "${channel}" "textsynth result: ${puri}" } & ;; @@ -331,8 +338,8 @@ annna_common() { annna-say -c "${channel}" "${user}, ${horostr}" ;; "${botname}, what's my future?") - purl="$(sacc gopher://parazyd.org/0/tarot.cgi | /br/bin/bitreich-paste)" - annna-say -c "${channel}" "${user}, your future is here: ${purl}" + puri="$(sacc gopher://parazyd.org/0/tarot.cgi | /br/bin/bitreich-paste)" + annna-say -c "${channel}" "${user}, your future is here: ${puri}" ;; "${botname}, how many memes do you know?") annna-say -c "${channel}" "I know $(hashtagcount) memes: gopher://adamsgaard.dk/0/pub/memecount" @@ -341,8 +348,8 @@ annna_common() { annna-say -c "${channel}" "${user}, too many. :( Please see yourself: gopher://magical.fish/1/covid" ;; "${botname}, please show me your memes.") - purl="$(printf "%s" "$(hashtags)" | /br/bin/bitreich-paste)" - annna-say -c "${channel}" "Here are my memes: ${purl}" + puri="$(printf "%s" "$(hashtags)" | /br/bin/bitreich-paste)" + annna-say -c "${channel}" "Here are my memes: ${puri}" ;; "${botname}, please distro-hop with me.") ndistro="$(curl -s 'https://distrowatch.com/dwres.php?resource=popularity' \ DIR diff --git a/curl-grabtitle b/curl-grabtitle @@ -1,9 +1,11 @@ #!/bin/sh export PATH="$HOME/bin:$PATH" -if test x"$1" = x""; then - echo "usage: $0 <url>" >&2 +if [ $# -lt 1 ]; +then + printf "usage: %s\n" "$(basename "$0")" >&2 exit 1 fi -fetch-url "$1" | grabtitle +fetch-uri "$1" | grabtitle + DIR diff --git a/fetch-uri b/fetch-uri @@ -0,0 +1,15 @@ +#!/bin/sh + +if [ $# -lt 1 ]; +then + printf "usage: %s\n" "$(basename "$0")" >&2 + exit 1 +fi + +curl \ + --preproxy socks5://127.0.0.1:9100 \ + -s \ + -L --max-redirs 3 \ + -m 5 \ + -H 'User-Agent:' \ + "$1" 2>/dev/null DIR diff --git a/fetch-url b/fetch-url @@ -1,14 +0,0 @@ -#!/bin/sh - -if test x"$1" = x""; then - echo "usage: $0 <url>" >&2 - exit 1 -fi - -curl \ - --preproxy socks5://127.0.0.1:9100 \ - -s \ - -L --max-redirs 3 \ - -m 5 \ - -H 'User-Agent:' \ - "$1" 2>/dev/null DIR diff --git a/screenshot-paste b/screenshot-paste @@ -4,11 +4,11 @@ export PATH="/home/annna/bin:$PATH" if [ $# -lt 1 ]; then - printf "usage: %s url\n" "$(basename "$0")" >&2 + printf "usage: %s uri\n" "$(basename "$0")" >&2 exit 1 fi -pastepath="$(screenshot-url "$1")" +pastepath="$(screenshot-uri "$1")" pastefile="$(basename "${pastepath}")" printf "gopher://bitreich.org/I/p/%s\n" "${pastefile}" DIR diff --git a/screenshot-uri b/screenshot-uri @@ -0,0 +1,29 @@ +#!/bin/sh + +if [ $# -lt 1 ]; +then + printf "usage: %s uri\n" "$(basename "$0")" >&2 + exit 1 +fi + +# Use Tor. +proxy="socks5://127.0.0.1:9100" +#proxy="" +export http_proxy="${proxy}" +export https_proxy="${proxy}" +export socks_proxy="${proxy}" + +uri="$1" +firefoxargs="--window-size=1024,768" +pastebase="/br/gopher/paste" + +cd "${pastebase}" +tmpfile="$(mktemp p-XXXXXXXXXXXXX.png)" +firefox $firefoxargs --new-instance --screenshot \ + "${pastebase}/${tmpfile}" "${uri}" 2>&1 >/dev/null + +# Firefox stores with rw---- +chmod g+wr "${pastebase}/${tmpfile}" + +printf "%s/%s\n" "${pastebase}" "${tmpfile}" + DIR diff --git a/screenshot-url b/screenshot-url @@ -1,29 +0,0 @@ -#!/bin/sh - -if [ $# -lt 1 ]; -then - printf "usage: %s url\n" "$(basename "$0")" >&2 - exit 1 -fi - -# Use Tor. -proxy="socks5://127.0.0.1:9100" -#proxy="" -export http_proxy="${proxy}" -export https_proxy="${proxy}" -export socks_proxy="${proxy}" - -url="$1" -firefoxargs="--window-size=1024,768" -pastebase="/br/gopher/paste" - -cd "${pastebase}" -tmpfile="$(mktemp p-XXXXXXXXXXXXX.png)" -firefox $firefoxargs --new-instance --screenshot \ - "${pastebase}/${tmpfile}" "${url}" 2>&1 >/dev/null - -# Firefox stores with rw---- -chmod g+wr "${pastebase}/${tmpfile}" - -printf "%s/%s\n" "${pastebase}" "${tmpfile}" - DIR diff --git a/subtitle-paste b/subtitle-paste @@ -4,17 +4,17 @@ export PATH="/home/annna/bin:$PATH" if [ $# -lt 1 ]; then - printf "usage: %s url\n" "$(basename "$0")" >&2 + printf "usage: %s uri\n" "$(basename "$0")" >&2 exit 1 fi pastebase="/br/gopher/paste" -url="$1" +uri="$1" cd "${pastebase}" tmpfile="$(mktemp -u p-XXXXXXXXXXXXX)" -youtube-dl -q --write-auto-sub --skip-download -o "${tmpfile}" "${url}" 2>/dev/null +youtube-dl -q --write-auto-sub --skip-download -o "${tmpfile}" "${uri}" 2>/dev/null rfile="$(find . -name "${tmpfile}.*")" [ -z "${rfile}" ] && exit 1 ofile="$(basename "${rfile}")"