-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTheParser.py
295 lines (284 loc) · 10.4 KB
/
TheParser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import csv,datetime,sys
dateTimeFormat = "%Y%m%d%H%M%S"
def SaveCDR(data,name):
with open(name+".csv","w",newline="") as csvfile:
writer = csv.writer(csvfile,delimiter=",")
writer.writerows(data)
def ReadCDR(filename):
CDRRaw = []
with open(filename+".csv","r",newline="") as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for record in reader:
CDRRaw.append(record)
return CDRRaw
def ParseO2CDR(CDRRaw):
ID = 0
CDR = []
for record in CDRRaw:
if record[0] not in ("MT","MO"):
continue
else:
#assign ID
r = []
r.append(ID)
ID += 1
#parse startDateTime
startDate = record[1] #dd/mm/yyyy
startDate = startDate[6:] + startDate[3:5] + startDate[:2]
startTime = record[2] #hh:mm:ss
startTime = startTime[:2] + startTime[3:5] + startTime[6:]
startDateTime = startDate + startTime
r.append(startDateTime)
#parse endDateTime
startDateTime = datetime.datetime.strptime(startDateTime,dateTimeFormat)
duration = record[3] #hh:mm:ss
seconds = int(duration[6:])
minutes = int(duration[3:5])
hours = int(duration[:2])
duration = datetime.timedelta(seconds=seconds,minutes=minutes,hours=hours)
endDateTime = startDateTime + duration
endDateTime = endDateTime.strftime(dateTimeFormat)
r.append(endDateTime)
#parse call type
callType = record[0]
r.append(callType)
#parse calling number
calling = record[6]
if calling[:3] == "+44":
calling = "0" + calling[3:]
if (len(calling) != 10) and (len(calling) != 11):
calling = calling.zfill(11)
r.append(calling)
#parse called number
called = record[10]
if called[:3] == "+44":
called = "0" + called[3:]
if (len(called) != 10) and (len(called) != 11):
called = called.zfill(11)
r.append(called)
CDR.append(r)
return CDR
def ParseVodafoneCDR(CDRRaw):
ID = 0
CDR = []
for record in CDRRaw:
if record[0] not in ("MT","MO"):
continue
else:
#assign ID
r = []
r.append(ID)
ID += 1
#parse startDateTime
startDateTime = record[2]
if len(startDateTime) == 19:
startDateTime = startDateTime[6:10] + startDateTime[3:5] + startDateTime[:2] + startDateTime[11:13] + startDateTime[14:16] + startDateTime[17:19]
elif len(startDateTime) == 16:
startDateTime = startDateTime[6:10] + startDateTime[3:5] + startDateTime[:2] + startDateTime[11:13] + startDateTime[14:16] + "00"
r.append(startDateTime)
#parse endDateTime
startDateTime = datetime.datetime.strptime(startDateTime,dateTimeFormat)
duration = record[4]
duration = datetime.timedelta(seconds=int(duration))
endDateTime = startDateTime + duration
endDateTime = endDateTime.strftime(dateTimeFormat)
r.append(endDateTime)
#parse call type
callType = record[0]
r.append(callType)
#parse calling number
calling = record[5]
if calling[:3] == "+44":
calling = "0" + calling[3:]
if (len(calling) != 10) and (len(calling) != 11):
calling = calling.zfill(11)
r.append(calling)
#parse called number
called = record[6]
if called[:3] == "+44":
called = "0" + called[3:]
if (len(called) != 10) and (len(called) != 11):
called = called.zfill(11)
r.append(called)
CDR.append(r)
return CDR
def ParseEECDR(CDRRaw):
ID = 0
CDR = []
for record in CDRRaw:
if record[0] == "Start Date":
continue
else:
#assign ID
r = []
r.append(ID)
ID += 1
#parse startDateTime
startDate = record[0]
startTime = record[1]
startDateTime = startDate[6:] + startDate[3:5] + startDate[:2] + startTime[:2] + startTime[3:5] + startTime[6:8]
r.append(startDateTime)
#parse endDateTime
endDate = record[2]
endTime = record[3]
endDateTime = endDate[6:] + endDate[3:5] + endDate[:2] + endTime[:2] + endTime[3:5] + endTime[6:8]
r.append(endDateTime)
#parse call type
callType = record[7]
r.append(callType)
#parse calling number
calling = record[4]
if calling[:3] == "+44":
calling = "0" + calling[3:]
if (len(calling) != 10) and (len(calling) != 11):
calling = calling.zfill(11)
r.append(calling)
#parse called number
called = record[5]
if called[:3] == "+44":
called = "0" + called[3:]
if (len(called) != 10) and (len(called) != 11):
called = called.zfill(11)
r.append(called)
CDR.append(r)
return CDR
def ParseH3GCDR(CDRRaw):
ID = 0
CDR = []
for record in CDRRaw:
if record[0] == "Date and Time of Call":
continue
else:
#assign ID
r = []
r.append(ID)
ID += 1
#parse startDateTime
startDateTime = record[0]
if len(startDateTime) == 19: #dd/mm/yyyy hh:mm:ss
startDateTime = startDateTime[6:10] + startDateTime[3:5] + startDateTime[:2] + startDateTime[11:13] + startDateTime[14:16] + startDateTime[17:19]
elif len(startDateTime) == 16: #dd/mm/yyyy hh:mm
startDateTime = startDateTime[6:10] + startDateTime[3:5] + startDateTime[:2] + startDateTime[11:13] + startDateTime[14:16] + "00"
else:
raise ValueError("startDateTime not in correct format")
r.append(startDateTime)
startDateTime = datetime.datetime.strptime(startDateTime,dateTimeFormat)
#parse endDateTime
duration = record[8]
duration = datetime.timedelta(seconds=int(duration))
endDateTime = startDateTime + duration
endDateTime = endDateTime.strftime(dateTimeFormat)
r.append(endDateTime)
#parse call type
callType = record[7]
r.append(callType)
#parse calling number
calling = record[1]
if calling[:3] == "+44":
calling = "0" + calling[3:]
if (len(calling) != 10) and (len(calling) != 11):
calling = calling.zfill(11)
r.append(calling)
#parse called number
called = record[3]
if called[:3] == "+44":
called = "0" + called[3:]
if (len(called) != 10) and (len(called) != 11):
called = called.zfill(11)
r.append(called)
CDR.append(r)
return CDR
def MergeH3G(CDR1,CDR2):
#CDRs from H3G parser, no ID or class labels
CDR = []
t = datetime.datetime(2100,1,1)
index = -1
ID = 0
while (len(CDR1) != 0) or (len(CDR2) != 0):
#print("starting loop")
if len(CDR1) != 0:
#print("CDR1 not empty, for loop time")
for i in range(len(CDR1)):
end = datetime.datetime.strptime(CDR1[i][2],dateTimeFormat)
if end < t:
t = end
index = (i,1)
if len(CDR2) != 0:
#print("CDR2 not empty, for loop time")
for i in range(len(CDR2)):
end = datetime.datetime.strptime(CDR2[i][2],dateTimeFormat)
if end < t:
t = end
index = (i,2)
if index[1] == 1:
r = CDR1.pop(index[0])
CDR.append([ID] + r[1:])
ID += 1
t = datetime.datetime(2100,1,1)
else:
r = CDR2.pop(index[0])
CDR.append([ID] + r[1:])
ID += 1
t = datetime.datetime(2100,1,1)
#print(CDR)
return CDR
def IdentifyProvider(parameters):
if len(parameters) == 1:
filename = parameters[0]
if filename[-4:] == ".csv":
filename = filename[:-4]
CDR1 = ReadCDR(filename)
CDR2 = None
elif len(parameters) == 2:
filename = parameters[0]
if filename[-4:] == ".csv":
filename = filename[:-4]
filename2 = parameters[1]
if filename2[-4:] == ".csv":
filename2 = filename2[:-4]
CDR1 = ReadCDR(filename)
CDR2 = ReadCDR(filename2)
if CDR1[0][0] == "Start Date":
CDR = ParseEECDR(CDR1) #EE
SaveCDR(CDR,filename+"PARSED")
elif CDR1[0][0] == "Date and Time of Call" and CDR2[0][0] == "Date and Time of Call":
CDRt1 = ParseH3GCDR(CDR1) #H3G
CDRt2 = ParseH3GCDR(CDR2)
CDR = MergeH3G(CDRt1,CDRt2)
SaveCDR(CDR,filename+"PARSED")
elif CDR1[0][0] == "Record":
CDR = ParseO2CDR(CDR1) #O2
SaveCDR(CDR,filename+"PARSED")
elif CDR1[0][0] == "Event - Tariff":
CDR = ParseVodafoneCDR(CDR1) #Vodafone
SaveCDR(CDR,filename+"PARSED")
else:
print("There is no headers to detect the mobile network")
print("1. EE")
print("2. H3G")
print("3. O2")
print("4. Vodafone")
x = int(input("Please select an option above: "))
if x == 1:
CDR = ParseEECDR(CDR1)
SaveCDR(CDR,filename+"PARSED")
elif x == 2:
if len(parameters) != 2:
raise ValueError("Two files are required for H3G, both incoming and outgoing")
else:
CDRt1 = ParseH3GCDR(CDR1)
CDRt2 = ParseH3GCDR(CDR2)
CDR = MergeH3G(CDRt1,CDRt2)
SaveCDR(CDR,filename+"PARSED")
elif x == 3:
CDR = ParseO2CDR(CDR1)
SaveCDR(CDR,filename+"PARSED")
elif x == 4:
CDR = ParseVodafoneCDR(CDR1)
SaveCDR(CDR,filename+"PARSED")
else:
raise ValueError("That is not a valid option!")
def main():
IdentifyProvider(sys.argv[1:3])
if __name__ == "__main__":
main()