add support for json dumping before/after supplied timestamp
This commit is contained in:
parent
070ed21dde
commit
ac27668621
37
bink.py
37
bink.py
@ -18,7 +18,9 @@ max_body_length = 64_000
|
|||||||
helptext = """see https://git.tilde.town/nebula/bink for details
|
helptext = """see https://git.tilde.town/nebula/bink for details
|
||||||
--help or -h: show this message
|
--help or -h: show this message
|
||||||
--pipe or -p: use stdin as post content. ex `echo "hello!" | town bink --pipe`
|
--pipe or -p: use stdin as post content. ex `echo "hello!" | town bink --pipe`
|
||||||
--dump or -d: print all posts in a json object"""
|
--dump or -d: print all posts in a json object
|
||||||
|
---dump-before or -db: print all posts before timestamp supplied as an additional argument
|
||||||
|
---dump-after or -da: print all posts after timestamp supplied as an additional argument"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.mkdir(home)
|
os.mkdir(home)
|
||||||
@ -60,7 +62,7 @@ def glob_posts(path):
|
|||||||
if os.path.isfile(post_path) and not os.path.islink(post_path)
|
if os.path.isfile(post_path) and not os.path.islink(post_path)
|
||||||
]
|
]
|
||||||
|
|
||||||
def generate_feed(before=None, count=200):
|
def generate_feed(before=None, after=None, count=200):
|
||||||
posts = glob_posts("/home/**/.bink/*")
|
posts = glob_posts("/home/**/.bink/*")
|
||||||
if our_path:
|
if our_path:
|
||||||
posts += glob_posts(f"{our_path}/*")
|
posts += glob_posts(f"{our_path}/*")
|
||||||
@ -69,6 +71,8 @@ def generate_feed(before=None, count=200):
|
|||||||
posts.remove(post)
|
posts.remove(post)
|
||||||
if before:
|
if before:
|
||||||
posts = [post for post in posts if post[0] < before]
|
posts = [post for post in posts if post[0] < before]
|
||||||
|
elif after:
|
||||||
|
posts = [post for post in posts if post[0] > after]
|
||||||
posts.sort(key=lambda x: x[0], reverse=True)
|
posts.sort(key=lambda x: x[0], reverse=True)
|
||||||
blogs = []
|
blogs = []
|
||||||
for time, user, path in posts[:count]:
|
for time, user, path in posts[:count]:
|
||||||
@ -86,14 +90,37 @@ def generate_feed(before=None, count=200):
|
|||||||
return blogs
|
return blogs
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
if sys.argv[1] in ("--help", "-h"):
|
argv1 = sys.argv[1]
|
||||||
|
if argv1 in ("--help", "-h"):
|
||||||
print(helptext)
|
print(helptext)
|
||||||
exit(0)
|
exit(0)
|
||||||
elif sys.argv[1] in ("--dump", "-d"):
|
elif argv1 in ("--dump", "-d"):
|
||||||
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(0)
|
exit(0)
|
||||||
elif sys.argv[1] in ("--pipe", "-p"):
|
elif argv1 in ("--dump-before", "-db"):
|
||||||
|
try:
|
||||||
|
with open("/dev/stdout", "w") as f:
|
||||||
|
json.dump(generate_feed(before=int(sys.argv[2])), f)
|
||||||
|
exit(0)
|
||||||
|
except ValueError:
|
||||||
|
print(f"Argument for {argv1} must be a number of epoch-nanoseconds.")
|
||||||
|
exit(1)
|
||||||
|
except IndexError:
|
||||||
|
print(f"Supply a timestamp to use this method.")
|
||||||
|
exit(1)
|
||||||
|
elif argv1 in ("--dump-after", "-da"):
|
||||||
|
try:
|
||||||
|
with open("/dev/stdout", "w") as f:
|
||||||
|
json.dump(generate_feed(after=int(sys.argv[2])), f)
|
||||||
|
exit(0)
|
||||||
|
except ValueError:
|
||||||
|
print(f"Argument for {argv1} must be a number of epoch-nanoseconds.")
|
||||||
|
exit(1)
|
||||||
|
except IndexError:
|
||||||
|
print(f"Supply a timestamp to use this method.")
|
||||||
|
exit(1)
|
||||||
|
elif argv1 in ("--pipe", "-p"):
|
||||||
try:
|
try:
|
||||||
with open("/dev/stdin", "r", encoding="UTF-8") as f:
|
with open("/dev/stdin", "r", encoding="UTF-8") as f:
|
||||||
body = f.read().strip()
|
body = f.read().strip()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user