Refactor all uri handling to be more extendable and readable. - annna - Annna t… | |
git clone git://bitreich.org/annna/ git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws6… | |
Log | |
Files | |
Refs | |
Tags | |
README | |
--- | |
commit 0f7e3893de03907ca5ba18f8462895a6055ff82c | |
parent c3d834f28259c7860e41365fb87cfcc3777cf973 | |
Author: Annna Robert-Houdin <[email protected]> | |
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(-) | |
--- | |
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! … | |
diff --git a/annna-get-hashtag-url b/annna-get-hashtag-uri | |
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 pa… | |
+ | |
+ # 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 o… | |
+ titleend=1 | |
;; | |
- *) | |
- if expr "${uri}" : '^https*://[^/]*you… | |
+ esac | |
+ fi | |
+ | |
+ if expr "${uri}" : '^https*://[^/]*youtu\(\.be\|be\..\… | |
+ then | |
+ sturi="$(subtitle-paste "${uri}")" | |
+ fi | |
+ | |
+ case "${uri}" in | |
+ *twitter.com*) | |
+ nuri="$(printf "%s\n" "${uri}" | sed 's;\(mobi… | |
+ suri="$(screenshot-paste "${nuri}")" | |
+ nuri="nitter: ${nuri} " | |
+ ;; | |
+ *en.wikipedia.org/wiki*) | |
+ if ! regeximatch "$uri" '\.(jpg,bmp,gif,png,ti… | |
+ then | |
+ nuri="$(printf 'gopherpedia: %s \n' "$… | |
+ curi="$(9 htmlfmt < "${tmpf}" | /br/bi… | |
+ else | |
+ nocuri=1 | |
+ fi | |
+ ;; | |
+ *www.reddit.com*) | |
+ nuri="$(printf 'old.reddit: %s \n' "${uri}" | … | |
+ curi="$(9 htmlfmt < "${tmpf}" | /br/bin/bitrei… | |
+ if [ -n "${uri#*www.reddit.com/r/*}" ]; | |
+ then | |
+ subreddit="$(printf '%s\n' "${uri}" | … | |
+ post="$(printf '%s\n' "${uri}" | cut -… | |
+ if [ -n "${post}" ]; | |
then | |
- spurl="$(subtitle-paste "${uri… | |
- if [ -n "${spurl}" ]; | |
- then | |
- annna-say -c "${channe… | |
- else | |
- annna-say -c "${channe… | |
- fi | |
+ guri="gopher://gopherddit.com/… | |
else | |
- set -x | |
- wikipedia=0 | |
- case "${uri}" in | |
- *twitter.com*) | |
- nurl="$(printf "%s\n" … | |
- spurl="$(screenshot-pa… | |
- annna-say -c "${channe… | |
- | |
- ;; | |
- *en.wikipedia.org/wiki*) | |
- if ! regeximatch "$uri… | |
- nurl="$(printf… | |
- purl="$(9 html… | |
- annna-say -c "… | |
- # Hack, Hack, … | |
- wikipedia=1 | |
- fi | |
- ;; | |
- *www.reddit.com*) | |
- nurl="$(printf '%s\n' … | |
- purl="$(9 htmlfmt < "$… | |
- if [ -n "${uri#*www.re… | |
- subreddit="$(p… | |
- post="$(printf… | |
- if [ -n "${pos… | |
- gurl="… | |
- else | |
- gurl="… | |
- fi | |
- annna-say -c "… | |
- else | |
- annna-say -c "… | |
- fi | |
- # Hack, Hack, Hack. | |
- wikipedia=1 | |
- ;; | |
- *) | |
- spurl="" | |
- ;; | |
- esac | |
- set +x | |
- | |
- if [ $wikipedia -eq 0 ]; | |
- then | |
- purl="$(9 htmlfmt < "$… | |
- if [ -n "${spurl}" ]; | |
- then | |
- annna-say -c "… | |
- else | |
- annna-say -c "… | |
- fi | |
- fi | |
+ guri="gopher://gopherddit.com/… | |
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}" … | |
+ fi | |
+ outputstr="${outputstr}content: ${curi… | |
+ fi | |
+ | |
+ [ -n "${nuri}" ] && outputstr="${outputstr}; $… | |
+ [ -n "${urititle}" ] && outputstr="${outputstr… | |
+ [ -n "${suri}" ] && outputstr="${outputstr}; s… | |
+ [ -n "${sturi}" ] && outputstr="${outputstr}; … | |
+ 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/bit… | |
+ puri="$(printf "%s" "${dresult}" | /br/bin/bit… | |
;; | |
20) | |
- purl="No matches found." | |
+ puri="No matches found." | |
;; | |
1) | |
- purl="Parse error." | |
+ puri="Parse error." | |
;; | |
*) | |
- purl="Parazyd destroyed the server. Nothing to… | |
+ puri="Parazyd destroyed the server. Nothing to… | |
;; | |
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, ,… | |
@@ -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/bit… | |
+ puri="$(printf "%s" "${dresult}" | /br/bin/bit… | |
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, ,… | |
@@ -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/bitrei… | |
- annna-say -c "${channel}" "${user}, your future is here: ${pur… | |
+ puri="$(sacc gopher://parazyd.org/0/tarot.cgi | /br/bin/bitrei… | |
+ annna-say -c "${channel}" "${user}, your future is here: ${pur… | |
;; | |
"${botname}, how many memes do you know?") | |
annna-say -c "${channel}" "I know $(hashtagcount) memes: gophe… | |
@@ -341,8 +348,8 @@ annna_common() { | |
annna-say -c "${channel}" "${user}, too many. :( Please see yo… | |
;; | |
"${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… | |
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 | |
+ | |
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 | |
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 | |
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}" | |
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}" | |
+ | |
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}" | |
- | |
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… | |
+youtube-dl -q --write-auto-sub --skip-download -o "${tmpfile}" "${uri}" 2>/dev… | |
rfile="$(find . -name "${tmpfile}.*")" | |
[ -z "${rfile}" ] && exit 1 | |
ofile="$(basename "${rfile}")" |