From faa6935be85399125bef37063645b7444ac86a31 Mon Sep 17 00:00:00 2001 From: Ari Archer Date: Tue, 4 Apr 2023 23:52:46 +0300 Subject: [PATCH] improve recents api Signed-off-by: Ari Archer --- blog.json | 3 ++- scripts/blog.py | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/blog.json b/blog.json index c3d4c5b..74d9a95 100644 --- a/blog.json +++ b/blog.json @@ -58,6 +58,7 @@ "locale": "en_GB", "home-page-header": "my blogs", "comment-url": "\/c", + "recents": 16, "blogs": { "new-blog-management-system-": { "title": "New blog management system!", @@ -535,7 +536,7 @@ }, "ari-web-blog-api-change": { "title": "Ari-web blog API change", - "content": "hello world\n\n[the blog api](\/blog.json) has changed, now i dont use base64 to encode, it was useless, i noticed\nnobody uses the api basically, but i want it to be available, so just notifying yall\nof the change, the reason i just changed it with no prior warning is because\nnobody uses it lol\n\nalso, a new api created -- [recents.json](\/recents.json) which has the most recent 5 blog posts as the\n`blog.json` api is extremely large lol, it also has [recents_json_hash.txt](\/recents_json_hash.txt)\n\nanyway, have fun i guess :)\n", + "content": "hello world\n\n[the blog api](\/blog.json) has changed, now i dont use base64 to encode, it was useless, i noticed\nnobody uses the api basically, but i want it to be available, so just notifying yall\nof the change, the reason i just changed it with no prior warning is because\nnobody uses it lol\n\nalso, a new api created -- [recents.json](\/recents.json) which has the most recent 5 blog posts as the\n`blog.json` api is extremely large lol, it also has [recents_json_hash.txt](\/recents_json_hash.txt)\n\nkeep in mind `recents.json` only has summaries of blog posts, not full content, time\n( UNIX timestamp ) and the title, thats it\n\nanyway, have fun i guess :)\n", "time": 1680636542.575466, "keywords": "ari-web blog api change" } diff --git a/scripts/blog.py b/scripts/blog.py index 0077dec..460d682 100755 --- a/scripts/blog.py +++ b/scripts/blog.py @@ -78,6 +78,7 @@ DEFAULT_CONFIG: Dict[str, Any] = { "locale": "en_GB", "home-page-header": "my blogs", "comment-url": "/c", + "recents": 16, "blogs": {}, } DEFAULT_CONFIG_FILE: str = "blog.json" @@ -880,7 +881,19 @@ def generate_metadata(config: Dict[str, Any]) -> Tuple[int, Dict[str, Any]]: with open("recents.json", "w") as blog_recents: log(f"generating {blog_recents.name!r}", "GENERATE") - ujson.dump(dict(tuple(config["blogs"].items())[-5:]), blog_recents) + + recents: Dict[str, Any] = {} + + for rid, recent in tuple(config["blogs"].items())[-(config["recents"]) :]: + r: Dict[str, Any] = recent.copy() + content: List[str] = r["content"].strip()[:196][::-1].split(maxsplit=1) + + r["content"] = content[len(content) > 1][::-1] + del r["keywords"] + + recents[rid] = r + + ujson.dump(recents, blog_recents) for hashable in (DEFAULT_CONFIG_FILE, blog_recents.name): with open(hashable, "rb") as api_file: