56 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
# {"title": "this is a test", "body": "this is text"}, 
 | 
						|
 | 
						|
import json
 | 
						|
from glob import glob
 | 
						|
from time import time_ns
 | 
						|
import os
 | 
						|
from collections import OrderedDict
 | 
						|
 | 
						|
home = os.path.expanduser("~/.bink")
 | 
						|
my_user = home.split("/")[2]
 | 
						|
 | 
						|
max_title_length = 120
 | 
						|
max_body_length = 64_000
 | 
						|
 | 
						|
try:
 | 
						|
    os.mkdir(home)
 | 
						|
except FileExistsError:
 | 
						|
    pass
 | 
						|
 | 
						|
def post_object(path):
 | 
						|
    split = path.split("/")
 | 
						|
    # example input: /home/nebula/.bink/999
 | 
						|
    # output: (999, "nebula", "/home/nebula/.bink/999")
 | 
						|
    return (int(split[-1]), split[2], path)
 | 
						|
 | 
						|
def generate_feed():
 | 
						|
    posts = [post_object(path) for path in glob("/home/**/.bink/*")]
 | 
						|
    posts.sort(key=lambda x: x[0], reverse=True)
 | 
						|
    tree = OrderedDict()
 | 
						|
    for time, user, path in posts:
 | 
						|
        # try:
 | 
						|
        with open(path, "r") as f:
 | 
						|
            data = json.load(f)
 | 
						|
        # except
 | 
						|
        obj = {
 | 
						|
            "user": user,
 | 
						|
            "time": time,
 | 
						|
            "body": data["body"],
 | 
						|
            "title": data["title"]
 | 
						|
        }
 | 
						|
        try:
 | 
						|
            tree[data["title"]].insert(0, obj)
 | 
						|
        except KeyError:
 | 
						|
            tree[data["title"]] = [obj]
 | 
						|
    return tree
 | 
						|
 | 
						|
def create_post(title, body):
 | 
						|
    thread = {
 | 
						|
        "body": body[:max_body_length],
 | 
						|
        "title": title[:max_title_length]
 | 
						|
    }
 | 
						|
    with open(f"{home}/{time_ns()}", "w") as f:
 | 
						|
        json.dump(thread, f, indent=2)
 | 
						|
 | 
						|
print(generate_feed())
 |