From 0217bf46110db2b4ee7b85da2514e7d26b88ee71 Mon Sep 17 00:00:00 2001 From: Johannes Date: Mon, 2 Oct 2023 19:54:32 +0200 Subject: [PATCH] First version of parser and basic stats --- main.py | 16 ++++++++++++++++ parse.py | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 main.py create mode 100644 parse.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..4d9c77e --- /dev/null +++ b/main.py @@ -0,0 +1,16 @@ +#! /usr/bin/env python3 +from datetime import datetime +from pathlib import Path + +from parse import parse, wiki_stats + + +def print_wiki_stats(filename: str) -> None: + path = Path(filename) + timestamp = datetime.fromtimestamp(path.stat().st_ctime) + print(f"Stand: {timestamp.strftime('%d.%m.%Y %H:%m Uhr')}") + print(wiki_stats(*parse(path))) + + +if __name__ == "__main__": + print_wiki_stats("koma89_pretixdata.json") diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..867cc6d --- /dev/null +++ b/parse.py @@ -0,0 +1,58 @@ +import json +from pathlib import Path +from typing import Dict, Tuple, Union # I have just python3.8 here :( + + +def parse(filename: Union[str, Path]) -> Tuple[ + Dict[int, str], + Dict[int, int], + Dict[int, str], + Dict[int, Dict[Tuple[int, str], int]] +]: + """Parse a JSON export from pretix. + + :param filename: filename of the JSON export to parse. + :returns: Four mappings: + * item_id to item name + * item_id to order count of this item + * question_id to question identifier + * question_id to stats for this question, which identifies an answer by its item_id and actual answer. + The stats map this identifier to number of occurrences + """ + with open(filename, 'r', encoding='utf8') as f: + data = json.load(f)['event'] + + items = {i['id']: i['name'] for i in data['items']} + count_items = {id_: 0 for id_ in items} + questions = {q['id']: q['identifier'] for q in data['questions']} + quest_answers = {id_: {} for id_ in questions} + + for order in data['orders']: + for position in order['positions']: + item_id = position['item'] + count_items[item_id] += 1 + for answer in position['answers']: + quest_id = answer['question'] + answer_key = (item_id, answer['answer']) + quest_answers[quest_id][answer_key] = ( + quest_answers[quest_id].get(answer_key, 0) + 1 + ) + + return items, count_items, questions, quest_answers + + +def wiki_stats(items: Dict[int, str], count_items: Dict[int, int], questions: Dict[int, str], + quest_answers: Dict[int, Dict[Tuple[int, str], int]]) -> str: + """Generate stats for the Wiki page. + + Input parameters are output of the parse() function. + """ + ret = "" + ret += ('== Anmeldezahlen ==\n' + '{|class="wikitable sortable"\n' + '! Posten !! Anzahl\n' + '|-\n') + for item_id, item_name in items.items(): + ret += f"|| {item_name} || {count_items.get(item_id)}\n|-\n" + ret += "|}\n" + return ret