blog/monitor.py
Yax 13d3d653de Make internal helper functions private
Added underscore prefix to functions used only internally:
- _strip_tags_and_truncate (used only in parse_post_file)
- _parse_headers (used only in parse_post_file)
- _rfc_2822_format (used only in parse_post_file)
- _make_links_absolute (used only in parse_post_file)
- _strip_html_tags (used only in parse_post_file)
- _get_friendly_date (used only in _setup_page_params)
- _exit_program (used only in monitor.py main loop)

This improves encapsulation by clearly marking implementation details.
2026-01-07 13:42:43 +01:00

58 lines
1.4 KiB
Python
Executable file

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import os
import requests
import time
import json
def fread(filename):
"""Read file and close the file."""
with open(filename, "r") as f:
return f.read()
def get_comment_count():
"""Fetch the total number of comments from Stacosys API.
Returns:
int: Total comment count, or 0 if request fails
"""
req_url = params["stacosys_url"] + "/comments/count"
query_params = dict(
token=params["stacosys_token"]
)
resp = requests.get(url=req_url, params=query_params)
return 0 if not resp.ok else int(resp.json()["count"])
def _exit_program():
"""Exit the program with status code 0."""
sys.exit(0)
# Default parameters.
params = {
"stacosys_token": "",
"stacosys_url": "",
"external_check": "",
}
# If params.json exists, load it.
if os.path.isfile("params.json"):
params.update(json.loads(fread("params.json")))
external_check_cmd = params["external_check"]
initial_count = get_comment_count()
print(f"Comments = {initial_count}")
while True:
# check number of comments every 60 seconds
for _ in range(15):
time.sleep(60)
if initial_count != get_comment_count():
_exit_program()
# check if git repo changed every 15 minutes
if external_check_cmd and os.system(external_check_cmd):
_exit_program()