From 0c0fdf68c53e2bbaba3aa60e24e43c1a3e525177 Mon Sep 17 00:00:00 2001 From: Michael Scalzetti Date: Fri, 21 Oct 2022 22:12:07 -0400 Subject: [PATCH] started working towards cleaning up the card data, split the program into smaller more specialized files to improve readability --- main.py | 41 +++++++++++++++++++++++++++++++++++++++++ parse.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ read.py | 37 ++----------------------------------- 3 files changed, 92 insertions(+), 35 deletions(-) create mode 100755 main.py create mode 100644 parse.py mode change 100755 => 100644 read.py diff --git a/main.py b/main.py new file mode 100755 index 0000000..b590b32 --- /dev/null +++ b/main.py @@ -0,0 +1,41 @@ +#! /usr/bin/python + +from read import * +from parse import * + +def get_intro(): + return """ + ___ _ +/'\_/`\ | _`\ ( ) +| | _ _ __ | (_) ) __ _ _ _| | +| (_) | /'_` ) /'_ `\| , / /'__`\ /'_` ) /'_` | +| | | |( (_| |( (_) || |\ \ ( ___/( (_| |( (_| | +(_) (_)`\__,_)`\__ |(_) (_)`\____)`\__,_)`\__,_) + ( )_) | + \___/' + """ + +def get_card_data(device): + raw_data = read(device) + card_data = parse_raw_data(raw_data) + + return card_data + +def get_card_string(device): + card = get_card_data(device) + + return card + +def main(): + print(get_intro()) + readers = get_readers() + device = get_device_from_readers(readers) + if device is None: + return + while True: + + print(f"\n{get_card_string(device)}") + print("\n"*3) + +if __name__ == "__main__": + main() diff --git a/parse.py b/parse.py new file mode 100644 index 0000000..fc09e0a --- /dev/null +++ b/parse.py @@ -0,0 +1,49 @@ +def parse_trim_start(raw_data): + for i in range(len(raw_data)): + cur_byte = raw_data[i].to_bytes(1,"big") + if cur_byte != b'\x00' and cur_byte!= b"\x10": + return raw_data[i:] + return b"" + +def parse_trim_end(raw_data): + index = 0 + for i in range(len(raw_data)): + if raw_data[i].to_bytes(1,"big") != b'\x00': + index = i + return raw_data[:index+1] + + + +def get_tracks_from_raw_data(raw_data): + SPLIT_CHAR = b"?" + + raw_data = parse_trim_end(parse_trim_start(raw_data)) + tracks = [parse_trim_end(parse_trim_start(track)) for track in raw_data.split(SPLIT_CHAR)] + + for track in tracks: + print(f"\t{track}") + + return tracks + + +def parse_raw_data(raw_data): + tracks = get_tracks_from_raw_data(raw_data) + card_data = {} + + for item in ["Track1", "Track2", "Track3"]: + card_data[item]={} + + # T1 + card_data["Track1"]["PAN"] = "" + card_data["Track1"]["NAME"] = "" + card_data["Track1"]["DATA"] = "" + + # T2 + card_data["Track2"]["PAN"]="" + card_data["Track2"]["DATA"]="" + + # T3 + card_data["Track3"]["PAN"]= "" + card_data["Track3"]["DATA"]="" + + return card_data diff --git a/read.py b/read.py old mode 100755 new mode 100644 index e1cb341..5a9d38a --- a/read.py +++ b/read.py @@ -1,6 +1,6 @@ -#! /usr/bin/python import hid + def get_readers(): return [item for item in hid.enumerate() if item["manufacturer_string"]=="Mag-Tek"] @@ -30,38 +30,5 @@ def get_device_from_readers(readers): def read(device): print("Waiting for swipe...") - return device.read(256)[3:] + return device.read(256) -def parse_raw_data(raw_data): - END_CHAR = ";" - START_CHAR = "%" - SPLIT_CHAR = b"?" - - print(raw_data) - tracks = raw_data.split(SPLIT_CHAR) - - print(tracks) - return - - data = raw_data.decode() - - - - end = end if end != -1 else len(data) - tracks = data[start:end].replace('\x00','').replace("\x10","").split(SPLIT_CHAR) - final_output = [track for track in tracks if len(track)!=0] - - return final_output - -def main(): - readers = get_readers() - device = get_device_from_readers(readers) - if device is None: - return - while True: - raw_data = read(device) - print(parse_raw_data(raw_data)) - print() - -if __name__ == "__main__": - main()