From 41d494eab4ab810e792fa583a865501d9beedfe8 Mon Sep 17 00:00:00 2001
From: filifa <filifa.breath652@8alias.com>
Date: Sun, 4 Aug 2024 15:31:07 -0500
Subject: [PATCH] add scripts for start and end of games

---
 mlbfinalpost.sh | 35 +++++++++++++++++++++++++++++++++++
 mlbstartpost.sh | 35 +++++++++++++++++++++++++++++++++++
 postfinal.sql   | 19 +++++++++++++++++++
 poststart.sql   | 21 +++++++++++++++++++++
 4 files changed, 110 insertions(+)
 create mode 100755 mlbfinalpost.sh
 create mode 100755 mlbstartpost.sh
 create mode 100644 postfinal.sql
 create mode 100644 poststart.sql

diff --git a/mlbfinalpost.sh b/mlbfinalpost.sh
new file mode 100755
index 0000000..4566de0
--- /dev/null
+++ b/mlbfinalpost.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+set -e
+
+while getopts 'd:a:' opt
+do
+	case $opt in
+		d)
+			db=$OPTARG
+			;;
+		a)
+			account=$OPTARG
+			;;
+		?)
+			exit 1
+			;;
+	esac
+done
+
+if [[ -z $db ]]
+then
+	echo "$0:" '-d is required' >&2
+	exit 1
+fi
+
+if [[ -z $account ]]
+then
+	echo "$0:" '-a is required' >&2
+	exit 1
+fi
+
+# format each row of data retreived from the select statement and pipe to toot
+fmt='printf "Final:\n\n%s %s\n%s %s\n\n#baseball #live\n", $1, $2, $3, $4'
+post="\"toot post --using $account\""
+sqlite3 $db < poststart.sql | awk -F  "{$fmt | $post; close($post)}"
diff --git a/mlbstartpost.sh b/mlbstartpost.sh
new file mode 100755
index 0000000..1cf9fca
--- /dev/null
+++ b/mlbstartpost.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+set -e
+
+while getopts 'd:a:' opt
+do
+	case $opt in
+		d)
+			db=$OPTARG
+			;;
+		a)
+			account=$OPTARG
+			;;
+		?)
+			exit 1
+			;;
+	esac
+done
+
+if [[ -z $db ]]
+then
+	echo "$0:" '-d is required' >&2
+	exit 1
+fi
+
+if [[ -z $account ]]
+then
+	echo "$0:" '-a is required' >&2
+	exit 1
+fi
+
+# format each row of data retreived from the select statement and pipe to toot
+fmt='printf "Starting soon:\n\n%s (%s-%s) @\n%s (%s-%s)\n\n#baseball #live\n", $1, $2, $3, $4, $5, $6'
+post="\"toot post --using $account\""
+sqlite3 $db < poststart.sql | awk -F  "{$fmt | $post; close($post)}"
diff --git a/postfinal.sql b/postfinal.sql
new file mode 100644
index 0000000..2a83310
--- /dev/null
+++ b/postfinal.sql
@@ -0,0 +1,19 @@
+.separator 
+
+begin;
+select
+json ->> 'teams' ->> 'away' ->> 'team' ->> 'name',
+json ->> 'teams' ->> 'away' ->> 'score',
+json ->> 'teams' ->> 'home' ->> 'team' ->> 'name',
+json ->> 'teams' ->> 'home' ->> 'score'
+from games
+where
+state = 'Final' and
+posted = 0;
+
+update games
+set posted = 1
+where
+state = 'Final' and
+posted = 0;
+commit;
diff --git a/poststart.sql b/poststart.sql
new file mode 100644
index 0000000..500924e
--- /dev/null
+++ b/poststart.sql
@@ -0,0 +1,21 @@
+.separator 
+
+begin;
+select
+json ->> 'teams' ->> 'away' ->> 'team' ->> 'name',
+json ->> 'teams' ->> 'away' ->> 'leagueRecord' ->> 'wins',
+json ->> 'teams' ->> 'away' ->> 'leagueRecord' ->> 'losses',
+json ->> 'teams' ->> 'home' ->> 'team' ->> 'name',
+json ->> 'teams' ->> 'home' ->> 'leagueRecord' ->> 'wins',
+json ->> 'teams' ->> 'home' ->> 'leagueRecord' ->> 'losses'
+from games
+where
+state = 'Warmup' and
+posted = 0;
+
+update games
+set posted = 1
+where
+state = 'Warmup' and
+posted = 0;
+commit;