Created
March 27, 2024 20:58
-
-
Save DanaEpp/8c32c7adc8a5cae9e07129b414d9bef1 to your computer and use it in GitHub Desktop.
A simple Python script that will convert and encode a Big List of Naughty Strings (BLNS) into a JSON file that Postman can use
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from argparse import ArgumentParser, Namespace | |
import os | |
import base64 | |
import json | |
def main(srcFile: str, dstFile:str) -> None: | |
if not os.path.isfile(srcFile): | |
print( '-s argument is invalid. Is it a proper BLNS txt file? Aborting!' ) | |
return | |
with open(srcFile, 'r') as f: | |
# put all lines in the file into a Python list | |
content = f.readlines() | |
# above line leaves trailing newline characters; strip them out | |
content = [x.strip('\n') for x in content] | |
# remove empty-lines and comments | |
content = [x for x in content if x and not x.startswith('#')] | |
# Base64 encode all content to make Postman Collection Runner parser to not break | |
content = [base64.b64encode(x.encode('utf-8')).decode('utf-8') for x in content] | |
# insert empty string since all are being removed | |
content.insert(0, "") | |
encodedContent: list = [] | |
for c in content: | |
encodedContent.append({"encodedNaughtyString": c}) | |
with open(dstFile, 'w') as f: | |
# write JSON to file; note the ensure_ascii parameter | |
json.dump(encodedContent, f, indent=2, ensure_ascii=False) | |
if __name__ == '__main__': | |
parser = ArgumentParser() | |
parser.add_argument( '-s', '--src', help='The source BLNS txt file to convert', type=str, required=True) | |
parser.add_argument( '-d', '--dst', help='The destination filename of the encoded BLNS CSV', type=str, required=True) | |
args: Namespace = parser.parse_args() | |
main(args.src, args.dst) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment