marxbook/misc/mxb-search

54 lines
1.3 KiB
Python
Executable file

#!/usr/bin/env python3
from argparse import ArgumentParser
import os
from subprocess import Popen, PIPE
from logging import getLogger
FILTER_CMDLINE = ["peco"] # ["fzf"] is also fine
BROWSER_CMDLINE = ["firefox"]
logger = getLogger()
def get_urls(folder: str):
fmt = "{Folder}\t{Tag}\t{Title}\t{Url}"
mxb_list = Popen(
["mxb", "list", "--format", fmt, folder],
stdout=PIPE,
)
filter_cmd = Popen(FILTER_CMDLINE, stdin=mxb_list.stdout, stdout=PIPE)
logger.debug("running...")
output = filter_cmd.communicate()[0].decode("utf8")
logger.debug("got output!")
for line in output.split("\n"):
url = line.split("\t")[-1]
if url:
yield url
def get_parser():
p = ArgumentParser()
p.add_argument("--folder", default="")
p.add_argument("--action", choices=["open", "echo"], default="open")
return p
def main():
args = get_parser().parse_args()
urls = get_urls(args.folder)
logger.debug("URLs= %s", urls)
if not urls:
return
for u in urls:
if args.action == "open":
cmd = BROWSER_CMDLINE + [u]
Popen(cmd, preexec_fn=os.setpgrp)
elif args.action == "echo":
logger.debug(u)
if __name__ == "__main__":
main()