a93932690a973e0ee90fce91129fa61ab9426e30
[pdt.git] / pdt.sh
1 #!/bin/bash
2 # pdt: post to FSF social media via command line
3 # Copyright (C) 2021 Ian Kelling
4 # SPDX-License-Identifier: AGPL-3.0-or-later
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19 # This is meant to be sourced.
20
21 PATH="$HOME/.local/bin:$PATH"
22
23
24 _pdtsh_file="$(readlink -f -- "${BASH_SOURCE[0]}")"
25 _pdtsh_dir="${_pdtsh_file%/*}"
26 _pdtsh_tweet="$_pdtsh_dir/t.py"
27
28 # usage: tweet [-PROFILE_NAME] [POST...]
29 # Uses variables as input:
30 # $media for image file path.
31 # $alt_text for image alt text.
32 tweet() {
33 local oath
34 oath=$HOME/.rainbow_oauth
35 if [[ $1 == -* ]]; then
36 rm -f $oath
37 ln -s $oath$1 $oath
38 shift
39 fi
40 source ~/src/twitter/venv/bin/activate
41 {
42 printf "%s\n" "$*"
43 if [[ $media ]]; then
44 printf "%s\n" "$media"
45 if [[ $alt_text ]]; then
46 printf "%s\n" "$alt_text"
47 fi
48 fi
49 } | $_pdtsh_tweet
50 deactivate
51 }
52
53 # usage: rbow [-PROFILE_NAME] [COMMAND]
54 #
55 # Wrapper for rainbowstream to use multiple logins and tweet directly
56 # from the command line. PROFILE_NAME can be anything you want.
57 rbow() {
58 local oath f
59 oath=$HOME/.rainbow_oauth
60 if [[ $1 == -* ]]; then
61 rm -f $oath
62 ln -s $oath$1 $oath
63 shift
64 fi
65 source ~/src/rainbowstream/venv/bin/activate
66 if (($#)); then
67 f=$(mktemp)
68 cat >$f <<'EOF'
69 # adds a short delay after each send for more reliable operation
70 set force_conservative 1
71 spawn "rainbowstream"
72 # wait for prompt
73 expect -nocase timeout {exit 1} "@*]: "
74 set cmd [lindex $argv 0];
75 send "$cmd\r\r"
76 expect -nocase timeout {exit 1} "@*]: "
77 # this may not be needed. didnt test
78 sleep 2
79 send "q\r"
80 interact
81 EOF
82 expect $f "$*" || { echo error: expect failed; deactivate; rm -f $f; return 1; }
83 rm -f $f
84 else
85 rainbowstream
86 fi
87 deactivate
88 }
89
90
91 # usage: toot [-PROFILE_NAME] [TOOT_ARGS]
92 toot() {
93 local mast_profile
94 source ~/src/toot/venv/bin/activate
95 if [[ $1 == -* ]]; then
96 mast_profile=${1#-}
97 shift
98 command toot activate --quiet $mast_profile || { deactivate; return 1; }
99 fi
100 command toot "$@" || { deactivate; return 1; }
101 deactivate
102 }
103
104
105 # post to mastodon + twitter + gnu social post
106 #
107 # If posting video, it only goes to twitter. Other accounts can be
108 # posted to manually with free software.
109 #
110 # Alt text only goes to mastodon.
111 #
112 # usage: pdt [-s mastodon|twitter|gnusocial] [--dbd] [-m IMAGE_FILE] [-a ALT_TEXT] [-v VIDEO_PATH] [POST]
113 # -s mastodon|twitter|gnusocial = post to a single social network.
114 # -m IMAGE_FILE = Uploads IMAGE_FILE. if MEDIA_FILE.txt exists, the last line of that file will be used as ALT_TEXT
115 # unless -a has been used.
116 pdt() {
117 local video media twitter_account gs_account mastodon_account video gs_arg network
118 local do_mastodon do_twitter do_gnusocial
119 local -a toot_args
120 if [[ $pdttest ]]; then
121 twitter_account=iank
122 gs_account=fsfes
123 mastodon_account=fsftest@hostux.social
124 else
125 twitter_account=fsf
126 gs_account=fsf
127 mastodon_account=fsf@hostux.social
128 fi
129 video=false
130 do_mastodon=true
131 do_twitter=true
132 do_gnusocial=true
133 while [[ $1 == -* ]]; do
134 case $1 in
135 -s)
136 network="$2"
137 do_mastodon=false
138 do_twitter=false
139 do_gnusocial=false
140 case $network in
141 mastodon)
142 do_mastodon=true
143 ;;
144 twitter)
145 do_twitter=true
146 ;;
147 gnusocial)
148 do_gnusocial=true
149 ;;
150 *)
151 echo "pdt: error: expected -s mastodon|twitter|gnusocial"
152 return 1
153 ;;
154 esac
155 shift 2
156 ;;
157 -a)
158 alt_text="$2"
159 shift 2
160 ;;
161 -m)
162 media="$2"
163 shift 2
164 toot_args+=(--media "$media" )
165 gs_arg="-F media=@$media"
166 ;;
167 --dbd)
168 twitter_account=dbd
169 gs_account=dbd
170 mastodon_account=endDRM@hostux.social
171 shift
172 ;;
173 -v)
174 video=true
175 media="$2"
176 shift 2
177 ;;
178 esac
179 done
180 if [[ $media ]]; then
181 if [[ ! -e $media ]]; then
182 echo "error: file not found $media"
183 return 1
184 fi
185 if [[ $media == *\ * ]]; then
186 echo "error: file path contains a space. move it to non-space path"
187 return 1
188 fi
189 if [[ ! $alt_text && -r $media.txt && -s $media.txt ]]; then
190 alt_text=$(tail -n1 $media.txt)
191 fi
192 if [[ $alt_text ]]; then
193 toot_args+=(--description "$alt_text" )
194 fi
195 fi
196 # if we have no argument
197 if (( ! $# )); then
198 read -r -p "input PDT text: " input
199 set -- "$input"
200 fi
201 if [[ $- == *i* ]]; then
202 echo "About to PDT the following line. Press enter to confirm or ctrl-c to quit:"
203 echo "$*"
204 read -r
205 fi
206 if $video; then
207 local oath
208 oath=$HOME/.rainbow_oauth
209 rm -f $oath
210 ln -s ${oath}-$twitter_account $oath
211 python3 ~/src/video-tweet/async-upload.py "$media" "$*"
212 return
213 fi
214 fails=()
215 if $do_twitter; then
216 if ! tweet -$twitter_account "$*"; then
217 fails+=(tweet)
218 fi
219 fi
220 if $do_mastodon; then
221 if ! toot -$mastodon_account post "$*" "${toot_args[@]}"; then
222 fails+=(toot)
223 fi
224 fi
225 if $do_gnusocial; then
226 # https://gnusocial.net/doc/twitterapi
227 if ! curl -o /dev/null -sS -u "$gs_account:$(cat ~/.gnusocial_login-$gs_account)" \
228 $gs_arg -F "status=$*" https://status.fsf.org/api/statuses/update.xml; then
229 fails+=(gnu-social)
230 fi
231 fi
232 if (( ${#fails[@]} )); then
233 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}"); echo
234 echo "FSF ERROR: ${fails[*]} might not have posted" >&2
235 fi
236 }
237
238 # Usage: toot-setup
239 #
240 # Only run manually for testing.
241 #
242 # this expects pdt-pip-setup has been run already, and it doesn't setup
243 # errhandle.
244 #
245 # note: auth info is stored at ~/.config/toot/config.json
246 pdt-toot-setup() {
247 local -a twitter_accounts mastodon_accounts
248 if [[ $pdttest ]]; then
249 twitter_accounts=(iank)
250 mastodon_accounts=(fsftest)
251 else
252 twitter_accounts=(fsf dbd)
253 mastodon_accounts=(fsf endDRM)
254 fi
255
256 rm -rf ~/src/toot
257 mkdir -p ~/src/toot
258 cd ~/src/toot
259 # on t11, got myself into a situation where when doing pip install virtualenv,
260 # it gave an error that /usr/bin/pip didn't exist, so i did
261 # sudo ln -s /home/iank/.local/bin/pip /usr/bin
262 #
263 python3 -m virtualenv -p python3 venv
264 source venv/bin/activate
265 # pip freeze after a pip install, as of 2022-11-28
266 cat >requirements.txt <<'EOF'
267 beautifulsoup4==4.11.1
268 certifi==2022.9.24
269 charset-normalizer==2.1.1
270 idna==2.8
271 requests==2.22.0
272 soupsieve==2.3.2.post1
273 toot==0.29.0
274 urllib3==1.25.8
275 urwid==2.1.2
276 wcwidth==0.2.5
277 EOF
278
279 # new 2022-11 packages
280 # beautifulsoup4==4.11.1
281 # certifi==2022.9.24
282 # charset-normalizer==2.1.1
283 # idna==3.4
284 # requests==2.28.1
285 # soupsieve==2.3.2.post1
286 # toot==0.29.0
287 # urllib3==1.26.13
288 # urwid==2.1.2
289 # wcwidth==0.2.5
290
291 # old 2019 packages
292 # beautifulsoup4==4.8.2
293 # certifi==2019.11.28
294 # chardet==3.0.4
295 # idna==2.8
296 # requests==2.22.0
297 # soupsieve==1.9.5
298 # toot==0.25.2
299 # urllib3==1.25.8
300 # urwid==2.1.0
301 # wcwidth==0.1.8
302
303 # i mixed in some old packages to get newer toot working on t9.
304
305 python3 -m pip install -r requirements.txt
306 cd -
307 deactivate
308 for account in ${mastodon_accounts[@]}; do
309 if ! toot activate $account@hostux.social &>/dev/null; then
310 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
311 echo "Please login to the account named \"$account\" on https://hostux.social in your main browser then press enter."
312 echo "WARNING: if you log into an account other than \"$account\", this won't work"
313 read -r
314 toot login -i hostux.social
315 fi
316 done
317 }
318
319 # Usage: pdt-pip-setup
320 pdt-pip-setup() {
321 if [[ ! -e ~/.local/bin/pip ]]; then
322 tmp=$(mktemp)
323 pyver=$(python3 --version | sed -r 's/.*(3\.[0-9]+).*/\1/')
324 echo fyi: detected pyver: $pyver. this should look something like 3.6
325
326 if dpkg --compare-versions 3.6 ge $pyver; then
327 # The bootstrap script at https://bootstrap.pypa.io/get-pip.py required 3.7+
328 # when i wrote this.
329 wget -O$tmp https://bootstrap.pypa.io/pip/$pyver/get-pip.py
330 else
331 wget -O$tmp https://bootstrap.pypa.io/get-pip.py
332 fi
333 python3 $tmp --user
334 hash -r
335 fi
336 python3 -m pip install --user -U virtualenv
337 }
338
339 # generally only meant to be called internally from pdt-setup
340 pdt-twitter-setup() {
341 # twitter setup
342 mkdir -p ~/src/twitter
343 cd ~/src/twitter
344 python3 -m virtualenv -p python3 venv
345 source venv/bin/activate
346 # as of 2022-11-30
347 cat >requirements.txt <<'EOF'
348 certifi==2022.9.24
349 twitter==1.19.6
350 EOF
351 python3 -m pip install -r requirements.txt
352 deactivate
353 cd -
354
355 # note: we could ditch rainbowstream altogether and just pass around
356 # oath api key string in a password file or something if we learned
357 # how to get it from the new twitter client im using. I'm just keeping
358 # this because it works and I like the format of how it stores its
359 # auth secrets.
360 cd ~/src/rainbowstream
361 python3 -m virtualenv -p python3 venv
362 source venv/bin/activate
363 python3 -m pip install -r requirements.txt
364 python3 -m pip install -e .
365 deactivate
366
367 for account in ${twitter_accounts[@]}; do
368 if [[ ! -s ~/.rainbow_oauth-$account ]]; then
369 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
370 echo "Please login to the account named \"$account\" on twitter in your main browser then press enter. After rainbowstream prompt loads, quit by typing q then enter"
371 read -r
372 rbow -$account
373 fi
374 done
375 for account in ${twitter_accounts[@]}; do
376 if [[ ! -s $HOME/.rainbow_oauth-$account ]]; then
377 echo "pdt-setup error: expected non-empty file at $HOME/.rainbow_oauth-$account by this point. try reruning pdt-setup and logging in to the correct twitter account in browser or contact ian"
378 return 1
379 fi
380 done
381 if [[ ! $pdttest ]] && diff -q $HOME/.rainbow_oauth-fsf $HOME/.rainbow_oauth-dbd &>/dev/null; then
382 echo "pdt-setup error: error, $HOME/.rainbow_oauth-fsf $HOME/.rainbow_oauth-dbd are the same. Did you follow the instructions closely and log into fsf and then dbd when prompted? try reruning pdt-setup and doing that"
383 return 1
384 fi
385 cd -
386 }
387
388
389 # Usage: pdt-setup
390 pdt-setup() {
391
392 start_dir="$PWD"
393 mkdir -p ~/src
394 cd ~/src
395 for repo in errhandle rainbowstream video-tweet; do
396 if [[ -e $repo ]]; then
397 cd $repo
398 git fetch
399 git reset --hard origin/master
400 git clean -xfffd
401 cd ..
402 else
403 git clone https://vcs.fsf.org/git/$repo.git
404 fi
405 done
406 cd "$start_dir"
407 source ~/src/errhandle/err
408 cp ~/src/rainbowstream/rainbowstream/colorset/config ~/.rainbow_config.json
409
410 pdt-pip-setup
411
412 pdt-toot-setup
413
414 pdt-twitter-setup
415
416
417 for account in dbd fsf; do
418 if [[ ! -s ~/.gnusocial_login-$account ]]; then
419 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
420 read -r -p "please enter the password for $account@status.fsf.org > " pass
421 touch ~/.gnusocial_login-$account
422 chmod 600 ~/.gnusocial_login-$account
423 printf "%s\n" "$pass" > ~/.gnusocial_login-$account
424 fi
425 done
426
427 err-allow
428 echo "pdt-setup complete"
429 }