nostril-query (2131B)
1 #!/usr/bin/env python3 2 3 import sys 4 import argparse 5 import json 6 7 parser = argparse.ArgumentParser(prog = 'nostril-query', description = 'Construct nostr queries') 8 parser.add_argument('-r', '--raw', action=argparse.BooleanOptionalAction) 9 parser.add_argument('-R', '--raw-envelope', action=argparse.BooleanOptionalAction) 10 parser.add_argument('-a', '--authors') 11 parser.add_argument('-p', '--mentions') 12 parser.add_argument('-e', '--references') 13 parser.add_argument('-d', '--parameter') 14 parser.add_argument('-t', '--hashtag') 15 parser.add_argument('-i', '--ids') 16 parser.add_argument('-k', '--kinds') 17 parser.add_argument('-S', '--search') 18 parser.add_argument('-g', 19 '--generic', 20 nargs=2, 21 metavar=('tag', 'value'), 22 help="Generic tag query: `#<tag>: value`") 23 parser.add_argument('-l', '--limit', type=int) 24 parser.add_argument('-s', '--since', type=int) 25 parser.add_argument('-u', '--until', type=int) 26 27 def usage(): 28 parser.print_help() 29 sys.exit(1) 30 31 args = parser.parse_args() 32 33 filt = {} 34 35 if args.authors: 36 filt["authors"] = args.authors.split(",") 37 38 if args.ids: 39 filt["ids"] = args.ids.split(",") 40 41 if args.limit is not None: 42 filt["limit"] = args.limit 43 44 if args.generic: 45 (tag, val) = args.generic 46 filt["#" + tag] = val.split(",") 47 48 if args.search: 49 filt["search"] = args.search 50 51 if args.since: 52 filt["since"] = args.since 53 54 if args.until: 55 filt["until"] = args.until 56 57 if args.hashtag is not None: 58 filt["#t"] = args.hashtag.split(",") 59 60 if args.mentions is not None: 61 filt["#p"] = args.mentions.split(",") 62 63 if args.references is not None: 64 filt["#e"] = args.references.split(",") 65 66 if args.parameter is not None: 67 filt["#d"] = args.parameter.split(",") 68 69 if args.kinds is not None: 70 kinds = args.kinds.split(",") 71 filt["kinds"] = [a for a in map(lambda s: int(s), kinds)] 72 73 if args.since is not None: 74 filt["since"] = args.since 75 76 q = "" 77 if args.raw is not None: 78 q = json.dumps(filt) 79 elif args.raw_envelope is not None: 80 q = json.dumps([filt]) 81 else: 82 q = json.dumps(["REQ","nostril-query",filt]) 83 84 print(q)