add microblogging data structures
This commit is contained in:
parent
ee2d2ac3ed
commit
9d63d7d886
55
bink.py
55
bink.py
@ -14,16 +14,35 @@ try:
|
|||||||
except FileExistsError:
|
except FileExistsError:
|
||||||
pass
|
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("/")
|
split = path.split("/")
|
||||||
# example input: /home/nebula/.bink/999
|
# example input: /home/nebula/.bink/999
|
||||||
# output: (999, "nebula", "/home/nebula/.bink/999")
|
# output: (999, "nebula", "/home/nebula/.bink/999")
|
||||||
return (int(split[-1]), split[2], path)
|
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():
|
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)
|
posts.sort(key=lambda x: x[0], reverse=True)
|
||||||
tree = OrderedDict()
|
forums = OrderedDict()
|
||||||
|
blogs = []
|
||||||
for time, user, path in posts:
|
for time, user, path in posts:
|
||||||
# try:
|
# try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
@ -32,24 +51,22 @@ def generate_feed():
|
|||||||
obj = {
|
obj = {
|
||||||
"user": user,
|
"user": user,
|
||||||
"time": time,
|
"time": time,
|
||||||
"body": data["body"],
|
"body": data["body"][:max_body_length],
|
||||||
"title": data["title"]
|
"forum": data["forum"]
|
||||||
}
|
}
|
||||||
try:
|
if obj["forum"]:
|
||||||
tree[data["title"]].insert(0, obj)
|
obj["forum"] = obj["forum"][:max_title_length]
|
||||||
except KeyError:
|
try:
|
||||||
tree[data["title"]] = [obj]
|
forums[data["forum"]].insert(0, obj)
|
||||||
return tree
|
except KeyError:
|
||||||
|
forums[data["forum"]] = [obj]
|
||||||
def create_post(title, body):
|
else:
|
||||||
thread = {
|
blogs.insert(0, obj)
|
||||||
"body": body[:max_body_length],
|
return (blogs, forums)
|
||||||
"title": title[:max_title_length]
|
|
||||||
}
|
|
||||||
with open(f"{home}/{time_ns()}", "w") as f:
|
|
||||||
json.dump(thread, f, indent=2)
|
|
||||||
|
|
||||||
if "dump" in argv:
|
if "dump" in argv:
|
||||||
with open("/dev/stdout", "w") as f:
|
with open("/dev/stdout", "w") as f:
|
||||||
json.dump(generate_feed(), f)
|
json.dump(generate_feed(), f)
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
import urwid
|
Loading…
x
Reference in New Issue
Block a user