nostril

A C cli tool for creating nostr events
git clone git://jb55.com/nostril
Log | Files | Refs | Submodules | README | LICENSE

nostril-query (1844B)


      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('-a', '--authors')
     10 parser.add_argument('-p', '--mentions')
     11 parser.add_argument('-e', '--references')
     12 parser.add_argument('-d', '--parameter')
     13 parser.add_argument('-t', '--hashtag')
     14 parser.add_argument('-i', '--ids')
     15 parser.add_argument('-k', '--kinds')
     16 parser.add_argument('-s', '--search')
     17 parser.add_argument('-g', 
     18                     '--generic',
     19                     nargs=2,
     20                     metavar=('tag', 'value'),
     21                     help="Generic tag query: `#<tag>: value`")
     22 parser.add_argument('-l', '--limit', type=int)
     23 parser.add_argument('-s', '--since', type=int)
     24 
     25 def usage():
     26     parser.print_help()
     27     sys.exit(1)
     28 
     29 args = parser.parse_args()
     30 
     31 filt = {}
     32 
     33 if args.authors:
     34     filt["authors"] = args.authors.split(",")
     35 
     36 if args.ids:
     37     filt["ids"] = args.ids.split(",")
     38 
     39 if args.limit is not None:
     40     filt["limit"] = args.limit
     41 
     42 if args.generic:
     43     (tag, val) = args.generic
     44     filt["#" + tag] = val.split(",")
     45 
     46 if args.search:
     47     filt["search"] = args.search
     48 
     49 if args.hashtag is not None:
     50     filt["#t"] = args.hashtag.split(",")
     51 
     52 if args.mentions is not None:
     53     filt["#p"] = args.mentions.split(",")
     54 
     55 if args.references is not None:
     56     filt["#e"] = args.references.split(",")
     57 
     58 if args.parameter is not None:
     59     filt["#d"] = args.parameter.split(",")
     60 
     61 if args.kinds is not None:
     62     kinds = args.kinds.split(",")
     63     filt["kinds"] = [a for a in map(lambda s: int(s), kinds)]
     64 
     65 if args.since is not None:
     66     filt["since"] = args.since
     67 
     68 q = ""
     69 if args.raw is not None:
     70     q = json.dumps(filt)
     71 else:
     72     q = json.dumps(["REQ","nostril-query",filt])
     73 
     74 print(q)