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
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.
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.
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/>.
19 # This is meant to be sourced.
21 PATH
="$HOME/.local/bin:$PATH"
24 _pdtsh_file
="$(readlink -f -- "${BASH_SOURCE[0]}")"
25 _pdtsh_dir
="${_pdtsh_file%/*}"
26 _pdtsh_tweet
="$_pdtsh_dir/t.py"
28 # usage: tweet [-PROFILE_NAME] [POST...]
29 # Uses variables as input:
30 # $media for image file path.
31 # $alt_text for image alt text.
34 oath
=$HOME/.rainbow_oauth
35 if [[ $1 == -* ]]; then
40 source ~
/src
/twitter
/venv
/bin
/activate
44 printf "%s\n" "$media"
45 if [[ $alt_text ]]; then
46 printf "%s\n" "$alt_text"
53 # usage: rbow [-PROFILE_NAME] [COMMAND]
55 # Wrapper for rainbowstream to use multiple logins and tweet directly
56 # from the command line. PROFILE_NAME can be anything you want.
59 oath
=$HOME/.rainbow_oauth
60 if [[ $1 == -* ]]; then
65 source ~
/src
/rainbowstream
/venv
/bin
/activate
69 # adds a short delay after each send for more reliable operation
70 set force_conservative 1
73 expect -nocase timeout {exit 1} "@*]: "
74 set cmd [lindex $argv 0];
76 expect -nocase timeout {exit 1} "@*]: "
77 # this may not be needed. didnt test
82 expect
$f "$*" ||
{ echo error
: expect failed
; deactivate
; rm -f $f; return 1; }
91 # usage: toot [-PROFILE_NAME] [TOOT_ARGS]
94 source ~
/src
/toot
/venv
/bin
/activate
95 if [[ $1 == -* ]]; then
98 command toot activate
--quiet $mast_profile ||
{ deactivate
; return 1; }
100 command toot
"$@" ||
{ deactivate
; return 1; }
105 # post to mastodon + twitter + gnu social post
107 # If posting video, it only goes to twitter. Other accounts can be
108 # posted to manually with free software.
110 # Alt text only goes to mastodon.
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.
117 local video media twitter_account gs_account mastodon_account video gs_arg network
118 local do_mastodon do_twitter do_gnusocial
120 if [[ $pdttest ]]; then
123 mastodon_account
=fsftest@hostux.social
127 mastodon_account
=fsf@hostux.social
133 while [[ $1 == -* ]]; do
151 echo "pdt: error: expected -s mastodon|twitter|gnusocial"
164 toot_args
+=(--media "$media" )
165 gs_arg
="-F media=@$media"
170 mastodon_account
=endDRM@hostux.social
180 if [[ $media ]]; then
181 if [[ ! -e $media ]]; then
182 echo "error: file not found $media"
185 if [[ $media == *\
* ]]; then
186 echo "error: file path contains a space. move it to non-space path"
189 if [[ ! $alt_text && -r $media.txt
&& -s $media.txt
]]; then
190 alt_text
=$
(tail -n1 $media.txt
)
192 if [[ $alt_text ]]; then
193 toot_args
+=(--description "$alt_text" )
196 # if we have no argument
198 read -r -p "input PDT text: " input
201 if [[ $
- == *i
* ]]; then
202 echo "About to PDT the following line. Press enter to confirm or ctrl-c to quit:"
208 oath
=$HOME/.rainbow_oauth
210 ln -s ${oath}-$twitter_account $oath
211 python3 ~
/src
/video-tweet
/async-upload.py
"$media" "$*"
216 if ! tweet
-$twitter_account "$*"; then
220 if $do_mastodon; then
221 if ! toot
-$mastodon_account post
"$*" "${toot_args[@]}"; then
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
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
240 # Only run manually for testing.
242 # this expects pdt-pip-setup has been run already, and it doesn't setup
245 # note: auth info is stored at ~/.config/toot/config.json
247 local -a twitter_accounts mastodon_accounts
248 if [[ $pdttest ]]; then
249 twitter_accounts
=(iank
)
250 mastodon_accounts
=(fsftest
)
252 twitter_accounts
=(fsf dbd
)
253 mastodon_accounts
=(fsf endDRM
)
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
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
269 charset-normalizer==2.1.1
272 soupsieve==2.3.2.post1
279 # new 2022-11 packages
280 # beautifulsoup4==4.11.1
282 # charset-normalizer==2.1.1
285 # soupsieve==2.3.2.post1
292 # beautifulsoup4==4.8.2
293 # certifi==2019.11.28
303 # i mixed in some old packages to get newer toot working on t9.
305 python3
-m pip
install -r requirements.txt
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"
314 toot login
-i hostux.social
319 # Usage: pdt-pip-setup
321 if [[ ! -e ~
/.local
/bin
/pip
]]; then
323 pyver
=$
(python3
--version |
sed -r 's/.*(3\.[0-9]+).*/\1/')
324 echo fyi
: detected pyver
: $pyver. this should
look something like
3.6
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+
329 wget
-O$tmp https
://bootstrap.pypa.io
/pip
/$pyver/get-pip.py
331 wget
-O$tmp https
://bootstrap.pypa.io
/get-pip.py
336 python3
-m pip
install --user -U virtualenv
339 # generally only meant to be called internally from pdt-setup
340 pdt-twitter-setup
() {
342 mkdir
-p ~
/src
/twitter
344 python3
-m virtualenv
-p python3 venv
345 source venv
/bin
/activate
347 cat >requirements.txt
<<'EOF'
351 python3
-m pip
install -r requirements.txt
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
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 .
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"
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"
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"
395 for repo
in errhandle rainbowstream video-tweet
; do
396 if [[ -e $repo ]]; then
399 git
reset --hard origin
/master
403 git clone https
://vcs.fsf.org
/git
/$repo.git
407 source ~
/src
/errhandle
/err
408 cp ~
/src
/rainbowstream
/rainbowstream
/colorset
/config ~
/.rainbow_config.json
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
428 echo "pdt-setup complete"