Skip to content

Instantly share code, notes, and snippets.

@alexshpilkin
Last active Apr 24, 2019
Embed
What would you like to do?
Add diffs to results posts
#!/bin/sh -eu
# NB: Contains literal tabs
caption() {
python -c '
from os import getenv
from sys import argv
from telegram import Bot
from telegram.error import BadRequest, TimedOut
bot = Bot(token=getenv("TOKEN"))
while True:
try:
bot.edit_message_caption(chat_id=getenv("CHAT"),
message_id=argv[1],
caption=argv[2],
parse_mode="HTML")
except BadRequest:
break
except TimedOut:
pass
else:
break
' "$@"
}
SG="lines%10==1 && lines%100!=11"
DU="lines%10>=2 && lines%10<=4 && (lines%100<10 || lines%100>=20)"
ROOT=https://alexshpilkin.github.io/uk-pres2019/
DIFF="diff -u1 -F^region"
prev=
( cd Updates; ls results.*; ) | \
while read -r curr; do
if [ -z "$prev" ]; then prev=$curr; continue; fi # First file
diff=${curr%.tsv}.html
if [ -f "Diffs/$diff" ]; then prev=$curr; continue; fi # Exported
echo "export $diff"
tac Updates/.hat | while IFS=' ' read -r msgid digest file; do
if [ "$file" = "$curr" ]; then
if [ "$file" != "$curr" ] || [ "$msgid" = ! ]; then
echo "zombie $curr"; continue
fi
lines=$(diff -y --suppress-common-lines \
"Updates/$prev" "Updates/$curr" | \
wc -l)
$DIFF --color=always "Updates/$prev" "Updates/$curr" | \
/usr/share/git/diff-highlight/diff-highlight | \
aha -t "$DIFF $prev $curr" | tr -d '\r' > "Diffs/$diff"
cd Diffs
git add "$diff"
git commit -m "Add $diff"
git push
cd ..
if (( $SG )); then caption="$lines изменение"
elif (( $DU )); then caption="$lines изменения"
else caption="$lines изменений"
fi
caption "$msgid" "<a href='$ROOT$diff'>$caption</a>"
fi
done
prev=$curr
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment