Last active
July 1, 2022 10:27
-
-
Save lc-at/c8e97609dacafb961047f8ee381028fb to your computer and use it in GitHub Desktop.
notigrade: a watchdog for SIMASTER KHS page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import logging | |
import sys | |
import time | |
import requests | |
from simaster import get_simaster_session | |
KHS_URL = 'https://simaster.ugm.ac.id/akademik/mhs_khs/view' | |
MSG_FORMAT = 'Simaster KHS page watchdog new count: %d' | |
logging.basicConfig() | |
logger = logging.getLogger('notigrade') | |
logger.setLevel(logging.DEBUG) | |
def send_notification(ntfy_url, count): | |
text = MSG_FORMAT % count | |
logger.info('Sending notification: %s', text) | |
resp = requests.post(ntfy_url, data=text) | |
logger.debug('Ntfy.sh response: %r', resp.text) | |
def main(username, password, ntfy_url, interval): | |
interval = int(interval) | |
last_count = 0 | |
while True: | |
logger.info('Getting a SIMASTER session') | |
try: | |
ses = get_simaster_session(username, password) | |
logger.info('Requesting the KHS page') | |
resp = ses.get(KHS_URL) | |
except Exception as e: | |
logger.warn('Exception occured: %r', e) | |
continue | |
count = len(re.findall( | |
r'<td.+?>[0-4]...</td>', resp.text, re.M | re.I)) | |
logger.debug('Count = %d, Last count = %d', count, last_count) | |
if last_count != count: | |
last_count = count | |
send_notification(ntfy_url, count) | |
logger.info('Sleeping for %d seconds', interval) | |
time.sleep(interval) | |
if __name__ == '__main__': | |
if len(sys.argv) != 5: | |
print( | |
f'usage: {sys.argv[0]} <username> <password> <ntfy_url> <interval>') | |
sys.exit(-1) | |
main(*sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import string | |
import requests | |
LOGIN_URL = "https://simaster.ugm.ac.id/services/simaster/service_login" | |
HEADERS = {"UGMFWSERVICE": "1", "User-Agent": "SimasterICS/1.0.0"} | |
def get_simaster_session(username, password): | |
ses = requests.Session() | |
req = ses.post( | |
LOGIN_URL, | |
data={ | |
"aId": "".join(random.choice(string.hexdigits) for _ in range(16)).lower(), | |
"username": username, | |
"password": password, | |
}, | |
headers=HEADERS, | |
) | |
if req.status_code != 200: | |
return None | |
return ses |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment