Created
February 10, 2022 05:05
-
-
Save takemikami/1f49810465cbd2315c12cab1c7ab0da7 to your computer and use it in GitHub Desktop.
translate har to mermaid
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
import json | |
import urllib.parse | |
import sys | |
import re | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
print("usage: python3 har2mermaid.py harfile") | |
exit(1) | |
har_file = sys.argv[1] | |
if har_file.endswith("\.har"): | |
print("input file extension must be har.") | |
exit(1) | |
md_file = re.sub(".har$", ".md", har_file) | |
with open(har_file, 'r') as f: | |
harraw = f.read() | |
harobj = json.loads(harraw) | |
lines = [ | |
"```mermaid", | |
"sequenceDiagram", | |
"participant visitor as visitor", | |
] | |
for h in set([urllib.parse.urlparse(r['request']['url']).netloc for r in harobj['log']['entries']]): | |
lines.append("participant {} as {}".format(h.replace(".", "_"), h)) | |
for r in harobj['log']['entries']: | |
hosturl = urllib.parse.urlparse(r['request']['url']) | |
host_str = hosturl.netloc.replace(".", "_") | |
request_str = hosturl.path[:50] | |
response_str = "{} - {}".format(r['response']['status'], r['response']['content']['mimeType']) | |
lines.append(" visitor ->>+ {}: {}".format(host_str, request_str)) | |
lines.append(" {} -->>- visitor: {}".format(host_str, response_str)) | |
lines.append("```") | |
with open(md_file, 'w') as w: | |
w.write('\n'.join(lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment