htdocs/automations/write_blog_pages.py

96 lines
3.6 KiB
Python

import blogs
import header
import root
from datetime import datetime
import os
file_extension = "html"
url = "https://deadvey.com"
blog_index_path = f"{root.path}/blog/"
# iterate over the files in the directory and remove them
'''
for filename in os.listdir(blog_index_path):
file_path = os.path.join(blog_index_path, filename)
if os.path.isfile(file_path): # make sure it's a file and not a directory
os.remove(file_path)
'''
tags = ["index"]
for i in range(0, len(blogs.blogs_array)):
for j in blogs.blogs_array[i][4]:
if j not in tags:
tags.append(j)
for tag in tags:
previous_year = 0 # Set the previous year as 0, the year is outputted to the page if it is different to 'previous_year'
file_name = tag.replace("/","per")
with open(f"{blog_index_path}/{file_name}.{file_extension}", "w") as blog_tag_file:
blog_tag_file.write(f"{header.header}")
if tag == "index":
blog_tag_file.write(f"<a href='/rss'>RSS Feed</a><br/>")
if tag != "index":
blog_tag_file.write(f"<h2>{tag}:</h2>")
for current_blog in range(len(blogs.blogs_array)-1, -1, -1): # eg 50 - 0
if tag in blogs.blogs_array[current_blog][4] or tag == "index":
# Hexadecimal time parsing/formatting
parsed_date = datetime.strptime(blogs.blogs_array[current_blog][2], '%a, %d %b %Y %H:%M:%S')
year = str(hex(int(parsed_date.strftime('%Y'))))[2:]
day = str(hex(int(parsed_date.timetuple().tm_yday)))[2:]
if previous_year != year:
blog_tag_file.write(f"<h3>{year}:</h3>")
# Write the link to the page
blog_tag_file.write(f"{day}: <a href='/blog/{current_blog}.{file_extension}'>{blogs.blogs_array[current_blog][0]}</a><br/>")
previous_year = year
# Also write all the blogs to their respective pages
for current_blog in range(len(blogs.blogs_array)-1, -1, -1): # eg 50 - 0
with open(f"{blog_index_path}/{current_blog}.{file_extension}","w") as current_blog_file:
parsed_date = datetime.strptime(blogs.blogs_array[current_blog][2], '%a, %d %b %Y %H:%M:%S')
year = str(hex(int(parsed_date.strftime('%Y'))))[2:]
day = str(hex(int(parsed_date.timetuple().tm_yday)))[2:]
hour = int(parsed_date.strftime('%H'))
minute = int(parsed_date.strftime('%M'))
second = int(parsed_date.strftime('%S'))
second_of_the_day = str(hex(int(((hour * 3600) + (minute * 60) + second) * (65536/86400))))[2:]
images_div = ""
tags_div = ""
buttons_div = ""
if current_blog > 0:
buttons_div += f"<a href='/blog/{current_blog-1}.html'><-- {blogs.blogs_array[current_blog-1][0]}</a> "
if current_blog < len(blogs.blogs_array) - 1:
buttons_div += f"<a href='/blog/{current_blog+1}.html'>{blogs.blogs_array[current_blog+1][0]}--></a>"
for i in blogs.blogs_array[current_blog][3]:
images_div += f'<img width="200" src="/images/{i}">\n'
for i in blogs.blogs_array[current_blog][4]:
dir_name = i.replace("/", "per") + "." + file_extension
tags_div += f'<a href="/blog/{dir_name}">{i}</a><br/>'
content = f'''
{header.header}
<b>{blogs.blogs_array[current_blog][0]}:</b>
{blogs.blogs_array[current_blog][1]}
<i>Published: {day}/{year} at {second_of_the_day}</i>
{buttons_div}
{tags_div}
{images_div}
</pre>
<style>
pre \u007b
white-space: pre-wrap;
\u007d
</style>
'''
current_blog_file.write(content)