Skip to content

Instantly share code, notes, and snippets.

@cyberheartmi9
Last active September 24, 2020 10:02
Show Gist options
  • Select an option

  • Save cyberheartmi9/858f5677b53dbe3ad11def8860e8cae7 to your computer and use it in GitHub Desktop.

Select an option

Save cyberheartmi9/858f5677b53dbe3ad11def8860e8cae7 to your computer and use it in GitHub Desktop.
import json
import re
import csv
from datetime import datetime
import time
YEAR=datetime.now().strftime("%Y")
MONTH=datetime.now().strftime("%m")
DAY=datetime.now().strftime("%d")
filename="Merchant-{}-{}-{}.log".format(YEAR,MONTH,DAY)
#def read(f):
# f.seek(0,2)
# while True:
# line = f.readline()
# if not line:
# time.sleep(0.1)
# continue
# yield line
FILE=open("test.log","r")
#RFILE=read(FILE)
#OUTFILE=open("log.csv","w")
#OUTFILE.writelines("date,timestamp,ip,port,PAN,Terminalid,tranAmount,tranCurrencyCode,expDate,systemTraceAuditNumber,clientId,tranDateTime,responseMessage,responseCode,responseStatus,workingKey\n")
################################# IN
Terminalid=""
PAN=""
tranAmount=""
tranCurrencyCode=""
expDate=""
systemTraceAuditNumber=""
clientId=""
tranDateTime=""
######################### OUT
responseMessage=""
responseCode=""
additionalAmount=""
tranFee=""
responseStatus=""
referenceNumber=""
workingKey=""
######################### info
date=""
timestamp=""
ip=""
port=""
for i in FILE.readlines():
data=i.split(" ")
#print()
x=data[7].strip("\n")
#print(i)
#break
##################################### Regex #####################################
pan=re.findall('"PAN":"[^"]*\"',x)
terminal = re.findall('"terminalId":"[^"]*\"', x)
TranAmount=re.findall('"tranAmount":"[^"]*\"',x)
TranCurrencyCode=re.findall('"tranCurrencyCode":"[^"]*\"',x)
ExpDate=re.findall('"expDate":"[^"]*\"',x)
SystemTraceAuditNumber=re.findall('"systemTraceAuditNumber":"[^"]*\"',x)
ClientId=re.findall('"clientId":"[^"]*\"',x)
TranDateTime=re.findall('"tranDateTime":"[^"]*\"',x)
ResponseMessage=re.findall('"responseMessage":\"\w*\"',x)
ResponseCode=re.findall('"responseCode":(\d+)*',x)
ResponseStatus=re.findall('"responseStatus":\"\w+\"',x)
WorkingKey=re.findall('"workingKey":\"\w+\"',x)
if len(pan)!=0:
PAN=pan[0].split(":")[1]
else:
PAN="NULL"
if len(terminal)!=0:
Terminalid=terminal[0].split(":")[1]
else:
Terminalid="NULL"
if len(TranAmount)!=0:
tranAmount=TranAmount[0].split(":")[1]
else:
tranAmount="NULL"
if len(TranCurrencyCode)!=0:
tranCurrencyCode=TranCurrencyCode[0].split(":")[1]
else:
tranCurrencyCode="NULL"
if len(ExpDate)!=0:
expDate=ExpDate[0].split(":")[1]
else:
expDate="NULL"
if len(SystemTraceAuditNumber)!=0:
systemTraceAuditNumber=SystemTraceAuditNumber[0].split(":")[1]
else:
systemTraceAuditNumber="NULL"
if len(ClientId)!=0:
clientId=ClientId[0].split(":")[1]
else:
clientId="NULL"
if len(TranDateTime)!=0:
tranDateTime=TranDateTime[0].split(":")[1]
else:
tranDateTime="NULL"
if len(ResponseMessage)!=0:
responseMessage=ResponseMessage[0].split(":")[1]
#print(responseMessage)
else:
responseMessage="NULL"
if len(ResponseCode)!=0:
responseCode=ResponseCode[0]
#print(responseCode)
else:
responseCode="NULL"
if len(ResponseStatus)!=0:
responseStatus=ResponseStatus[0].split(":")[1]
#print(responseStatus)
else:
responseStatus="Failed"
if len(WorkingKey)!=0:
workingKey=WorkingKey[0].split(":")[1]
#print(workingKey)
else:
workingKey="NULL"
date=data[0]
timestamp=data[1].split(",")[0]
ip=data[5].split(":")[0]
port=data[5].split(":")[1]
print("{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{} ".format(date,timestamp,ip,port,PAN,Terminalid,tranAmount,tranCurrencyCode,expDate,systemTraceAuditNumber,clientId,tranDateTime,responseMessage,responseCode,responseStatus,workingKey))
# log="{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{} \n".format(date,timestamp,ip,port,PAN,Terminalid,tranAmount,tranCurrencyCode,expDate,systemTraceAuditNumber,clientId,tranDateTime,responseMessage,responseCode,responseStatus,workingKey)
#OUTFILE.writelines(log)
#OUTFILE.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment