meta: remove s+ow (#60)
* meta: s+ow is now a sister project * deps(poetry): update and lock
This commit is contained in:
parent
c7d8f81654
commit
56be6e4748
206
README.md
206
README.md
|
@ -5,7 +5,6 @@ surplus is a Python script to convert
|
||||||
to iOS Shortcuts-like shareable text.
|
to iOS Shortcuts-like shareable text.
|
||||||
|
|
||||||
- [installation](#installation)
|
- [installation](#installation)
|
||||||
- [on Termux: surplus on wheels](#on-termux-surplus-on-wheels)
|
|
||||||
- [usage](#usage)
|
- [usage](#usage)
|
||||||
- [command-line usage](#command-line-usage)
|
- [command-line usage](#command-line-usage)
|
||||||
- [example api usage](#example-api-usage)
|
- [example api usage](#example-api-usage)
|
||||||
|
@ -38,217 +37,24 @@ Central, Singapore
|
||||||
for most, you can install surplus built from the latest stable release:
|
for most, you can install surplus built from the latest stable release:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
pip install https://github.com/markjoshwel/surplus/releases/latest/download/surplus-latest-py3-none-any.whl
|
pipx install https://github.com/markjoshwel/surplus/releases/latest/download/surplus-latest-py3-none-any.whl
|
||||||
```
|
```
|
||||||
|
|
||||||
or directly from the repository using pip:
|
or directly from the repository using pip:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
pip install git+https://github.com/markjoshwel/surplus.git@main
|
pipx install git+https://github.com/markjoshwel/surplus.git@main
|
||||||
```
|
```
|
||||||
|
|
||||||
|
**Termux users:** consider [surplus on wheels](https://github.com/markjoshwel/surplus-on-wheels),
|
||||||
|
a sister project that allows you to run surplus regularly throughout the day and send it
|
||||||
|
to someone on a messaging platform.
|
||||||
|
|
||||||
surplus is also a public domain dedicated [single python file](surplus/surplus.py), so
|
surplus is also a public domain dedicated [single python file](surplus/surplus.py), so
|
||||||
feel free to grab that and embed it into your own program as you see fit.
|
feel free to grab that and embed it into your own program as you see fit.
|
||||||
|
|
||||||
see [licence](#licence) for licensing information.
|
see [licence](#licence) for licensing information.
|
||||||
|
|
||||||
### on Termux: surplus on wheels
|
|
||||||
|
|
||||||
surplus on wheels (s+ow) is a pure shell script to get your location using
|
|
||||||
`termux-location`, process it through surplus, and send it to a WhatsApp user/group using a
|
|
||||||
[modified mdtest demonstration binary from the tulir/whatsmeow project](https://github.com/markjoshwel/whatsmeow-termux/tree/main/mdtest).
|
|
||||||
|
|
||||||
> [!IMPORTANT]
|
|
||||||
> if you just want to use surplus by itself, follow the normal installation guide above.
|
|
||||||
|
|
||||||
there are two ways to install and setup s+ow:
|
|
||||||
|
|
||||||
- [by itself](#by-itself)
|
|
||||||
- or [with an hourly cronjob](#with-an-hourly-cronjob)
|
|
||||||
|
|
||||||
see [s+ow usage instructions here](#using-sow).
|
|
||||||
|
|
||||||
#### by itself
|
|
||||||
|
|
||||||
1. firstly install python and termux-api if you haven't already:
|
|
||||||
|
|
||||||
```text
|
|
||||||
pkg install python termux-api
|
|
||||||
```
|
|
||||||
|
|
||||||
also install the accompanying the Termux:API app from [F-Froid](https://f-droid.org/en/packages/com.termux.api/).
|
|
||||||
|
|
||||||
2. install surplus:
|
|
||||||
|
|
||||||
```text
|
|
||||||
pip install https://github.com/markjoshwel/surplus/releases/latest/download/surplus-latest-py3-none-any.whl
|
|
||||||
```
|
|
||||||
|
|
||||||
3. install the modified mdtest binary for aarch64:
|
|
||||||
|
|
||||||
```text
|
|
||||||
wget https://github.com/markjoshwel/whatsmeow-termux/releases/latest/download/mdtest.tar.gz
|
|
||||||
tar -xvf mdtest.tar.gz
|
|
||||||
chmod +x mdtest
|
|
||||||
mkdir -p ~/.local/bin/
|
|
||||||
mv mdtest ~/.local/bin/
|
|
||||||
rm mdtest.tar.gz
|
|
||||||
```
|
|
||||||
|
|
||||||
4. install surplus on wheels:
|
|
||||||
|
|
||||||
```text
|
|
||||||
mkdir -p ~/.local/bin/
|
|
||||||
curl https://raw.githubusercontent.com/markjoshwel/surplus/main/s+ow > ~/.local/bin/s+ow
|
|
||||||
chmod +x ~/.local/bin/s+ow
|
|
||||||
```
|
|
||||||
|
|
||||||
if `~/.local/bin` is not in your `$PATH`, add the following to your shell's rc file:
|
|
||||||
|
|
||||||
```shell
|
|
||||||
export PATH="$HOME/.local/bin:$PATH"
|
|
||||||
```
|
|
||||||
|
|
||||||
#### with an hourly cronjob
|
|
||||||
|
|
||||||
> [!IMPORTANT]
|
|
||||||
> these instructions rely on following the previous instructions, and assumes that s+ow works.
|
|
||||||
|
|
||||||
1. install necessary packages to run cron jobs:
|
|
||||||
|
|
||||||
```text
|
|
||||||
pkg install cronie termux-services
|
|
||||||
```
|
|
||||||
|
|
||||||
2. restart termux and start the cron service:
|
|
||||||
|
|
||||||
```text
|
|
||||||
sv-enable cron
|
|
||||||
```
|
|
||||||
|
|
||||||
3. setup the cron job:
|
|
||||||
|
|
||||||
> [!IMPORTANT]
|
|
||||||
> fill in the `JID_NOMINAL_TARGET` and `JID_ERRORED_TARGET` variables before running s+ow.
|
|
||||||
> [(see using s+ow)](#using-sow)
|
|
||||||
|
|
||||||
run the following command:
|
|
||||||
|
|
||||||
```text
|
|
||||||
crontab -e
|
|
||||||
```
|
|
||||||
|
|
||||||
and add the following text:
|
|
||||||
|
|
||||||
```text
|
|
||||||
59 * * * * JID_NOMINAL_TARGET="" JID_ERRORED_TARGET="" LOCATION_PRIORITISE_NETWORK=n SPOW_CRON=y ~/.local/bin/s+ow
|
|
||||||
```
|
|
||||||
|
|
||||||
this will run s+ow every hour, a minute before the hour.
|
|
||||||
|
|
||||||
surplus when given `SPOW_CRON=y` will detect that it is being run as a cron job, and
|
|
||||||
delay itself appropriately, thus the job is ran at `59` instead of `0`.
|
|
||||||
|
|
||||||
modify the variables as per your needs.
|
|
||||||
see [using s+ow](#using-sow) for more information.
|
|
||||||
|
|
||||||
#### using s+ow
|
|
||||||
|
|
||||||
for first-time setup of mdtest, run the following command and pair your WhatsApp account
|
|
||||||
with mdtest:
|
|
||||||
|
|
||||||
```text
|
|
||||||
~/.local/bin/s+ow mdtest
|
|
||||||
```
|
|
||||||
|
|
||||||
wait for mdtest to sync with WhatsApp. you can safely leave after a minute or after the
|
|
||||||
console stops moving. whichever comes first.
|
|
||||||
|
|
||||||
s+ow uses two environment variables:
|
|
||||||
|
|
||||||
1. `JID_NOMINAL_TARGET`
|
|
||||||
JID of the WhatsApp user/group to send the location to if everything runs correctly.
|
|
||||||
|
|
||||||
2. `JID_ERRORED_TARGET`
|
|
||||||
JID of the WhatsApp user/group to send the stderr/logs to if something goes wrong.
|
|
||||||
|
|
||||||
3. `SPOW_CRON`
|
|
||||||
set as non-empty to declare that s+ow is being run as a cron job.
|
|
||||||
cron jobs are run thirty seconds in advance to attempt to display surplus output
|
|
||||||
on time as waiting for a GPS lock may be slow.
|
|
||||||
|
|
||||||
4. `LOCATION_PRIORITISE_NETWORK`
|
|
||||||
set as non-empty to declare that s+ow can just use network location instead of GPS
|
|
||||||
if GPS is taking too long.
|
|
||||||
you should only turn this on if punctuality means that much to you, or you’re in a
|
|
||||||
country with cell towers close by or everywhere, like Singapore.
|
|
||||||
|
|
||||||
setting it to `n` will also be treated as empty.
|
|
||||||
|
|
||||||
the JIDs can be obtained by sending a message to the user/group, while running
|
|
||||||
`s+ow mdtest`, and examining the output for your message. JIDs are email address-like
|
|
||||||
strings.
|
|
||||||
|
|
||||||
you can fake your s+ow messages by either:
|
|
||||||
|
|
||||||
1. setting a dummy `last` file in s+ow cache
|
|
||||||
|
|
||||||
`$HOME/.cache/s+ow/last` is used as the fallback response when a part of s+ow (either
|
|
||||||
`termux-location` or `surplus` errors out). you can set this file to whatever you want
|
|
||||||
and just disable location permissions for Termux.
|
|
||||||
|
|
||||||
2. setting a `fake` file in s+ow cache
|
|
||||||
|
|
||||||
> [!IMPORTANT]
|
|
||||||
> this is currently unimplemented.
|
|
||||||
|
|
||||||
you can also write text to `$HOME/.cache/s+ow/fake` to fake upcoming messages. the file
|
|
||||||
is delimited by two newlines. as such, arrange the file like so:
|
|
||||||
|
|
||||||
```text
|
|
||||||
The Clementi Mall
|
|
||||||
3155 Commonwealth Avenue West
|
|
||||||
Westpeak Terrace
|
|
||||||
129588
|
|
||||||
Southwest, Singapore
|
|
||||||
|
|
||||||
Westgate
|
|
||||||
3 Gateway Drive
|
|
||||||
Jurong East
|
|
||||||
608532
|
|
||||||
Southwest, Singapore
|
|
||||||
|
|
||||||
...
|
|
||||||
```
|
|
||||||
|
|
||||||
on every run of s+ow, the first group of lines will be consumed, and the file will be
|
|
||||||
updated with the remaining lines. if the file is empty, it will be deleted.
|
|
||||||
|
|
||||||
#### quick install scripts
|
|
||||||
|
|
||||||
> [!WARNING]
|
|
||||||
> these scripts assume you're starting from a fresh base install of Termux.
|
|
||||||
> if you have already cron jobs, then manually carry out the instructiions in
|
|
||||||
> [with an hourly cronjob](#with-an-hourly-cronjob).
|
|
||||||
|
|
||||||
1. setup s+ow:
|
|
||||||
|
|
||||||
```text
|
|
||||||
curl https://raw.githubusercontent.com/markjoshwel/surplus/main/termux-s+ow-setup | sh
|
|
||||||
```
|
|
||||||
|
|
||||||
2. restart termux
|
|
||||||
|
|
||||||
3. setup cron job:
|
|
||||||
|
|
||||||
```text
|
|
||||||
curl https://raw.githubusercontent.com/markjoshwel/surplus/main/termux-s+ow-setup-cron | sh
|
|
||||||
```
|
|
||||||
|
|
||||||
the script will run `crontab -e`, and you can then edit the variables as per your
|
|
||||||
needs.
|
|
||||||
see [using s+ow](#using-sow) for more information.
|
|
||||||
|
|
||||||
## usage
|
## usage
|
||||||
|
|
||||||
### command-line usage
|
### command-line usage
|
||||||
|
|
20
poetry.lock
generated
20
poetry.lock
generated
|
@ -157,13 +157,13 @@ timezone = ["pytz"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ipython"
|
name = "ipython"
|
||||||
version = "8.17.1"
|
version = "8.17.2"
|
||||||
description = "IPython: Productive Interactive Computing"
|
description = "IPython: Productive Interactive Computing"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = ">=3.9"
|
python-versions = ">=3.9"
|
||||||
files = [
|
files = [
|
||||||
{file = "ipython-8.17.1-py3-none-any.whl", hash = "sha256:b4510d0a163c89c78cf81be68acc841dde4a8290b6ee3f3c9578ccdd2574553c"},
|
{file = "ipython-8.17.2-py3-none-any.whl", hash = "sha256:1e4d1d666a023e3c93585ba0d8e962867f7a111af322efff6b9c58062b3e5444"},
|
||||||
{file = "ipython-8.17.1.tar.gz", hash = "sha256:9e12020f4bf74f08631c0f033bb580e074fbe36c64903195f3e63b9c0a986cbe"},
|
{file = "ipython-8.17.2.tar.gz", hash = "sha256:126bb57e1895594bb0d91ea3090bbd39384f6fe87c3d57fd558d0670f50339bb"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[package.dependencies]
|
[package.dependencies]
|
||||||
|
@ -179,17 +179,17 @@ stack-data = "*"
|
||||||
traitlets = ">=5"
|
traitlets = ">=5"
|
||||||
|
|
||||||
[package.extras]
|
[package.extras]
|
||||||
all = ["black", "curio", "docrepr", "exceptiongroup", "ipykernel", "ipyparallel", "ipywidgets", "matplotlib", "matplotlib (!=3.2.0)", "nbconvert", "nbformat", "notebook", "numpy (>=1.22)", "pandas", "pickleshare", "pytest (<7)", "pytest (<7.1)", "pytest-asyncio", "qtconsole", "setuptools (>=18.5)", "sphinx (>=1.3)", "sphinx-rtd-theme", "stack-data", "testpath", "trio", "typing-extensions"]
|
all = ["black", "curio", "docrepr", "exceptiongroup", "ipykernel", "ipyparallel", "ipywidgets", "matplotlib", "matplotlib (!=3.2.0)", "nbconvert", "nbformat", "notebook", "numpy (>=1.22)", "pandas", "pickleshare", "pytest (<7)", "pytest (<7.1)", "pytest-asyncio (<0.22)", "qtconsole", "setuptools (>=18.5)", "sphinx (>=1.3)", "sphinx-rtd-theme", "stack-data", "testpath", "trio", "typing-extensions"]
|
||||||
black = ["black"]
|
black = ["black"]
|
||||||
doc = ["docrepr", "exceptiongroup", "ipykernel", "matplotlib", "pickleshare", "pytest (<7)", "pytest (<7.1)", "pytest-asyncio", "setuptools (>=18.5)", "sphinx (>=1.3)", "sphinx-rtd-theme", "stack-data", "testpath", "typing-extensions"]
|
doc = ["docrepr", "exceptiongroup", "ipykernel", "matplotlib", "pickleshare", "pytest (<7)", "pytest (<7.1)", "pytest-asyncio (<0.22)", "setuptools (>=18.5)", "sphinx (>=1.3)", "sphinx-rtd-theme", "stack-data", "testpath", "typing-extensions"]
|
||||||
kernel = ["ipykernel"]
|
kernel = ["ipykernel"]
|
||||||
nbconvert = ["nbconvert"]
|
nbconvert = ["nbconvert"]
|
||||||
nbformat = ["nbformat"]
|
nbformat = ["nbformat"]
|
||||||
notebook = ["ipywidgets", "notebook"]
|
notebook = ["ipywidgets", "notebook"]
|
||||||
parallel = ["ipyparallel"]
|
parallel = ["ipyparallel"]
|
||||||
qtconsole = ["qtconsole"]
|
qtconsole = ["qtconsole"]
|
||||||
test = ["pickleshare", "pytest (<7.1)", "pytest-asyncio", "testpath"]
|
test = ["pickleshare", "pytest (<7.1)", "pytest-asyncio (<0.22)", "testpath"]
|
||||||
test-extra = ["curio", "matplotlib (!=3.2.0)", "nbformat", "numpy (>=1.22)", "pandas", "pickleshare", "pytest (<7.1)", "pytest-asyncio", "testpath", "trio"]
|
test-extra = ["curio", "matplotlib (!=3.2.0)", "nbformat", "numpy (>=1.22)", "pandas", "pickleshare", "pytest (<7.1)", "pytest-asyncio (<0.22)", "testpath", "trio"]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "isort"
|
name = "isort"
|
||||||
|
@ -498,13 +498,13 @@ files = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wcwidth"
|
name = "wcwidth"
|
||||||
version = "0.2.8"
|
version = "0.2.9"
|
||||||
description = "Measures the displayed width of unicode strings in a terminal"
|
description = "Measures the displayed width of unicode strings in a terminal"
|
||||||
optional = false
|
optional = false
|
||||||
python-versions = "*"
|
python-versions = "*"
|
||||||
files = [
|
files = [
|
||||||
{file = "wcwidth-0.2.8-py2.py3-none-any.whl", hash = "sha256:77f719e01648ed600dfa5402c347481c0992263b81a027344f3e1ba25493a704"},
|
{file = "wcwidth-0.2.9-py2.py3-none-any.whl", hash = "sha256:9a929bd8380f6cd9571a968a9c8f4353ca58d7cd812a4822bba831f8d685b223"},
|
||||||
{file = "wcwidth-0.2.8.tar.gz", hash = "sha256:8705c569999ffbb4f6a87c6d1b80f324bd6db952f5eb0b95bc07517f4c1813d4"},
|
{file = "wcwidth-0.2.9.tar.gz", hash = "sha256:a675d1a4a2d24ef67096a04b85b02deeecd8e226f57b5e3a72dbb9ed99d27da8"},
|
||||||
]
|
]
|
||||||
|
|
||||||
[metadata]
|
[metadata]
|
||||||
|
|
387
s+ow
387
s+ow
|
@ -1,387 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# surplus on wheels (s+ow): a pure shell script to run surplus with mdtest using the termux-api
|
|
||||||
|
|
||||||
# shellcheck disable=SC2059
|
|
||||||
LOCATION_FALLBACK="%d%d%d\nSingapore?"
|
|
||||||
# shellcheck disable=SC2269
|
|
||||||
LOCATION_PRIORITISE_NETWORK="$LOCATION_PRIORITISE_NETWORK"
|
|
||||||
LOCATION_TIMEOUT=${LOCATION_TIMEOUT:-50}
|
|
||||||
|
|
||||||
# shellcheck disable=SC2269
|
|
||||||
JID_NOMINAL_TARGET="$JID_NOMINAL_TARGET"
|
|
||||||
# shellcheck disable=SC2269
|
|
||||||
JID_ERRORED_TARGET="$JID_ERRORED_TARGET"
|
|
||||||
|
|
||||||
MDTEST_BIN="$HOME/.local/bin/mdtest"
|
|
||||||
MDTEST_DIR="$HOME/.local/share/mdtest"
|
|
||||||
|
|
||||||
SPOW_CACHE_DIR="$HOME/.cache/s+ow"
|
|
||||||
|
|
||||||
# per-tool session logs
|
|
||||||
SPOW_NETLC_OUT="$SPOW_CACHE_DIR/location.net.json"
|
|
||||||
SPOW_GPSLC_OUT="$SPOW_CACHE_DIR/location.gps.json"
|
|
||||||
SPOW_LOCTN_OUT="$SPOW_CACHE_DIR/location.json"
|
|
||||||
SPOW_SPLUS_OUT="$SPOW_CACHE_DIR/surplus.out.log"
|
|
||||||
SPOW_SPLUS_ERR="$SPOW_CACHE_DIR/surplus.err.log"
|
|
||||||
|
|
||||||
# per-session collated logs
|
|
||||||
SPOW_SESH_OUT="$SPOW_CACHE_DIR/out.log"
|
|
||||||
SPOW_SESH_ERR="$SPOW_CACHE_DIR/err.log"
|
|
||||||
|
|
||||||
# per-week collated logs
|
|
||||||
SPOW_WEEK_PRE="$SPOW_CACHE_DIR/$(date +%Y)W$(date +"%V")"
|
|
||||||
SPOW_WEEK_OUT="$SPOW_WEEK_PRE.out.log"
|
|
||||||
SPOW_WEEK_ERR="$SPOW_WEEK_PRE.err.log"
|
|
||||||
|
|
||||||
# last successful surplus output
|
|
||||||
SPOW_LAST_OUT="$SPOW_CACHE_DIR/last"
|
|
||||||
|
|
||||||
# list of fakes
|
|
||||||
# shellcheck disable=SC2034
|
|
||||||
SPOW_FAKE_OUT="$SPOW_CACHE_DIR/fake"
|
|
||||||
|
|
||||||
# check for network location priority
|
|
||||||
if [ "$LOCATION_PRIORITISE_NETWORK" = "n" ]; then
|
|
||||||
LOCATION_PRIORITISE_NETWORK=""
|
|
||||||
fi
|
|
||||||
|
|
||||||
# check for cron status
|
|
||||||
if [ "$SPOW_CRON" = "n" ]; then
|
|
||||||
SPOW_CRON=""
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ensure commands exist
|
|
||||||
if ! command -v termux-location >/dev/null 2>&1; then
|
|
||||||
printf "s+ow: error: termux-location is not installed.\ninstall it with 'pkg install termux-api' and with installing the termux:api app from the play store or f-droid.\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ! command -v surplus >/dev/null 2>&1; then
|
|
||||||
printf "s+ow: error: surplus is not installed.\ninstall it with 'pip install https://github.com/markjoshwel/surplus/releases/latest/download/surplus-latest-py3-none-any.whl'\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
if ! command -v ~/.local/bin/mdtest >/dev/null 2>&1; then
|
|
||||||
printf "s+ow: error: mdtest is not installed.\ninstall it by getting a release or building it from source from https://github.com/markjoshwel/whatsmeow-termux\n"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# ensure directories
|
|
||||||
mkdir -p "$SPOW_CACHE_DIR" "$MDTEST_DIR"
|
|
||||||
|
|
||||||
# create new session logs
|
|
||||||
rm -f "$SPOW_LOCTN_OUT" "$SPOW_SPLUS_OUT" "$SPOW_SPLUS_ERR" \
|
|
||||||
"$SPOW_SESH_OUT" "$SPOW_SESH_ERR"
|
|
||||||
touch "$SPOW_NETLC_OUT" "$SPOW_GPSLC_OUT" "$SPOW_LOCTN_OUT" \
|
|
||||||
"$SPOW_SPLUS_OUT" "$SPOW_SPLUS_ERR" \
|
|
||||||
"$SPOW_SESH_OUT" "$SPOW_SESH_ERR" \
|
|
||||||
"$SPOW_WEEK_OUT" "$SPOW_WEEK_ERR"
|
|
||||||
|
|
||||||
status=0 # 0 is nominal
|
|
||||||
# 1 is an termux-location error
|
|
||||||
# 2 is a surplus error
|
|
||||||
|
|
||||||
# helper functions
|
|
||||||
|
|
||||||
locate() {
|
|
||||||
# spawn termux-location processes
|
|
||||||
(
|
|
||||||
termux-location -p "network" >"$SPOW_NETLC_OUT"
|
|
||||||
if [ -s "$SPOW_NETLC_OUT" ]; then
|
|
||||||
printf "net" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
else
|
|
||||||
printf "net?" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
fi
|
|
||||||
cat "$SPOW_NETLC_OUT" >>"$SPOW_SESH_OUT"
|
|
||||||
) &
|
|
||||||
tl_net_pid="$!"
|
|
||||||
sleep 1
|
|
||||||
(
|
|
||||||
termux-location -p "gps" >"$SPOW_GPSLC_OUT"
|
|
||||||
if [ -s "$SPOW_GPSLC_OUT" ]; then
|
|
||||||
printf "gps" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
else
|
|
||||||
printf "gps?" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
fi
|
|
||||||
cat "$SPOW_GPSLC_OUT" >>"$SPOW_SESH_OUT"
|
|
||||||
) &
|
|
||||||
tl_gps_pid="$!"
|
|
||||||
|
|
||||||
# wait until timeout or both finished
|
|
||||||
printf "running termux-location" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
while [ "$LOCATION_TIMEOUT" -gt 0 ]; do
|
|
||||||
# get process statuses
|
|
||||||
kill -0 "$tl_net_pid" >/dev/null 2>&1
|
|
||||||
tl_net_status="$?"
|
|
||||||
kill -0 "$tl_gps_pid" >/dev/null 2>&1
|
|
||||||
tl_gps_status="$?"
|
|
||||||
|
|
||||||
# break if both finished
|
|
||||||
if [ "$tl_net_status" -eq 1 ] && [ "$tl_gps_status" -eq 1 ]; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
|
|
||||||
# exception: if network is proritised: just use that
|
|
||||||
if [ "$tl_net_status" -eq 1 ] && [ -n "$LOCATION_PRIORITISE_NETWORK" ]; then
|
|
||||||
# break only if theres an actual response
|
|
||||||
if [ -s "$SPOW_NETLC_OUT" ]; then
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
# else just keep on waiting for gps to finish
|
|
||||||
fi
|
|
||||||
|
|
||||||
sleep 1
|
|
||||||
printf "." | tee -a "$SPOW_SESH_ERR"
|
|
||||||
LOCATION_TIMEOUT=$((LOCATION_TIMEOUT - 1))
|
|
||||||
done
|
|
||||||
if [ "$LOCATION_TIMEOUT" -eq 0 ]; then
|
|
||||||
printf " errored (timeout)\n" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
else
|
|
||||||
printf " nominal\n" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# check outputs
|
|
||||||
printf "determining output: " | tee -a "$SPOW_SESH_ERR"
|
|
||||||
if [ -s "$SPOW_NETLC_OUT" ] && [ -s "$SPOW_GPSLC_OUT" ]; then
|
|
||||||
printf "both succeeded, "
|
|
||||||
acc_net="$(grep "\"accuracy\"" <"$SPOW_NETLC_OUT" | awk -F ': ' '{print $2}' | tr -d ',')"
|
|
||||||
acc_gps="$(grep "\"accuracy\"" <"$SPOW_GPSLC_OUT" | awk -F ': ' '{print $2}' | tr -d ',')"
|
|
||||||
|
|
||||||
# compare accuracy
|
|
||||||
if awk -v n1="$acc_net" -v n2="$acc_gps" 'BEGIN { if (n1 < n2) exit 0; else exit 1; }'; then
|
|
||||||
printf "choosing network (%s < %s)" "$acc_net" "$acc_gps" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
cat "$SPOW_NETLC_OUT" >"$SPOW_LOCTN_OUT"
|
|
||||||
else
|
|
||||||
printf "choosing gps (%s < %s)" "$acc_gps" "$acc_net" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
cat "$SPOW_GPSLC_OUT" >"$SPOW_LOCTN_OUT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
cat "$SPOW_GPSLC_OUT" >"$SPOW_LOCTN_OUT"
|
|
||||||
else
|
|
||||||
# one or none succeeded
|
|
||||||
if [ -s "$SPOW_NETLC_OUT" ]; then
|
|
||||||
if [ -n "$LOCATION_PRIORITISE_NETWORK" ]; then
|
|
||||||
printf "using network (prioritised)" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
else
|
|
||||||
printf "using network" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
fi
|
|
||||||
cat "$SPOW_NETLC_OUT" >"$SPOW_LOCTN_OUT"
|
|
||||||
fi
|
|
||||||
if [ -s "$SPOW_GPSLC_OUT" ]; then
|
|
||||||
printf "using gps" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
cat "$SPOW_GPSLC_OUT" >"$SPOW_LOCTN_OUT"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
if [ ! -s "$SPOW_LOCTN_OUT" ]; then
|
|
||||||
printf "none (error)" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
fi
|
|
||||||
printf "\n" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
}
|
|
||||||
|
|
||||||
gensharetext() {
|
|
||||||
surplus -td "$1" >"$SPOW_SPLUS_OUT" 2>"$SPOW_SPLUS_ERR"
|
|
||||||
ret="$?"
|
|
||||||
cat "$SPOW_SPLUS_OUT" >>"$SPOW_SESH_OUT"
|
|
||||||
cat "$SPOW_SPLUS_ERR" >>"$SPOW_SESH_ERR"
|
|
||||||
return "$ret"
|
|
||||||
}
|
|
||||||
|
|
||||||
send() {
|
|
||||||
(cd "$MDTEST_DIR" && "$MDTEST_BIN" send "$1" "$2")
|
|
||||||
}
|
|
||||||
|
|
||||||
notify_start() {
|
|
||||||
termux-notification \
|
|
||||||
--priority "min" \
|
|
||||||
--ongoing \
|
|
||||||
--id "s+ow" \
|
|
||||||
--title "surplus on wheels" \
|
|
||||||
--content "s+ow has started running."
|
|
||||||
}
|
|
||||||
|
|
||||||
notify() {
|
|
||||||
# $1 is text
|
|
||||||
# $2 is attempt number (if any)
|
|
||||||
attempt_text="$1"
|
|
||||||
if [ $# -eq 2 ]; then
|
|
||||||
attempt_text="$1 (attempt $2)"
|
|
||||||
fi
|
|
||||||
termux-notification \
|
|
||||||
--priority "min" \
|
|
||||||
--ongoing \
|
|
||||||
--id "s+ow" \
|
|
||||||
--title "surplus on wheels" \
|
|
||||||
--content "$attempt_text"
|
|
||||||
}
|
|
||||||
|
|
||||||
notify_end() {
|
|
||||||
# $1 is done tuple
|
|
||||||
# $2 is sharetext
|
|
||||||
termux-notification \
|
|
||||||
--priority "min" \
|
|
||||||
--id "s+ow" \
|
|
||||||
--title "surplus on wheels" \
|
|
||||||
--content "$(printf 'Run has finished. %s\n\n%s' "$1" "$2")"
|
|
||||||
}
|
|
||||||
|
|
||||||
# program functions
|
|
||||||
|
|
||||||
mdtest() {
|
|
||||||
(cd "$MDTEST_DIR" && "$MDTEST_BIN")
|
|
||||||
}
|
|
||||||
|
|
||||||
run() {
|
|
||||||
notify_start
|
|
||||||
printf "[run! stdout (%s)]\n" "$(date)" >>"$SPOW_SESH_OUT"
|
|
||||||
printf "[run! stderr (%s)]\n" "$(date)" >>"$SPOW_SESH_ERR"
|
|
||||||
|
|
||||||
time_run_start="$(date +%s)"
|
|
||||||
|
|
||||||
# if cron: wait until its the new hour
|
|
||||||
if [ -n "$SPOW_CRON" ]; then
|
|
||||||
notify "Waiting for the 30th second to pass..."
|
|
||||||
printf "waiting for the 30th second to pass...\n"
|
|
||||||
while [ "$(date +'%S')" -lt 30 ]; do
|
|
||||||
printf " $(date)\n"
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
printf "proceeding\n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
time_locate_start="$(date +%s)"
|
|
||||||
|
|
||||||
# termux-location
|
|
||||||
location=""
|
|
||||||
for locate_run in 1 2 3; do # run three times in case :p
|
|
||||||
notify "Running termux-location" "$locate_run"
|
|
||||||
|
|
||||||
if [ "$locate_run" -gt "1" ]; then
|
|
||||||
LOCATION_TIMEOUT=75 locate
|
|
||||||
else
|
|
||||||
locate
|
|
||||||
fi
|
|
||||||
if [ ! -s "$SPOW_LOCTN_OUT" ]; then
|
|
||||||
# erroneous: is empty
|
|
||||||
echo "s+ow: error: failed to get location" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
status=1
|
|
||||||
else
|
|
||||||
# nominal: is not empty
|
|
||||||
location="$(cat "$SPOW_LOCTN_OUT")"
|
|
||||||
status=0
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
time_locate_end="$(date +%s)"
|
|
||||||
time_locate=$((time_locate_end - time_locate_start))
|
|
||||||
|
|
||||||
time_surplus_start="$(date +%s)"
|
|
||||||
|
|
||||||
# surplus
|
|
||||||
printf "running surplus... "
|
|
||||||
notify "Running surplus -td $location"
|
|
||||||
if [ "$status" -eq 0 ]; then
|
|
||||||
if gensharetext "$location"; then
|
|
||||||
# surplus ran nominally
|
|
||||||
cp "$SPOW_SPLUS_OUT" "$SPOW_LAST_OUT"
|
|
||||||
status=0
|
|
||||||
printf "nominal\n"
|
|
||||||
else
|
|
||||||
# something happened :^)
|
|
||||||
status=2
|
|
||||||
printf "errored\n"
|
|
||||||
fi
|
|
||||||
else
|
|
||||||
printf "skipped\n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
time_surplus_end="$(date +%s)"
|
|
||||||
time_surplus=$((time_surplus_end - time_surplus_start))
|
|
||||||
|
|
||||||
# if cron: wait until its the new hour
|
|
||||||
if [ -n "$SPOW_CRON" ]; then
|
|
||||||
notify "Waiting for the new hour..."
|
|
||||||
printf "waiting until the new hour...\n"
|
|
||||||
while [ "$(date +'%M')" -eq 59 ]; do
|
|
||||||
printf " $(date)\n"
|
|
||||||
sleep 1
|
|
||||||
done
|
|
||||||
printf "proceeding\n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
time_sendmsg_start="$(date +%s)"
|
|
||||||
|
|
||||||
# mdtest/send message
|
|
||||||
printf "sending message(s)... "
|
|
||||||
notify "Sending message(s)"
|
|
||||||
sent_type=0 # 0 for freshly made sharetext
|
|
||||||
# 1 for recycling a last location
|
|
||||||
# 2 for using fallback template
|
|
||||||
sharetext=""
|
|
||||||
if [ "$status" -eq 0 ]; then
|
|
||||||
# s+ow has behaved nominally until now, send as per normal
|
|
||||||
sharetext="$(cat "$SPOW_SPLUS_OUT")"
|
|
||||||
printf "\n"
|
|
||||||
send "$JID_NOMINAL_TARGET" "$sharetext"
|
|
||||||
else
|
|
||||||
# something has gone wrong, send an appropriate fallback
|
|
||||||
sharetext=""
|
|
||||||
if [ -s "$SPOW_LAST_OUT" ]; then
|
|
||||||
# use last successful location
|
|
||||||
sharetext="$(cat "$SPOW_LAST_OUT")"
|
|
||||||
sent_type=1
|
|
||||||
printf "using last...\n"
|
|
||||||
else
|
|
||||||
# no last location, use fallback
|
|
||||||
# shellcheck disable=SC2059
|
|
||||||
sharetext="$(printf "$LOCATION_FALLBACK" "$status" "$locate_run" "$sent_type")"
|
|
||||||
sent_type=2
|
|
||||||
printf "using fallback... \n"
|
|
||||||
fi
|
|
||||||
|
|
||||||
# send done info except
|
|
||||||
printf "(%d, %d, %d) [lc:%ds sp:%ds]\n" "$status" "$locate_run" "$sent_type" "$time_locate" "$time_surplus" >>"$SPOW_SESH_ERR"
|
|
||||||
|
|
||||||
send "$JID_NOMINAL_TARGET" "$sharetext"
|
|
||||||
send "$JID_ERRORED_TARGET" "$(cat "$SPOW_SESH_ERR")"
|
|
||||||
fi
|
|
||||||
|
|
||||||
time_sendmsg_end="$(date +%s)"
|
|
||||||
time_sendmsg=$((time_sendmsg_end - time_sendmsg_start))
|
|
||||||
|
|
||||||
time_run_end="$(date +%s)"
|
|
||||||
time_run=$((time_run_end - time_run_start))
|
|
||||||
|
|
||||||
done_info="$(printf "(%d, %d, %d) [lc:%ds sp:%ds sm:%ds - %ds]\n" "$status" "$locate_run" "$sent_type" "$time_locate" "$time_surplus" "$time_sendmsg" "$time_run")"
|
|
||||||
echo "done $done_info" | tee -a "$SPOW_SESH_ERR"
|
|
||||||
|
|
||||||
# cleanup
|
|
||||||
notify_end "$done_info" "$sharetext"
|
|
||||||
}
|
|
||||||
|
|
||||||
# script entry
|
|
||||||
|
|
||||||
if [ "$1" = "mdtest" ]; then
|
|
||||||
mdtest
|
|
||||||
elif [ -z "$1" ]; then
|
|
||||||
# ensure JID targets are set
|
|
||||||
if [ -z "$JID_NOMINAL_TARGET" ] || [ -z "$JID_ERRORED_TARGET" ]; then
|
|
||||||
echo "s+ow: error: JID_NOMINAL_TARGET and JID_ERRORED_TARGET are not set"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
run
|
|
||||||
else
|
|
||||||
echo "usage: $0 [mdtest]
|
|
||||||
|
|
||||||
surplus on wheels: a pure shell script to run surplus with mdtest using the termux-api
|
|
||||||
|
|
||||||
choices
|
|
||||||
$0 mdtest
|
|
||||||
run mdtest for testing or authentication
|
|
||||||
$0
|
|
||||||
run surplus on wheels normally"
|
|
||||||
fi
|
|
||||||
|
|
||||||
printf "%s\n\n" "$(cat "$SPOW_SESH_OUT")" >>"$SPOW_WEEK_OUT"
|
|
||||||
printf "%s\n\n" "$(cat "$SPOW_SESH_ERR")" >>"$SPOW_WEEK_ERR"
|
|
|
@ -1,27 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# get packages
|
|
||||||
yes | pkg upgrade
|
|
||||||
yes | pkg install python cronie termux-api termux-services wget
|
|
||||||
|
|
||||||
# install surplus
|
|
||||||
pip install https://github.com/markjoshwel/surplus/releases/latest/download/surplus-latest-py3-none-any.whl
|
|
||||||
|
|
||||||
# install whatsmeow
|
|
||||||
wget https://github.com/markjoshwel/whatsmeow-termux/releases/latest/download/mdtest.tar.gz
|
|
||||||
tar -xvf mdtest.tar.gz
|
|
||||||
chmod +x mdtest
|
|
||||||
mkdir -p ~/.local/bin/
|
|
||||||
mv mdtest ~/.local/bin/
|
|
||||||
rm mdtest.tar.gz
|
|
||||||
|
|
||||||
# install s+ow
|
|
||||||
mkdir -p ~/.local/bin/
|
|
||||||
curl https://raw.githubusercontent.com/markjoshwel/surplus/s+ow/s+ow > ~/.local/bin/s+ow
|
|
||||||
chmod +x ~/.local/bin/s+ow
|
|
||||||
|
|
||||||
# setup path
|
|
||||||
echo "export PATH=\$PATH:\$HOME/.local/bin/" >> ~/.profile
|
|
||||||
|
|
||||||
printf "\ns+ow setup complete\n"
|
|
|
@ -1,4 +0,0 @@
|
||||||
#!/bin/sh
|
|
||||||
sv-enable crond
|
|
||||||
printf "59 * * * *\tJID_NOMINAL_TARGET=\"\" JID_ERRORED_TARGET=\"\" LOCATION_PRIORITISE_NETWORK=n SPOW_CRON=y ~/.local/bin/s+ow\n" | crontab -
|
|
||||||
crontab -e
|
|
Loading…
Reference in a new issue