From 484675bec76ff34da5b70926cf5a1b7d89249720 Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Mon, 4 Nov 2024 00:18:58 +0000 Subject: [PATCH] Add rudimentary sniper script --- cli_sniper.py | 18 ++++++++++++++++++ requirements.txt | 1 + sniper.py | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 cli_sniper.py create mode 100644 requirements.txt create mode 100644 sniper.py diff --git a/cli_sniper.py b/cli_sniper.py new file mode 100644 index 0000000..c612fb1 --- /dev/null +++ b/cli_sniper.py @@ -0,0 +1,18 @@ +import sys +from sniper import Sniper + +class CLISniper(Sniper): + UPDATE_MESSAGE = "The detected accommodation page is different to the previous one, indicating a possible change in application status! Please check the accommodation page as soon as possbible!" + def on_change(self): + print(self.UPDATE_MESSAGE) + + def on_same(self): + print("No change from last check...", file=sys.stderr) + +def main(): + print("Now running Accommodation sniper...", file=sys.stderr) + sniper = CLISniper() + sniper.loop() + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..663bd1f --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests \ No newline at end of file diff --git a/sniper.py b/sniper.py new file mode 100644 index 0000000..f616131 --- /dev/null +++ b/sniper.py @@ -0,0 +1,47 @@ +from abc import ABC, abstractmethod +import time +from typing import Optional +import requests +import difflib + +ACCOMMODATION_URL = "https://warwick.ac.uk/services/accommodation/students/current/" +def get_accommodation_page() -> Optional[str]: + r = requests.get(ACCOMMODATION_URL) + if r is None: + return None + else: + return r.text + +def get_difference(page_before: str, page_after: str) -> Optional[list[str]]: + if page_before == page_after: + return None + + return [*difflib.context_diff(page_before.splitlines(), page_after.splitlines())] + +THIRTY_MINUTES = 30 * 60 +TIMEOUT = THIRTY_MINUTES +class Sniper(ABC): + previous_page: str + + def __init__(self): + self.previous_page = get_accommodation_page() + + def update(self) -> None: + current_page = get_accommodation_page() + if get_difference(self.previous_page, current_page): + self.on_change() + else: + self.on_same() + self.previous_page = current_page + + @abstractmethod + def on_change(self) -> None: + pass + + def on_same(self) -> None: + pass + + def loop(self): + while True: + time.sleep(TIMEOUT) + self.update()