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()