htdocs/automations/write_blog_pages.py
2025-03-04 17:45:31 +00:00

88 lines
3.5 KiB
Python

import blogs
import header
import root
from datetime import datetime
import os
file_extension = "gmi"
url = "gemini://deadvey.com"
blog_index_path = f"{root.path}/blog/"
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"## All:\n")
if tag != "index":
blog_tag_file.write(f"## {tag}:\n")
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"### {year}:\n")
# Write the link to the page
blog_tag_file.write(f"=> /blog/{current_blog}.{file_extension} {day}: {blogs.blogs_array[current_blog][0]}\n")
previous_year = year
blog_tag_file.write("")
# 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 = ""
blogs.blogs_array[current_blog][1] = blogs.blogs_array[current_blog][1].replace(". ", ".\n")
blogs.blogs_array[current_blog][1] = blogs.blogs_array[current_blog][1].replace("! ", "!\n")
blogs.blogs_array[current_blog][1] = blogs.blogs_array[current_blog][1].replace("? ", "?\n")
if current_blog > 0:
buttons_div += f"=> /blog/{current_blog-1}.{file_extension} <-- {blogs.blogs_array[current_blog-1][0]}\n"
if current_blog < len(blogs.blogs_array) - 1:
buttons_div += f"=> /blog/{current_blog+1}.{file_extension} {blogs.blogs_array[current_blog+1][0]}-->\n"
for i in blogs.blogs_array[current_blog][3]:
images_div += f'=> /images/{i} {i}\n'
for i in blogs.blogs_array[current_blog][4]:
dir_name = i.replace("/", "per") + "." + file_extension
tags_div += f'=> /blog/{dir_name} {i}\n'
content = f'''
{header.header}
{buttons_div}
## {blogs.blogs_array[current_blog][0]}:
{blogs.blogs_array[current_blog][1]}
### Published: {day}/{year} at {second_of_the_day}
{tags_div}
{images_div}
'''
current_blog_file.write(content)