From 0976e4b5ef7b6fc4fea6bf396cb23c9a422933d1 Mon Sep 17 00:00:00 2001 From: "Peter J. Holzer" Date: Wed, 15 Feb 2023 14:44:29 +0100 Subject: [PATCH] Add support for matomo reports --- screen-sizes | 109 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 78 insertions(+), 31 deletions(-) diff --git a/screen-sizes b/screen-sizes index bd3fea8..197e092 100755 --- a/screen-sizes +++ b/screen-sizes @@ -1,4 +1,5 @@ #!/home/hjp/venv/screen-sizes/bin/python +import argparse import json import sys @@ -6,46 +7,92 @@ import PIL.Image import PIL.ImageDraw import PIL.ImageFont -sizes = [] -with open(sys.argv[1]) as fh: - for ln in fh: - audit_record = json.loads(ln[ln.find("{"):]) - sizes.append(audit_record["data"]["device_info"]["viewport"]) +ap = argparse.ArgumentParser() +ap.add_argument("--format", choices=("audit", "matomo"), required=True) +ap.add_argument("file") +args = ap.parse_args() -widths = sorted(set(s["width"] for s in sizes)) -heights = sorted(set(s["height"] for s in sizes)) -width = widths[-1] -height = heights[-1] -print(width, height) +if args.format == "audit": + sizes = [] + with open(args.file) as fh: + for ln in fh: + audit_record = json.loads(ln[ln.find("{"):]) + sizes.append(audit_record["data"]["device_info"]["viewport"]) -dist = [ - [0] * width - for y in range(height) -] + widths = sorted(set(s["width"] for s in sizes)) + heights = sorted(set(s["height"] for s in sizes)) + width = widths[-1] + height = heights[-1] + print(width, height) -for i, s in enumerate(sizes): - w = s["width"] - h = s["height"] - print(i, w, h) - for y in range(h): + dist = [ + [0] * width + for y in range(height) + ] + + for i, s in enumerate(sizes): + w = s["width"] + h = s["height"] + print(i, w, h) + for y in range(h): + for x in range(w): + dist[y][x] += 1 + + dist_w = [0] * width + for i, s in enumerate(sizes): + w = s["width"] for x in range(w): - dist[y][x] += 1 + dist_w[x] += 1 -dist_w = [0] * width -for i, s in enumerate(sizes): - w = s["width"] - for x in range(w): - dist_w[x] += 1 + dist_h = [0] * height + for i, s in enumerate(sizes): + h = s["height"] + for y in range(h): + dist_h[y] += 1 +elif args.format == "matomo": + sizes = [] + with open(args.file) as fh: + data = json.load(fh) + for dp in data: + w, _, h = dp["label"].partition("x") + w = int(w) + h = int(h) + sizes.append({"width": w, "height": h, "count": dp["nb_visits"]}) + widths = sorted(set(s["width"] for s in sizes)) + heights = sorted(set(s["height"] for s in sizes)) + width = widths[-1] + height = heights[-1] + print(width, height) + + dist = [ + [0] * width + for y in range(height) + ] + + for i, s in enumerate(sizes): + w = s["width"] + h = s["height"] + print(i, w, h) + for y in range(h): + for x in range(w): + dist[y][x] += s["count"] + + dist_w = [0] * width + for i, s in enumerate(sizes): + w = s["width"] + for x in range(w): + dist_w[x] += s["count"] + + dist_h = [0] * height + for i, s in enumerate(sizes): + h = s["height"] + for y in range(h): + dist_h[y] += s["count"] -dist_h = [0] * height -for i, s in enumerate(sizes): - h = s["height"] - for y in range(h): - dist_h[y] += 1 origin_x = origin_y = 150 img = PIL.Image.new('L', (width+origin_x, height+origin_y)) -max_val = len(sizes) +max_val = dist[0][0] for y in range(height): for x in range(width): img.putpixel((x+origin_x, y+origin_y), int(dist[y][x] * 255 / max_val))