Source code for my personal quote bot project.
1#!/bin/bash
2
3parent_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
4cd "$parent_path"
5
6while [[ $# -gt 0 ]]; do
7 key="$1"
8
9 case $key in
10 -h|--help)
11 echo -e "$0 - usage:" \
12 "[-h] [-c CREDENTIALS] [-f POSTLIST] [-t]\n" \
13 "-h, --help show this message\n" \
14 "-t, --tweet enables tweeting mode\n" \
15 "-b, --bluesky enables bluesky mode\n"
16 "-f, --filename file containing post list\n" \
17 "-c, --credentials specify twitter credentials file"
18 exit
19 ;;
20 -t|--tweet)
21 # Enables tweeting of the text files
22 # as opposed to simply displaying them
23 TWEETMODE=ON
24 shift # past argument
25 ;;
26 -b|--bluesky)
27 # Enables posting of text files via Bluesky
28 BLUESKY=ON
29 shift
30 ;;
31 -f|--filename)
32 # Controls which file contains the list
33 # of filenames to select
34 FNAME="$2"
35 shift # past argument
36 shift # past value
37 ;;
38 -c|--credentials)
39 # Optionally specify twitter credentials file
40 CREDENTIALS="$2"
41 shift # past argument
42 shift # past value
43 ;;
44 esac
45done
46
47if [ -z ${CREDENTIALS} ]; then
48 CREDENTIALS="./config/acc.toml"
49fi
50
51if [ -z ${FNAME} ]; then
52 FNAME="./config/quotes"
53fi
54
55function shift_list() {
56 ! test -s "${FNAME}" && find "./src" -type f -iname "*.txt" | shuf >"${FNAME}"
57
58 QUOTE_FNAME=$(head -1 "${FNAME}")
59 tail -n +2 "${FNAME}" > "${FNAME}.tmp" && mv "${FNAME}.tmp" "${FNAME}"
60}
61
62function post_tweet() {
63 ./util/tweet.py -c "${CREDENTIALS}" send -f "${QUOTE_FNAME}"
64 return $?
65}
66
67function post_bluesky() {
68 python3 ./util/bluesky.py -f "${QUOTE_FNAME}"
69 return $?
70}
71
72shift_list
73
74if [ ${TWEETMODE} ]; then
75 # Keep going until a tweet was posted
76 while post_tweet; [ $? -ne 0 ]; do shift_list; done
77elif [ ${BLUESKY} ]; then
78 # Keep going until a post is successful
79 while post_bluesky; [ $? -ne 0 ]; do shift_list; done
80else
81 cat ${QUOTE_FNAME}
82fi