Add rudimentary sniper script
This commit is contained in:
parent
1df1fedbe8
commit
484675bec7
18
cli_sniper.py
Normal file
18
cli_sniper.py
Normal 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
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
requests
|
47
sniper.py
Normal file
47
sniper.py
Normal 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()
|
Loading…
Reference in New Issue
Block a user