#!/usr/bin/python3 """ For more information see README.md author: Sven Hauer September, 2019 """ import os import sys import io import glob import argparse import PIL.Image def read_logfile(filename): with open(filename, "rb") as binary_file: # Read the whole file at once return binary_file.read() # find start and end of png based on: # https://github.com/corkami/pics/blob/master/binary/PNG.png def check_for_png(data): start_bytes = b'\x89PNG\r\n\x1a\n' end_bytes = b'IEND\xAE\x42\x60\x82' start = data.find(start_bytes) if start < 0: raise ValueError end = data.find(end_bytes)+len(end_bytes) return (start, end) if __name__ == "__main__": parser = argparse.ArgumentParser() # a german bankleitzahl is a number beginning with [1-9] and has eight digits. parser.add_argument("--blz", type=str, metavar="BLZ", default = '*', help="enforce a bankleitzahl") parser.add_argument("-s", "--scaling", type=int, metavar="n", default=3, dest="img_scaling", help="Scale the output image.") parser.add_argument('--pngfile', metavar="filename", dest="png_outfile", default=None, type=argparse.FileType('wb'), help="Save image as png") args = parser.parse_args() log_path = "~/aqbanking/backends/aqhbci/data/banks/de/{}/logs/*.log".format(args.blz) #print(f"Using logfile glob pattern: '{log_path}'") # find all logfiles and sort them to find the newest file. input_log_file = sorted(glob.glob(os.path.expanduser(log_path)), key = lambda x: os.path.getmtime(x))[-1] print(f"Using logfile: '{input_log_file}'") # read logfile data = read_logfile(input_log_file) # find the embedded png image try: start, end = check_for_png(data) except ValueError: print("No valid png found in logfile.") sys.exit(-3) # save png image into an in memory file png_data = io.BytesIO(data[start:end]) # save image to a file if args.png_outfile: args.png_outfile.write(png_data.getbuffer()) # show a scaled image into a window img = PIL.Image.open(png_data) img = img.resize((a*args.img_scaling for a in img.size), PIL.Image.NEAREST) img.show()