Add rudimentary sniper script

This commit is contained in:
Kiril Kovachev 2024-11-04 00:18:58 +00:00
parent 1df1fedbe8
commit 484675bec7
3 changed files with 66 additions and 0 deletions

18
cli_sniper.py Normal file
View File

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

1
requirements.txt Normal file
View File

@ -0,0 +1 @@
requests

47
sniper.py Normal file
View File

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