Skip to main content

Tidy up the README and the JS script

ID
b925c56
date
2024-06-19 21:57:42+00:00
author
Alex Chan <alex@alexwlchan.net>
parent
37532a9
message
Tidy up the README and the JS script
changed files
3 files, 14 additions, 50 deletions

Changed files

README.md (1835) → README.md (1876)

diff --git a/README.md b/README.md
index b4adfca..e9fed71 100644
--- a/README.md
+++ b/README.md
@@ -36,17 +36,21 @@ I use this script to analyse the props, to identity ways we can reduce the size 
 
 ## Usage
 
-You need Python 3, then download the script from this repo.
+You need Node installed.
+Download the `measure.js` from this repo.
 
-The script takes two arguments: a URL, and a name to identify the results.
+The script takes two arguments:
+
+* the URL to fetch (required)
+* a label for the downloaded files (optional)
 
 Example:
 
 ```console
-$ python3 measure.py https://wellcomecollection.org/collections collections
-html      = 210.22 kB
-next_data =  35.71 kB (16%)
+$ node measure.js https://wellcomecollection.org/collections collections
+HTML       =  196.45 kB
+NEXT_DATA =   52.17 kB (26.6%)
 
-Saved HTML to _out/collections.html
-Saved JSON to _out/collections.json
+Saved HTML to out/collections.html
+Saved JSON to out/collections.json
 ```

measure.js (2922) → measure.js (2906)

diff --git a/measure.js b/measure.js
index df6abcb..f2ceafd 100644
--- a/measure.js
+++ b/measure.js
@@ -88,14 +88,14 @@ https.get(url, options, (res) => {
   // We've got the whole HTML file.  Parse it, and save the results.
   res.on('end', () => {
     let htmlByteCount = Buffer.byteLength(html, 'utf8');
-    console.log(`HTML          = ${leftPad(naturalSize(htmlByteCount), 10)}`);
+    console.log(`HTML       = ${leftPad(naturalSize(htmlByteCount), 10)}`);
 
     let nextData = html
         .split('<script id="__NEXT_DATA__" type="application/json">')[1]
         .split("</script>")[0];
 
     let nextDataByteCount = Buffer.byteLength(nextData, 'utf8');
-    console.log(`__NEXT_DATA__ = ${leftPad(naturalSize(nextDataByteCount), 10)} (${(nextDataByteCount / htmlByteCount * 100).toFixed(1)}%)`);
+    console.log(`NEXT_DATA = ${leftPad(naturalSize(nextDataByteCount), 10)} (${(nextDataByteCount / htmlByteCount * 100).toFixed(1)}%)`);
 
     console.log();
 
@@ -103,7 +103,7 @@ https.get(url, options, (res) => {
     console.log(`Saved HTML to out/${label}.html`);
 
     writeToFile({ filename: `${label}.json`, contents: nextData });
-    console.log(`Saved __NEXT_DATA__ to out/${label}.json`);
+    console.log(`Saved JSON to out/${label}.json`);
   });
 
 }).on('error', (err) => {

measure.py (1035) → measure.py (0)

diff --git a/measure.py b/measure.py
deleted file mode 100755
index 9e8559d..0000000
--- a/measure.py
+++ /dev/null
@@ -1,40 +0,0 @@
-#!/usr/bin/env python3
-
-import json
-import os
-import sys
-import urllib.request
-
-
-def naturalsize(bytes):
-    return "%3.2f kB" % (bytes / 1024)
-
-
-def analyse(*, url, name):
-    os.makedirs("_out", exist_ok=True)
-    urllib.request.urlretrieve(url, f"_out/{name}.html")
-
-    html = open(f"_out/{name}.html").read()
-
-    next_data = html.split('<script id="__NEXT_DATA__" type="application/json">')[
-        1
-    ].split("</script>")[0]
-
-    with open(f'_out/{name}.json', 'w') as outfile:
-        outfile.write(json.dumps(json.loads(next_data), indent=2, sort_keys=True))
-
-    print(f"html      = {naturalsize(len(html))}")
-    print(f"next_data = {naturalsize(len(next_data)).rjust(9)} ({int(len(next_data) / len(html) * 100)}%)")
-    print("")
-    print(f"Saved HTML to _out/{name}.html")
-    print(f"Saved JSON to _out/{name}.json")
-
-
-if __name__ == "__main__":
-    try:
-        url = sys.argv[1]
-        name = sys.argv[2]
-    except IndexError:
-        sys.exit(f"Usage: {__file__} <URL> <NAME>")
-
-    analyse(url=url, name=name)