d4952eb0537336507c0cf7b6fb95468fb8f1d4a2
[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 alt_text 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 toot_args+=(--description "$alt_text" )
160 shift 2
161 ;;
162 -m)
163 media="$2"
164 shift 2
165 toot_args+=(--media "$media" )
166 gs_arg="-F media=@$media"
167 ;;
168 --dbd)
169 twitter_account=dbd
170 gs_account=dbd
171 mastodon_account=endDRM@hostux.social
172 shift
173 ;;
174 -v)
175 video=true
176 media="$2"
177 shift 2
178 ;;
179 esac
180 done
181 if [[ $media ]]; then
182 if [[ ! -e $media ]]; then
183 echo "error: file not found $media"
184 return 1
185 fi
186 if [[ $media == *\ * ]]; then
187 echo "error: file path contains a space. move it to non-space path"
188 return 1
189 fi
190 if [[ ! $alt_text && -r $media.txt && -s $media.txt ]]; then
191 alt_text=$(tail -n1 $media.txt)
192 fi
193 fi
194 # if we have no argument
195 if (( ! $# )); then
196 read -r -p "input PDT text: " input
197 set -- "$input"
198 fi
199 if [[ $- == *i* ]]; then
200 echo "About to PDT the following line. Press enter to confirm or ctrl-c to quit:"
201 echo "$*"
202 read -r
203 fi
204 if $video; then
205 local oath
206 oath=$HOME/.rainbow_oauth
207 rm -f $oath
208 ln -s ${oath}-$twitter_account $oath
209 python3 ~/src/video-tweet/async-upload.py "$media" "$*"
210 return
211 fi
212 fails=()
213 if $do_twitter; then
214 if ! tweet -$twitter_account "$*"; then
215 fails+=(tweet)
216 fi
217 fi
218 if $do_mastodon; then
219 if ! toot -$mastodon_account post "$*" "${toot_args[@]}"; then
220 fails+=(toot)
221 fi
222 fi
223 if $do_gnusocial; then
224 # https://gnusocial.net/doc/twitterapi
225 if ! curl -o /dev/null -sS -u "$gs_account:$(cat ~/.gnusocial_login-$gs_account)" \
226 $gs_arg -F "status=$*" https://status.fsf.org/api/statuses/update.xml; then
227 fails+=(gnu-social)
228 fi
229 fi
230 if (( ${#fails[@]} )); then
231 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}"); echo
232 echo "FSF ERROR: ${fails[*]} might not have posted" >&2
233 fi
234 }
235
236 # Usage: toot-setup
237 #
238 # Only run manually for testing.
239 #
240 # this expects pdt-pip-setup has been run already, and it doesn't setup
241 # errhandle.
242 #
243 # note: auth info is stored at ~/.config/toot/config.json
244 pdt-toot-setup() {
245 local -a twitter_accounts mastodon_accounts
246 if [[ $pdttest ]]; then
247 twitter_accounts=(iank)
248 mastodon_accounts=(fsftest)
249 else
250 twitter_accounts=(fsf dbd)
251 mastodon_accounts=(fsf endDRM)
252 fi
253
254 rm -rf ~/src/toot
255 mkdir -p ~/src/toot
256 cd ~/src/toot
257 # on t11, got myself into a situation where when doing pip install virtualenv,
258 # it gave an error that /usr/bin/pip didn't exist, so i did
259 # sudo ln -s /home/iank/.local/bin/pip /usr/bin
260 #
261 python3 -m virtualenv -p python3 venv
262 source venv/bin/activate
263 # pip freeze after a pip install, as of 2022-11-28
264 cat >requirements.txt <<'EOF'
265 beautifulsoup4==4.11.1
266 certifi==2022.9.24
267 charset-normalizer==2.1.1
268 idna==2.8
269 requests==2.22.0
270 soupsieve==2.3.2.post1
271 toot==0.29.0
272 urllib3==1.25.8
273 urwid==2.1.2
274 wcwidth==0.2.5
275 EOF
276
277 # new 2022-11 packages
278 # beautifulsoup4==4.11.1
279 # certifi==2022.9.24
280 # charset-normalizer==2.1.1
281 # idna==3.4
282 # requests==2.28.1
283 # soupsieve==2.3.2.post1
284 # toot==0.29.0
285 # urllib3==1.26.13
286 # urwid==2.1.2
287 # wcwidth==0.2.5
288
289 # old 2019 packages
290 # beautifulsoup4==4.8.2
291 # certifi==2019.11.28
292 # chardet==3.0.4
293 # idna==2.8
294 # requests==2.22.0
295 # soupsieve==1.9.5
296 # toot==0.25.2
297 # urllib3==1.25.8
298 # urwid==2.1.0
299 # wcwidth==0.1.8
300
301 # i mixed in some old packages to get newer toot working on t9.
302
303 python3 -m pip install -r requirements.txt
304 cd -
305 deactivate
306 for account in ${mastodon_accounts[@]}; do
307 if ! toot activate $account@hostux.social &>/dev/null; then
308 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
309 echo "Please login to the account named \"$account\" on https://hostux.social in your main browser then press enter."
310 echo "WARNING: if you log into an account other than \"$account\", this won't work"
311 read -r
312 toot login -i hostux.social
313 fi
314 done
315 }
316
317 # Usage: pdt-pip-setup
318 pdt-pip-setup() {
319 if [[ ! -e ~/.local/bin/pip ]]; then
320 tmp=$(mktemp)
321 pyver=$(python3 --version | sed -r 's/.*(3\.[0-9]+).*/\1/')
322 echo fyi: detected pyver: $pyver. this should look something like 3.6
323
324 if dpkg --compare-versions 3.6 ge $pyver; then
325 # The bootstrap script at https://bootstrap.pypa.io/get-pip.py required 3.7+
326 # when i wrote this.
327 wget -O$tmp https://bootstrap.pypa.io/pip/$pyver/get-pip.py
328 else
329 wget -O$tmp https://bootstrap.pypa.io/get-pip.py
330 fi
331 python3 $tmp --user
332 hash -r
333 fi
334 python3 -m pip install --user -U virtualenv
335 }
336
337 # generally only meant to be called internally from pdt-setup
338 pdt-twitter-setup() {
339 # twitter setup
340 mkdir -p ~/src/twitter
341 cd ~/src/twitter
342 python3 -m virtualenv -p python3 venv
343 source venv/bin/activate
344 # as of 2022-11-30
345 cat >requirements.txt <<'EOF'
346 certifi==2022.9.24
347 twitter==1.19.6
348 EOF
349 python3 -m pip install -r requirements.txt
350 deactivate
351 cd -
352
353 # note: we could ditch rainbowstream altogether and just pass around
354 # oath api key string in a password file or something if we learned
355 # how to get it from the new twitter client im using. I'm just keeping
356 # this because it works and I like the format of how it stores its
357 # auth secrets.
358 cd ~/src/rainbowstream
359 python3 -m virtualenv -p python3 venv
360 source venv/bin/activate
361 python3 -m pip install -r requirements.txt
362 python3 -m pip install -e .
363 deactivate
364
365 for account in ${twitter_accounts[@]}; do
366 if [[ ! -s ~/.rainbow_oauth-$account ]]; then
367 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
368 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"
369 read -r
370 rbow -$account
371 fi
372 done
373 for account in ${twitter_accounts[@]}; do
374 if [[ ! -s $HOME/.rainbow_oauth-$account ]]; then
375 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"
376 return 1
377 fi
378 done
379 if [[ ! $pdttest ]] && diff -q $HOME/.rainbow_oauth-fsf $HOME/.rainbow_oauth-dbd &>/dev/null; then
380 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"
381 return 1
382 fi
383 cd -
384 }
385
386
387 # Usage: pdt-setup
388 pdt-setup() {
389
390 start_dir="$PWD"
391 mkdir -p ~/src
392 cd ~/src
393 for repo in errhandle rainbowstream video-tweet; do
394 if [[ -e $repo ]]; then
395 cd $repo
396 git fetch
397 git reset --hard origin/master
398 git clean -xfffd
399 cd ..
400 else
401 git clone https://vcs.fsf.org/git/$repo.git
402 fi
403 done
404 cd "$start_dir"
405 source ~/src/errhandle/err
406 cp ~/src/rainbowstream/rainbowstream/colorset/config ~/.rainbow_config.json
407
408 pdt-pip-setup
409
410 pdt-toot-setup
411
412 pdt-twitter-setup
413
414
415 for account in dbd fsf; do
416 if [[ ! -s ~/.gnusocial_login-$account ]]; then
417 printf "$(tput setaf 5)█$(tput sgr0)%.0s" $(eval echo "{1..${COLUMNS:-60}}");
418 read -r -p "please enter the password for $account@status.fsf.org > " pass
419 touch ~/.gnusocial_login-$account
420 chmod 600 ~/.gnusocial_login-$account
421 printf "%s\n" "$pass" > ~/.gnusocial_login-$account
422 fi
423 done
424
425 err-allow
426 echo "pdt-setup complete"
427 }