Browse Source

Improve argument parsing with argparse

tags/v0.2
Julian 6 months ago
parent
commit
422dff7218
Signed by: j.r GPG Key ID: E74A18B933F9145E
2 changed files with 13 additions and 16 deletions
  1. +1
    -1
      README.md
  2. +12
    -15
      main.py

+ 1
- 1
README.md View File

@@ -24,7 +24,7 @@ To run it you need a list of Jitsi Meet instances in a file in tis format:
```
Then you can run the script with:
```
python3 main.py /path/to/instances.txt > instances.md
./main.py /path/to/instances.txt > instances.md
```

# Contributing


+ 12
- 15
main.py View File

@@ -6,6 +6,7 @@ import sys
import socket
import urllib.request
import json
import argparse

domains = []
bad_stuns = {
@@ -90,8 +91,7 @@ def checkHoster(domain):
return False, data['data']['geo']['isp']
return False, "n.A."

def parseText(path):
f = open(path, "r")
def parseText(f):
char = f.read(1)
while char:
if char == "\"":
@@ -141,23 +141,20 @@ def outputJSON(outputs, unavailable):
sys.stdout.write(json.dumps({"data": outputList}));

def parseFormat(format):
if format == "json":
return "json"
if format == "json" or format == "md":
return format
else:
return "md"
msg = "%r is not valid, valid are md and json" % format
raise argparse.ArgumentTypeError(msg)

def main():
format = "md"

# Get domain list and format
if (len(sys.argv) <= 1):
sys.stderr.write("Please provide list with '" + sys.argv[0] + " /path/to/list.txt'\nOptional second argument: format [md or json]\n")
sys.exit(1)
else:
parseText(sys.argv[1])
if (len(sys.argv) >= 3):
format = parseFormat(sys.argv[2])
parser = argparse.ArgumentParser()
parser.add_argument("list", type=argparse.FileType("r"), help="File with instances, each in one line with the following format: \"meet.example.com\",")
parser.add_argument("-f", "--format", help="Output format, can either be md or json", default="md", type=parseFormat)
args = parser.parse_args()

parseText(args.list)
format = args.format
sys.stderr.write(f"Output format: {format}\n")

#Check all domains


Loading…
Cancel
Save