123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import datetime
- import base64
- import os
- import glob
- import functools
- import sys
-
- from Crypto.Cipher import AES
-
- monthDict = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6, 'Jul': 7, 'Aug': 8, 'Sep':9, 'Oct':10, 'Nov': 11, 'Dec' : 12}
-
-
- def readAndProcess(fileName):
- f = open(fileName, 'r')
- return processAppTimes(f.read().split('\n'))
-
- def processAppTimes(fileContent):
- # fileContent = fileContent.split('\n')
- fileContent = [line.split(' ') for line in fileContent if len(line.strip()) > 0]
-
- # dictionary to accumulate the seconds for each application
- appDict = {}
-
-
- # for each line in the text file, extract the tokens, compute the time diff
- # and accumulate in dictionary
-
- isFirst = True
- for event in fileContent:
- # print("evenToken:", tokenTime)
- # print (event[tokenTime])
- (hour, minutes, seconds) = event[tokenTime].split(':')
-
- (year, month, day) = (int(event[tokenYear]), monthDict[event[tokenMonth]],int(event[tokenDay]))
- currDateTime = datetime.datetime(year, month, day, int(hour), int(minutes), int(seconds))
- # print(event, currDateTime, event[tokenApp])
- if (not isFirst):
- timeDelta = currDateTime - prevDateTime
- # print(timeDelta, prevApp)
- if prevApp in appDict:
- appDict[prevApp] = appDict[prevApp] + timeDelta.seconds
- else:
- appDict[prevApp] = 0 + timeDelta.seconds
- else:
- isFirst = False
- prevDateTime = currDateTime
- prevApp = event[tokenApp]
-
- return appDict
-
- def decryptFileAndProcess(encryptedFName):
- key = "This is a secret"
-
- f = open(encryptedFName, 'rb')
- print("Decrypting the file: %s ..." % encryptedFName)
-
- cipher = AES.new(key)
- decrypted = cipher.decrypt(f.read())
- fileContent = decrypted.decode("utf-8").split("\n")
-
- # positions of tokens in each line
-
- # print("Is this a time? ", fileContent[0][tokenTime])
- # print("Is this a year month day? ", fileContent[0][tokenYear], fileContent[0][tokenMonth], fileContent[0][tokenDay])
- # print("If not, modify the token values....")
- # print(fileContent)
- # print([line for line in fileContent if len(line) > 10])
- return processAppTimes([line for line in fileContent if len(line) > 10])
-
-
-
- def latestUserFile(searchDir, userID):
- # search_dir = "/tmp/images/"
- print("Searching dir %s for latest file of userID: %s ...." % (searchDir, userID))
- files = [f for f in glob.glob(searchDir + "/" + userID + "-*") if os.path.isfile(f)]
- return max(files, key = lambda x: os.path.getmtime(x))
-
-
-
- if len(sys.argv) != 3:
- print("Usage: " + sys.argv[0] + " (directory) (UserID)")
- print("Example: " + sys.argv[0] + " /tmp/images 1000")
- sys.exit(1)
-
- searchDir = sys.argv[1]
- userID = sys.argv[2]
-
- (tokenApp,tokenMonth, tokenDay, tokenYear, tokenTime) = (0,3,4,7,5)
- # print(latestUserFile(searchDir,userID))
- results = decryptFileAndProcess( latestUserFile(searchDir,userID))
-
- print("Results:\n",results)
- # print(decryptFileAndProcess( latestUserFile("5000")))
|