first commit
This commit is contained in:
102
app/main.py
Normal file
102
app/main.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
from utils.common_utils import get_common_utils
|
||||
from utils.logging_setup import get_logger
|
||||
from utils.settings import get_settings
|
||||
from utils.cache_db import get_cache
|
||||
|
||||
import requests
|
||||
from dotenv import load_dotenv
|
||||
|
||||
logger = get_logger()
|
||||
settings = get_settings()
|
||||
utils = get_common_utils()
|
||||
|
||||
load_dotenv()
|
||||
|
||||
|
||||
CACHE_DIR = Path.cwd() / "config" / "cache.db"
|
||||
|
||||
def gotify(title:str="Test", message:str="testing msg",priority:int=5):
|
||||
"""
|
||||
Send a message to a Gotify server using a Bearer token.
|
||||
|
||||
Args:
|
||||
server_url (str): Base URL of the Gotify server (e.g., http://10.10.20.8:8080).
|
||||
token (str): Gotify app token for authentication.
|
||||
title (str): Title of the message.
|
||||
message (str): Body of the message.
|
||||
priority (int, optional): Message priority (1–10). Defaults to 5.
|
||||
|
||||
Returns:
|
||||
bool: True if the message was sent successfully, False otherwise.
|
||||
"""
|
||||
server_url = os.getenv("GOTIFY_URL")
|
||||
token = os.getenv("GOTIFY_TOKEN")
|
||||
|
||||
if not server_url or not token:
|
||||
print("[!] Missing GOTIFY_URL or GOTIFY_TOKEN in environment.")
|
||||
return False
|
||||
|
||||
url = f"{server_url.rstrip('/')}/message"
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {token}",
|
||||
"Content-Type": "application/json"
|
||||
}
|
||||
payload = {
|
||||
"title": title,
|
||||
"message": message,
|
||||
"priority": priority
|
||||
}
|
||||
|
||||
try:
|
||||
response = requests.post(url, headers=headers, json=payload, timeout=10)
|
||||
response.raise_for_status()
|
||||
return True
|
||||
except requests.RequestException as e:
|
||||
print(f"[!] Failed to send Gotify message: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def check_dir_changed():
|
||||
changed = False
|
||||
|
||||
dir_to_watch = os.getenv("DIR_TO_WATCH")
|
||||
|
||||
# create a cache db object and key for this dir (base64 of the dir)
|
||||
cache = get_cache(CACHE_DIR)
|
||||
cache_key = utils.TextUtils.encode_base64(dir_to_watch)
|
||||
|
||||
# get the old file listing from cache
|
||||
existing_contents = cache.read(cache_key)
|
||||
|
||||
try:
|
||||
# get current files
|
||||
current_files = utils.FileUtils.list_files_in_dir_w_subs(dir_to_watch)
|
||||
except Exception as e:
|
||||
logger.error(f"Unable to check for files due to: {e}")
|
||||
return False
|
||||
|
||||
# if they are different..
|
||||
if existing_contents != current_files:
|
||||
logger.info(f"The contents of {dir_to_watch} changed")
|
||||
changed = True
|
||||
|
||||
# update the cache
|
||||
if existing_contents is None:
|
||||
cache.create(cache_key,current_files)
|
||||
else:
|
||||
cache.update(cache_key,current_files)
|
||||
|
||||
return changed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
title = "NEW ACTIVITY DETECTED"
|
||||
msg = "Your Camera System has uploaded videos!. We have detected activity!"
|
||||
|
||||
files_changed = check_dir_changed()
|
||||
if files_changed:
|
||||
gotify(title,msg)
|
||||
Reference in New Issue
Block a user