YnM-Ai
import requests
import json
from sopel.plugin import interval
# Weboldalak listája
websites = [
"https://ai.ynm.hu",
"https://chat.ynm.hu",
"https://forum.ynm.hu"
]
# Állapot fájl
status_file = "/home/ai/.sopel/website_status.json"
# Funkció a weboldalak elérhetőségének ellenőrzésére
def check_website(url):
try:
response = requests.get(url, timeout=5)
if response.status_code == 200:
return True
else:
return False
except requests.RequestException:
return False
# Állapot betöltése a fájlból
def load_status():
try:
with open(status_file, "r") as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
# Ha a fájl nem létezik, vagy hibás, akkor újraindítjuk az állapotokat
return {website: None for website in websites}
# Állapot mentése a fájlba
def save_status(status):
with open(status_file, "w") as f:
json.dump(status, f)
# A bot értesíteni fogja a csatornát, ha egy weboldal nem elérhető vagy újra online
@interval(600) # Minden 60 másodpercben lefut
def website_check(bot):
website_status = load_status() # Betöltjük az állapotot a fájlból
for website in websites:
is_online = check_website(website)
# Ha először vált offline-ra
if is_online and website_status[website] == False:
bot.say(f"{website} is back online.", "#YnM")
# Ha offline lett, és korábban online volt
elif not is_online and website_status[website] != False:
bot.say(f"Warning: {website} is down!", "#YnM")
# Frissítjük az oldal állapotát
website_status[website] = is_online
# Mentjük az állapotot a fájlba
save_status(website_status)