Skip to content

Commit

Permalink
Initial code commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Przemek authored and Przemek committed Sep 11, 2020
1 parent f328fc9 commit 35eec17
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
data/*
.vscode/*
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
# pb-rozklad
# 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
17 changes: 17 additions & 0 deletions config-sample.txt
Original file line number Diff line number Diff line change
@@ -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:"
70 changes: 70 additions & 0 deletions main.sh
Original file line number Diff line number Diff line change
@@ -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"
11 changes: 11 additions & 0 deletions msg_discord.sh
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions msg_facebook.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 35eec17

Please sign in to comment.