From c121f846c597726e6cb284572faa9fe89aa665fd Mon Sep 17 00:00:00 2001 From: FC Stegerman Date: Mon, 24 Jul 2023 18:17:57 +0200 Subject: [PATCH] add stocard_stores.py script --- app/src/main/res/raw/readme.md | 16 +--------- app/src/main/res/raw/stocard_stores.py | 44 ++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 15 deletions(-) create mode 100755 app/src/main/res/raw/stocard_stores.py diff --git a/app/src/main/res/raw/readme.md b/app/src/main/res/raw/readme.md index 7b88fe8ad..8f2ebe8ca 100644 --- a/app/src/main/res/raw/readme.md +++ b/app/src/main/res/raw/readme.md @@ -1,19 +1,5 @@ # stocard_stores.csv -stocard_stores.csv was created by extracting /data/data/de.stocard/de.stocard.stocard/databases/stores on a rooted devices and running the following command over it: - -``` -sqlite3 -header -csv sync_db "select id,content from synced_resources where collection = '/loyalty-card-providers/'" > stocard_providers.csv -while IFS= read -r line; do - if [ "$line" = "id,content" ]; then - echo "_id,name,barcodeFormat" > stocard_stores.csv - else - id="$(echo "$line" | cut -d ',' -f1)" - name="$(echo "$line" | cut -d ',' -f2- | sed 's/""/"/g' | sed 's/^"//g' | sed 's/"$//g' | jq -r .name)" - barcodeFormat="$(echo "$line" | cut -d ',' -f2- | sed 's/""/"/g' | sed 's/^"//g' | sed 's/"$//g' | jq -r .default_barcode_format)" - echo "$id,\"$name\",$barcodeFormat" >> stocard_stores.csv - fi -done < stocard_providers.csv -``` +`stocard_stores.csv` was created using `stocard_stores.py` Only used for data portability reasons (ensuring importing works). Do NOT copy this anywhere else or use it for any purpose other than ensuring we can import a GDPR-provided export. We want to make sure this stays under fair use. diff --git a/app/src/main/res/raw/stocard_stores.py b/app/src/main/res/raw/stocard_stores.py new file mode 100755 index 000000000..f5b309b25 --- /dev/null +++ b/app/src/main/res/raw/stocard_stores.py @@ -0,0 +1,44 @@ +#!/usr/bin/python3 + +import csv +import json +import msgpack + +MSGPACK = "bootstrapdata.msgpack" +OUTFILE = "stocard_stores.csv" + + +def load(fh): + data = [] + for r in msgpack.Unpacker(fh, raw=False): + if r["collection"] == "/loyalty-card-providers/": + d = json.loads(r["data"]) + data.append([r["resource_id"], d["name"], d["default_barcode_format"]]) + return data + + +def save(data, output_file=OUTFILE): + with open(output_file, "w") as fh: + writer = csv.writer(fh, lineterminator="\n") + writer.writerow(["_id", "name", "barcodeFormat"]) + for row in data: + writer.writerow(row) + + +if __name__ == "__main__": + import argparse + parser = argparse.ArgumentParser( + epilog=f"INPUT_FILE must be a .msgpack or .apk and defaults to {MSGPACK}; " + f"OUTPUT_FILE defaults to {OUTFILE}") + parser.add_argument("input_file", metavar="INPUT_FILE", nargs="?", default=MSGPACK) + parser.add_argument("output_file", metavar="OUTPUT_FILE", nargs="?", default=OUTFILE) + args = parser.parse_args() + if args.input_file.lower().endswith(".apk"): + import zipfile + with zipfile.ZipFile(args.input_file) as zf: + with zf.open(f"assets/{MSGPACK}") as fh: + data = load(fh) + else: + with open(args.input_file, "rb") as fh: + data = load(fh) + save(data, args.output_file)