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
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))