diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc21c62 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +data/* +.vscode/* \ No newline at end of file diff --git a/README.md b/README.md index 475dbe7..86a312a 100644 --- a/README.md +++ b/README.md @@ -1 +1,10 @@ -# pb-rozklad \ No newline at end of file +# pb-rozklad + +## Wymagane: +Shell: curl, diff, imagemagick (convert, compare), ghostscript + +Python3: virtualenv, fbchat, pyotp + +## Konfiguracja: +- Odblokowanie konwersji PDF w ImageMagick: https://stackoverflow.com/a/59193253 +- Problemy z HTTPS na PB: https://imlc.me/dh-key-too-small \ No newline at end of file diff --git a/config-sample.txt b/config-sample.txt new file mode 100644 index 0000000..2814ef2 --- /dev/null +++ b/config-sample.txt @@ -0,0 +1,17 @@ +PDF_URL="https://degra.wi.pb.edu.pl/rozklady/doc/isi2.pdf" +PDF_DATE_FORMAT="+isi2_%F_%T.pdf" +ARCHIVE_WAYBACK=true +ARCHIVE_LOCAL=true + + +DC_ENABLED=true +DC_WEBHOOK="https://discordapp.com/api/webhooks/123456789012345678/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +DC_MESSAGE="nowy rozkład:" + + +FB_ENABLED=true +FB_EMAIL="mail@mail.com" +FB_PASS="hunter2" +FB_TOTP="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" +FB_GROUP_ID="1234567890123456" +FB_MESSAGE="nowy rozkład:" \ No newline at end of file diff --git a/main.sh b/main.sh new file mode 100755 index 0000000..85c24a3 --- /dev/null +++ b/main.sh @@ -0,0 +1,70 @@ +#!/bin/bash +if (( $# != 1 )); then + echo "Usage:" + echo $0 "config-file" + exit 1 +fi +source $1 + +script_path=$(dirname $(realpath $0)) +cd $script_path +mkdir -p "data/workdir" +if [ $ARCHIVE_LOCAL = true ]; then mkdir -p "data/archive"; fi +cd "data/workdir" + + +http_status=$(curl -o /dev/null -LIsw "%{http_code}" $PDF_URL) +if [ $http_status -ne 200 ]; then + echo "Error: server returned status code" $http_status + exit 1 +fi +curl -o "current.pdf" $PDF_URL -s + +if [ -r "previous.pdf" ]; then + diff "previous.pdf" "current.pdf" + diff_exitcode=$? + if [ $diff_exitcode -eq 0 ]; then + rm "current.pdf" + exit 0 + elif [ $diff_exitcode -ne 1 ]; then + echo "Diff exited with code" $diff_exitcode + exit 1 + fi +else + diff_exitcode=-1 +fi + +convert -flatten -density 400 "current.pdf" "current.png" +if [ $diff_exitcode -eq 1 ]; then + convert -flatten -density 400 "previous.pdf" "previous.png" + compare "current.png" "previous.png" "comparison.png" +fi + +# nicer name for Discord upload and local archive +pdf_nice="$(date $PDF_DATE_FORMAT)" +ln -s "current.pdf" $pdf_nice + +if [ $DC_ENABLED = true ]; then + export DC_WEBHOOK DC_MESSAGE pdf_nice diff_exitcode + bash $script_path"/msg_discord.sh" +fi +if [ $FB_ENABLED = true ]; then + export FB_ENABLED FB_EMAIL FB_PASS FB_TOTP FB_GROUP_ID FB_MESSAGE diff_exitcode + source $script_path"/venv/bin/activate" + python3 $script_path"/msg_facebook.py" +fi + +if [ $ARCHIVE_WAYBACK = true ]; then + curl -o /dev/null "https://web.archive.org/save/$ROZKLAD_URL" -w %{url_effective} -Ls +fi +if [ $ARCHIVE_LOCAL = true ]; then + cp $pdf_nice $script_path"/data/archive/" +fi + +# cleanup +rm "current.png" +if [ $diff_exitcode -eq 1 ]; then + rm "previous.png" "comparison.png" +fi +rm $pdf_nice +mv "current.pdf" "previous.pdf" \ No newline at end of file diff --git a/msg_discord.sh b/msg_discord.sh new file mode 100755 index 0000000..f177617 --- /dev/null +++ b/msg_discord.sh @@ -0,0 +1,11 @@ +#!/bin/bash +genpostdata () { + cat << EOF +{"content": "$DC_MESSAGE"} +EOF +} + +curl -o /dev/null -H "Content-Type: application/json" -d "$(genpostdata)" $DC_WEBHOOK -s +curl -o /dev/null -F "data=@current.png" $DC_WEBHOOK -s +if [ $diff_exitcode -eq 1 ]; then curl -o /dev/null -F "data=@comparison.png" $DC_WEBHOOK -s; fi +curl -o /dev/null -F "data=@"$pdf_nice $DC_WEBHOOK -s \ No newline at end of file diff --git a/msg_facebook.py b/msg_facebook.py new file mode 100755 index 0000000..ac48e10 --- /dev/null +++ b/msg_facebook.py @@ -0,0 +1,14 @@ +import os +import fbchat +import pyotp + +totp = pyotp.TOTP(os.environ["FB_TOTP"]) +class CustomClient(fbchat.Client): + def on2FACode(self): + return totp.now() +client = CustomClient(os.environ["FB_EMAIL"], os.environ["FB_PASS"]) + +client.send(fbchat.Message(text=os.environ["FB_MESSAGE"]), thread_id=os.environ["FB_GROUP_ID"], thread_type=fbchat.ThreadType.GROUP) +client.sendLocalFiles("current.png", thread_id=os.environ["FB_GROUP_ID"], thread_type=fbchat.ThreadType.GROUP) +if os.environ["diff_exitcode"] == "1": + client.sendLocalFiles("comparison.png", thread_id=os.environ["FB_GROUP_ID"], thread_type=fbchat.ThreadType.GROUP) \ No newline at end of file