diff --git a/bink.py b/bink.py index acbdc7a..32b2cb4 100755 --- a/bink.py +++ b/bink.py @@ -14,16 +14,35 @@ try: except FileExistsError: pass -def post_object(path): +def write_object(obj): + with open(f"{home}/{time_ns()}", "w") as f: + json.dump(obj, f, indent=2) + +def file_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 create_forum_post(title, body): + thread = { + "body": body[:max_body_length], + "forum": title[:max_title_length] + } + write_object(thread) + +def create_blog_post(body): + blog = { + "body": body, + "forum": False + } + write_object(blog) + def generate_feed(): - posts = [post_object(path) for path in glob("/home/**/.bink/*")] + posts = [file_object(path) for path in glob("/home/**/.bink/*")] posts.sort(key=lambda x: x[0], reverse=True) - tree = OrderedDict() + forums = OrderedDict() + blogs = [] for time, user, path in posts: # try: with open(path, "r") as f: @@ -32,24 +51,22 @@ def generate_feed(): obj = { "user": user, "time": time, - "body": data["body"], - "title": data["title"] + "body": data["body"][:max_body_length], + "forum": data["forum"] } - 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) + if obj["forum"]: + obj["forum"] = obj["forum"][:max_title_length] + try: + forums[data["forum"]].insert(0, obj) + except KeyError: + forums[data["forum"]] = [obj] + else: + blogs.insert(0, obj) + return (blogs, forums) if "dump" in argv: with open("/dev/stdout", "w") as f: json.dump(generate_feed(), f) - exit() \ No newline at end of file + exit() + +import urwid \ No newline at end of file