Add support for matomo reports

This commit is contained in:
Peter J. Holzer 2023-02-15 14:44:29 +01:00
parent 10e29c3e39
commit 0976e4b5ef
1 changed files with 78 additions and 31 deletions

View File

@ -1,4 +1,5 @@
#!/home/hjp/venv/screen-sizes/bin/python #!/home/hjp/venv/screen-sizes/bin/python
import argparse
import json import json
import sys import sys
@ -6,46 +7,92 @@ import PIL.Image
import PIL.ImageDraw import PIL.ImageDraw
import PIL.ImageFont import PIL.ImageFont
sizes = [] ap = argparse.ArgumentParser()
with open(sys.argv[1]) as fh: ap.add_argument("--format", choices=("audit", "matomo"), required=True)
for ln in fh: ap.add_argument("file")
audit_record = json.loads(ln[ln.find("{"):]) args = ap.parse_args()
sizes.append(audit_record["data"]["device_info"]["viewport"])
widths = sorted(set(s["width"] for s in sizes)) if args.format == "audit":
heights = sorted(set(s["height"] for s in sizes)) sizes = []
width = widths[-1] with open(args.file) as fh:
height = heights[-1] for ln in fh:
print(width, height) audit_record = json.loads(ln[ln.find("{"):])
sizes.append(audit_record["data"]["device_info"]["viewport"])
dist = [ widths = sorted(set(s["width"] for s in sizes))
[0] * width heights = sorted(set(s["height"] for s in sizes))
for y in range(height) width = widths[-1]
] height = heights[-1]
print(width, height)
for i, s in enumerate(sizes): dist = [
w = s["width"] [0] * width
h = s["height"] for y in range(height)
print(i, w, h) ]
for y in range(h):
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): for x in range(w):
dist[y][x] += 1 dist_w[x] += 1
dist_w = [0] * width dist_h = [0] * height
for i, s in enumerate(sizes): for i, s in enumerate(sizes):
w = s["width"] h = s["height"]
for x in range(w): for y in range(h):
dist_w[x] += 1 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 origin_x = origin_y = 150
img = PIL.Image.new('L', (width+origin_x, height+origin_y)) 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 y in range(height):
for x in range(width): for x in range(width):
img.putpixel((x+origin_x, y+origin_y), int(dist[y][x] * 255 / max_val)) img.putpixel((x+origin_x, y+origin_y), int(dist[y][x] * 255 / max_val))