Commit 7ef2fa3e authored by Florent Viard's avatar Florent Viard Committed by Vincent A

tools/har-to-old: Replace the "prefix" argument with an optional "destdir"

The previous syntax for har-to-old was:

    usage: [-h] file prefix

Change it to:

    usage: [-h] file [destdir]

- prefix renamed in destdir, as it was not really a prefix but the
name(/path) to the destination folder for extracted files
- automatically create the destdir folder if it does not exist
- if destdir is not provided, use the file name without extension
parent f21512f2
......@@ -4,6 +4,7 @@ from argparse import ArgumentParser, FileType
from base64 import b64decode
import json
import mimetypes
import os
from pathlib import Path
from urllib.parse import urlparse
......@@ -58,12 +59,14 @@ def guess_extension(entry):
def main():
def extract(n, prefix):
def extract(n, destdir):
os.makedirs(destdir, exist_ok=True)
entry = data['log']['entries'][n]
ext = guess_extension(entry)
name = Path(urlparse(entry['request']['url']).path).stem
prefix = f'{prefix}/{n + 1:03d}-{entry["response"]["status"]}{name and f"-{name}"}{ext}'
prefix = f'{destdir}/{n + 1:03d}-{entry["response"]["status"]}{name and f"-{name}"}{ext}'
with open(f'{prefix}-request.txt', 'wb') as fd:
write_request(entry, fd)
......@@ -74,13 +77,20 @@ def main():
parser = ArgumentParser()
parser.add_argument('file', type=FileType('r'), help='HAR file to extract')
parser.add_argument('destdir', nargs='?', default=None, help='Destination directory for extracted files')
args = parser.parse_args()
if args.destdir is None:
# Automatically generate destdir if not provided
args.destdir =[:-4]
args.destdir = f'{}_content'
data = json.load(args.file)
for n in range(len(data['log']['entries'])):
print('extracting request', n)
extract(n, args.prefix)
extract(n, args.destdir)
if __name__ == '__main__':
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment