Browse Source

Sometimes the decrypted files have fluff at the end which could confuse the python script. Added condition to check

Rafael Arce Nazario 6 years ago
parent
commit
4d92f078a0
1 changed files with 20 additions and 16 deletions
  1. 20
    16
      tp-smart-process.py

+ 20
- 16
tp-smart-process.py View File

@@ -30,26 +30,30 @@ def processAppTimes(fileContent):
30 30
     # for each line in the text file, extract the tokens, compute the time diff
31 31
     # and accumulate in dictionary
32 32
 
33
+    maxTokenIdx = max(tokenApp, tokenMonth, tokenDay, tokenYear, tokenTime)
34
+
33 35
     isFirst = True
34 36
     for event in fileContent:
35 37
 #         print("evenToken:", tokenTime)
36 38
 #         print (event[tokenTime])
37
-        (hour, minutes, seconds) = event[tokenTime].split(':') 
38
-        
39
-        (year, month, day) = (int(event[tokenYear]), monthDict[event[tokenMonth]],int(event[tokenDay]))
40
-        currDateTime = datetime.datetime(year, month, day, int(hour), int(minutes), int(seconds)) 
41
-#         print(event, currDateTime, event[tokenApp])
42
-        if (not isFirst):
43
-            timeDelta = currDateTime - prevDateTime 
44
-#             print(timeDelta, prevApp)
45
-            if prevApp in appDict:
46
-                appDict[prevApp] = appDict[prevApp] + timeDelta.seconds 
39
+
40
+        if len(event) > maxTokenIdx:
41
+            (hour, minutes, seconds) = event[tokenTime].split(':') 
42
+            
43
+            (year, month, day) = (int(event[tokenYear]), monthDict[event[tokenMonth]],int(event[tokenDay]))
44
+            currDateTime = datetime.datetime(year, month, day, int(hour), int(minutes), int(seconds)) 
45
+    #         print(event, currDateTime, event[tokenApp])
46
+            if (not isFirst):
47
+                timeDelta = currDateTime - prevDateTime 
48
+    #             print(timeDelta, prevApp)
49
+                if prevApp in appDict:
50
+                    appDict[prevApp] = appDict[prevApp] + timeDelta.seconds 
51
+                else:
52
+                    appDict[prevApp] = 0 + timeDelta.seconds
47 53
             else:
48
-                appDict[prevApp] = 0 + timeDelta.seconds
49
-        else:
50
-            isFirst = False
51
-        prevDateTime = currDateTime
52
-        prevApp = event[tokenApp]
54
+                isFirst = False
55
+            prevDateTime = currDateTime
56
+            prevApp = event[tokenApp]
53 57
 
54 58
     return appDict
55 59
 
@@ -68,7 +72,7 @@ def decryptFileAndProcess(encryptedFName):
68 72
     # print("Is this a time? ", fileContent[0][tokenTime])
69 73
     # print("Is this a year month day? ", fileContent[0][tokenYear], fileContent[0][tokenMonth], fileContent[0][tokenDay])
70 74
     # print("If not, modify the token values....")
71
-#     print(fileContent)
75
+    # print(fileContent)
72 76
 #     print([line for line in fileContent if len(line) > 10])
73 77
     return processAppTimes([line for line in fileContent if len(line) > 10])
74 78