cygnus/update_fortune.py

27 lines
768 B
Python

from bs4 import BeautifulSoup
import os
file_name = "/var/www/html/index.html"
#if the file doesn't exist, we should copy the template to the folder
if not os.exists(file_name):
template_name = "index.html"
shutil.copy(template_name, file_name)
print("The index file was not available, so we've restored it from template.")
fortune = os.popen("/usr/games/fortune | /usr/games/cowsay").read()
soup = None
print("The emergency fortune system has set the fortune to {}".format(fortune))
with open(file_name, "r") as the_file:
soup = BeautifulSoup(the_file, features="html.parser")
fortune_holder = soup.find(id="fortune-container")
fortune_holder.string = fortune
with open(file_name, "w") as the_file:
the_file.write(str(soup))