First small refactoring :)
With these tings: - Create db during runtime - Better generation of psv - Lower case symbol descriptions - Some Readme fixes - ...
This commit is contained in:
parent
6cc15f513f
commit
4bf3fb5dad
13 changed files with 108 additions and 4544 deletions
2
.dockerignore
Normal file
2
.dockerignore
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.*
|
||||||
|
env
|
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,2 +1,2 @@
|
||||||
working-db.db3
|
*.db3
|
||||||
env
|
env
|
||||||
|
|
19
Dockerfile
19
Dockerfile
|
@ -4,22 +4,23 @@ LABEL maintainer "J. Elfring <code@elfring.ms>"
|
||||||
LABEL org.opencontainers.image.source https://github.com/elfrinjo/compoBot
|
LABEL org.opencontainers.image.source https://github.com/elfrinjo/compoBot
|
||||||
|
|
||||||
RUN apk --no-cache --update \
|
RUN apk --no-cache --update \
|
||||||
add bash \
|
add ca-certificates \
|
||||||
ca-certificates \
|
coreutils \
|
||||||
|
bash \
|
||||||
curl \
|
curl \
|
||||||
|
sed \
|
||||||
sqlite \
|
sqlite \
|
||||||
&& mkdir -p /app/sequences \
|
&& mkdir /app \
|
||||||
&& mkdir /data
|
&& mkdir /data
|
||||||
|
|
||||||
COPY sequences/Compose.db3 /app/sequences/Compose.db3
|
COPY . /app/
|
||||||
COPY compoBot.sh /app/compoBot.sh
|
|
||||||
|
|
||||||
ENV database="/data/working-db.db3" \
|
ENV database=/data/compobot.db3 \
|
||||||
minWait=43200 \
|
minWait=43200 \
|
||||||
maxWait=86400 \
|
maxWait=86400 \
|
||||||
mtdApi="https://mastodon.example/api/v1/statuses" \
|
mtdVisibility=direct \
|
||||||
mtdVisibility="direct" \
|
mtdApi=https://mastodon.example/api/v1/statuses \
|
||||||
mtdToken="THIS_SHOULD_BE_A_Bearer-Token"
|
mtdToken=INSERT-YOUR-BEARER-TOKEN
|
||||||
|
|
||||||
VOLUME /data
|
VOLUME /data
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
36
README.md
36
README.md
|
@ -1,37 +1,29 @@
|
||||||
# compoBot — A bot to toot composekey sequences
|
# compoBot — A bot to toot composekey sequences
|
||||||
The compose key (a.k.a. Multi Key) is a massively useful and
|
The compose key (a.k.a. Multi Key) is a massively useful and (unfortunately) forgotten feature.
|
||||||
(unfortunately) forgotten feature.
|
It lets you type a keyboard sequence eg. `<Multi_Key><t><m>` and it inserts a special character: ™.
|
||||||
It lets you type a keyboard sequence eg. `<Multi_Key><t><m>` and
|
|
||||||
it inserts a special character: ™.
|
|
||||||
|
|
||||||
This bot regularly toots one of those sequences to a mastodon
|
This bot regularly toots one of those sequences to a mastodon account so that everyone can appreciate them.
|
||||||
account so that everyone can appreciate them.
|
|
||||||
|
|
||||||
The sequences used, are standard on modern linux systems.
|
The sequences used, are standard on modern linux systems.
|
||||||
However, the compose/multikey is usually not mapped on most
|
However, the compose/multikey is usually not mapped on most keyboards.
|
||||||
keyboards.
|
With `xmodmap`, it can be mapped to a rarely used key like menu or capslock.
|
||||||
With `xmodmap`, it can be mapped to a rarely used key like menu
|
On windows systems, there is [WinCompose](https://github.com/samhocevar/wincompose) to get similar results.
|
||||||
or capslock.
|
|
||||||
On windows systems, there is
|
|
||||||
(WinCompose)[https://github.com/samhocevar/wincompose]
|
|
||||||
to get similar results.
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
compoBot can be used from shell or docker.
|
compoBot can be used from shell or docker.
|
||||||
The prerequisits are minimal: sqlite3, curl and the standard tools.
|
The prerequisits are minimal: sqlite3, curl and the standard tools.
|
||||||
|
|
||||||
It is configured by environment variables wich may be set in a file
|
It is configured by environment variables wich may be set in a file called env.
|
||||||
called env.
|
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
|---------------|------------------------------------------|----------------------------------------------|
|
|---------------|-----------------------------------------------|----------------------------------------------------------------|
|
||||||
| database | ./working-db.db3 /data/working-db.db3 | Databasefile, should be persistant in docker |
|
| database | ./compobot.db3 (/data/compobot.db3 on Docker) | Databasefile |
|
||||||
| minWait | 43200 (12 h) | Minimum random time to wait between toots |
|
| minWait | 43200 (=12 h) | Minimum random time to wait between toots in seconds |
|
||||||
| maxWait | 86400 (24 h) | Maximum random time to wait between toots |
|
| maxWait | 86400 (=24 h) | Maximum random time to wait between toots in seconds |
|
||||||
| mtdVisibility | direct | Privacy setting for the toot¹ |
|
| mtdVisibility | direct | Privacy setting for the toot¹ (public unlisted private direct) |
|
||||||
| mtdApi | https://mastodon.example/api/v1/statuses | API endpoint to sent statuses² |
|
| mtdApi | https://mastodon.example/api/v1/statuses | API endpoint to sent statuses² |
|
||||||
| mtdToken | THIS_SHOULD_BE_A_Bearer-Token | API authentication² |
|
| mtdToken | INSERT-YOUR-BEARER-TOKEN | API authentication² |
|
||||||
|
|
||||||
¹) https://docs.joinmastodon.org/user/posting/
|
¹) https://docs.joinmastodon.org/entities/status/
|
||||||
²) https://docs.joinmastodon.org/methods/apps/#create-an-application
|
²) https://docs.joinmastodon.org/methods/apps/#create-an-application
|
||||||
|
|
||||||
|
|
25
compoBot.sh
25
compoBot.sh
|
@ -8,19 +8,20 @@
|
||||||
|
|
||||||
|
|
||||||
## Databasefile
|
## Databasefile
|
||||||
database=${database:="./working-db.db3"}
|
database=${database:="./compobot.db3"}
|
||||||
|
|
||||||
## Wait-time between toots in seconds
|
## Wait-time between toots in seconds
|
||||||
minWait=${minWait:=5}
|
minWait=${minWait:=43200}
|
||||||
maxWait=${maxWait:=10}
|
maxWait=${maxWait:=86400}
|
||||||
|
|
||||||
## Mastodon Settings&Credentials
|
## Mastodon Settings&Credentials
|
||||||
mtdVisibility=${mtdVisibility:="direct"} # public, unlisted, private, direct.
|
mtdVisibility=${mtdVisibility:="direct"} # public, unlisted, private, direct.
|
||||||
mtdApi=${mtdApi:="https://mastodon.example/api/v1/statuses"}
|
mtdApi=${mtdApi:="https://mastodon.example/api/v1/statuses"}
|
||||||
mtdToken=${mtdToken:="THIS_SHOULD_BE_A_Bearer-Token"}
|
mtdToken=${mtdToken:="INSERT-YOUR-BEARER-TOKEN"}
|
||||||
|
|
||||||
#####################################################################
|
#####################################################################
|
||||||
|
|
||||||
|
## Source config fro env if there is a file called env.
|
||||||
test -f ./env && source ./env
|
test -f ./env && source ./env
|
||||||
|
|
||||||
echo "=============> App-Start at $(date +%Y-%m-%dT%H%M%S)"
|
echo "=============> App-Start at $(date +%Y-%m-%dT%H%M%S)"
|
||||||
|
@ -28,20 +29,20 @@ echo "=============> App-Start at $(date +%Y-%m-%dT%H%M%S)"
|
||||||
## Blow up on errors
|
## Blow up on errors
|
||||||
set -e
|
set -e
|
||||||
|
|
||||||
## Get a fresh copy of the sequence-db if it doesn't exist.
|
|
||||||
test -f $database || cp sequences/Compose.db3 $database
|
|
||||||
|
|
||||||
while true ; do
|
while true ; do
|
||||||
|
echo "-------------> Toot-Start at $(date +%Y-%m-%dT%H%M%S)"
|
||||||
|
|
||||||
echo "-------------> Starting at $(date +%Y-%m-%dT%H%M%S)"
|
## Create Database if it does not exist
|
||||||
|
test -f $database || sh dbFactory.sh $database
|
||||||
|
|
||||||
## Number of rows still available,
|
## Number of rows still available,
|
||||||
## when no row is left, we will delete the sent-table and start over
|
## when no row is left, just recreate the database and startover.
|
||||||
rowsAvailable=$(sqlite3 "$database" "SELECT count(keySequenceROWID) FROM stillAvailable")
|
rowsAvailable=$(sqlite3 "$database" "SELECT count(keySequenceROWID) FROM stillAvailable")
|
||||||
echo rowsAvailable: $rowsAvailable
|
echo rowsAvailable: $rowsAvailable
|
||||||
if [ $rowsAvailable -eq 0 ] ; then
|
if [ $rowsAvailable -eq 0 ] ; then
|
||||||
echo "Deleting the sent-table and starting ower."
|
echo "Deleting the database and starting ower."
|
||||||
sqlite3 "$database" "DELETE from alreadySent"
|
rm -f $database
|
||||||
|
sh dbFactory.sh $database
|
||||||
fi
|
fi
|
||||||
|
|
||||||
## Pick a random row
|
## Pick a random row
|
||||||
|
@ -75,7 +76,7 @@ while true ; do
|
||||||
|
|
||||||
## Sleep until the next time
|
## Sleep until the next time
|
||||||
waitTime=$(shuf -i $minWait-$maxWait -n 1)
|
waitTime=$(shuf -i $minWait-$maxWait -n 1)
|
||||||
echo "Waiting for $waitTime seconds"
|
echo "Waiting for $waitTime seconds. " $(date -d "$waitTime seconds")
|
||||||
echo ""
|
echo ""
|
||||||
sleep $waitTime
|
sleep $waitTime
|
||||||
|
|
||||||
|
|
22
dbFactory.sh
Normal file
22
dbFactory.sh
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ $# -ne 1 ]; then
|
||||||
|
echo "This script shoud only be run from inside compoBot.sh"
|
||||||
|
echo ""
|
||||||
|
echo "Usage:"
|
||||||
|
echo "$ sh dbFactory.sh /path/to/new/dbfile.db3"
|
||||||
|
echo ""
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
target=$1
|
||||||
|
|
||||||
|
cat lib/Compose.pre \
|
||||||
|
| grep -i '^<multi' \
|
||||||
|
| grep -v '<dead' \
|
||||||
|
| grep -v '<U....>' \
|
||||||
|
| grep -v '<U.....>' \
|
||||||
|
| sed --regexp-extended 's/(.*>)\s*:\s\"(.*)\"\s*(\S*)\s*#\s*(.*)/\1+\2+\3+\L\4/' \
|
||||||
|
> /tmp/compose.psv
|
||||||
|
|
||||||
|
sqlite3 $target ".read lib/initDb.sql"
|
|
@ -14,11 +14,11 @@ $ grep -i '^<multi' Compose.pre | grep -v '<dead' | grep -v '<U....>' | grep -v
|
||||||
|
|
||||||
Convert to psv
|
Convert to psv
|
||||||
--------------
|
--------------
|
||||||
- Plus-separated-values because + is not used in the file.
|
- Plus-separated-values because + is not used elsewhere in the file.
|
||||||
- Remove tabs and squish spaces.
|
|
||||||
- Regex to extract the fields into psv.
|
- Regex to extract the fields into psv.
|
||||||
|
- The second description is all-uppper, we'll convert it to all lower.
|
||||||
|
|
||||||
$ cat Compose.usable | tr -d "\t" | tr -s " " | sed --regexp-extended 's/(\S*)\s*: \"(.*)\"\s*(\S*)\s*#\s*(.*)/\1+\2+\3+\4/' > Compose.psv
|
$ sed --regexp-extended 's/(.*>)\s*:\s\"(.*)\"\s*(\S*)\s*#\s*(.*)/\1+\2+\3+\L\4/' Compose.usable > Compose.psv
|
||||||
|
|
||||||
Load into SQLite
|
Load into SQLite
|
||||||
----------------
|
----------------
|
40
lib/initDb.sql
Normal file
40
lib/initDb.sql
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
-- A table for the sequences
|
||||||
|
CREATE TABLE "keySequences" (
|
||||||
|
"keySequence" TEXT NOT NULL,
|
||||||
|
"utfCharacter" TEXT NOT NULL,
|
||||||
|
"desc1" TEXT,
|
||||||
|
"desc2" TEXT
|
||||||
|
);
|
||||||
|
|
||||||
|
-- A table for characters we already sent.
|
||||||
|
CREATE TABLE "alreadySent" (
|
||||||
|
"keySequenceROWID" INTEGER,
|
||||||
|
"timestamp" INTEGER
|
||||||
|
);
|
||||||
|
|
||||||
|
-- A view with yet unsent characters
|
||||||
|
CREATE VIEW stillAvailable (
|
||||||
|
keySequenceROWID,
|
||||||
|
keySequence,
|
||||||
|
utfCharacter,
|
||||||
|
desc1,
|
||||||
|
desc2
|
||||||
|
)
|
||||||
|
AS
|
||||||
|
SELECT ROWID, keySequence, utfCharacter, desc1, desc2
|
||||||
|
FROM keySequences
|
||||||
|
WHERE ROWID NOT IN (
|
||||||
|
SELECT keySequenceROWID
|
||||||
|
FROM alreadySent
|
||||||
|
);
|
||||||
|
|
||||||
|
-- A table with some entry-phrases:
|
||||||
|
CREATE TABLE "phrases" (
|
||||||
|
"phrase" TEXT
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Load Data
|
||||||
|
.mode csv
|
||||||
|
.separator "+"
|
||||||
|
.import lib/phrases.txt phrases
|
||||||
|
.import /tmp/compose.psv keySequences
|
Binary file not shown.
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue