started working towards cleaning up the card data, split the program into smaller more specialized files to improve readability

This commit is contained in:
Michael Scalzetti 2022-10-21 22:12:07 -04:00
parent 54248d702b
commit 0c0fdf68c5
3 changed files with 92 additions and 35 deletions

41
main.py Executable file
View File

@ -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()

49
parse.py Normal file
View File

@ -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

37
read.py Executable file → Normal file
View File

@ -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()