db-pkpass

Convert Deutsche Bahn PDF tickets to PKPass
git clone https://git.ce9e.org/db-pkpass.git

commit
c53d7c9dfc5ddf07b19904f89d1aa949ecf259f2
parent
42cc0e306d1e30edeb45860cd064e268df204665
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2025-06-01 10:39
allow to load content from json instead of extract from pdf

Diffstat

M README.md 8 ++++++++
M db_pkpass.py 13 +++++++++----

2 files changed, 17 insertions, 4 deletions


diff --git a/README.md b/README.md

@@ -9,6 +9,14 @@ $ python3 -m pip install -r requirements.txt
    9     9 $ python3 db_pkpass.py /path/to/my/ticket.pdf
   10    10 ```
   11    11 
   -1    12 You can also make manual adjustments before packing the pkpass file:
   -1    13 
   -1    14 ```sh
   -1    15 $ python3 db_pkpass.py /path/to/my/ticket.pdf --debug > ticket.json
   -1    16 $ $EDITOR ticket.json
   -1    17 $ python3 db_pkpass.py ticket.json
   -1    18 ```
   -1    19 
   12    20 # Limitations
   13    21 
   14    22 -   The PKPass file does not contain a signature, so it will not work with

diff --git a/db_pkpass.py b/db_pkpass.py

@@ -4,6 +4,7 @@ import datetime
    4     4 import hashlib
    5     5 import io
    6     6 import json
   -1     7 import os
    7     8 import zipfile
    8     9 from zoneinfo import ZoneInfo
    9    10 
@@ -284,14 +285,18 @@ if __name__ == '__main__':
  284   285     parser.add_argument('--debug', action='store_true')
  285   286     args = parser.parse_args()
  286   287 
  287    -1     with open(args.path, 'rb') as fh:
  288    -1         pdf = pymupdf.open(stream=fh.read())
  289    -1     content = extract_content(pdf)
   -1   288     if args.path.endswith('.json'):
   -1   289         with open(args.path) as fh:
   -1   290             content = json.load(fh)
   -1   291     else:
   -1   292         with open(args.path, 'rb') as fh:
   -1   293             pdf = pymupdf.open(stream=fh.read())
   -1   294         content = extract_content(pdf)
  290   295 
  291   296     if args.debug:
  292   297         print(json.dumps(content, indent=2))
  293   298     else:
  294    -1         output_path = args.path.replace('.pdf', '.pkpass')
   -1   299         output_path = os.path.splitext(args.path)[0] + '.pkpass'
  295   300         with open(output_path, 'wb') as fh:
  296   301             fh.write(dump_pkpass({
  297   302                 'pass.json': json.dumps(content).encode('utf-8'),