No Description

tp-smart-process.py 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import datetime
  2. import base64
  3. import os
  4. import glob
  5. import functools
  6. import sys
  7. from Crypto.Cipher import AES
  8. 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}
  9. def readAndProcess(fileName):
  10. f = open(fileName, 'r')
  11. return processAppTimes(f.read().split('\n'))
  12. def processAppTimes(fileContent):
  13. # fileContent = fileContent.split('\n')
  14. fileContent = [line.split(' ') for line in fileContent if len(line.strip()) > 0]
  15. # dictionary to accumulate the seconds for each application
  16. appDict = {}
  17. # for each line in the text file, extract the tokens, compute the time diff
  18. # and accumulate in dictionary
  19. isFirst = True
  20. for event in fileContent:
  21. # print("evenToken:", tokenTime)
  22. # print (event[tokenTime])
  23. (hour, minutes, seconds) = event[tokenTime].split(':')
  24. (year, month, day) = (int(event[tokenYear]), monthDict[event[tokenMonth]],int(event[tokenDay]))
  25. currDateTime = datetime.datetime(year, month, day, int(hour), int(minutes), int(seconds))
  26. # print(event, currDateTime, event[tokenApp])
  27. if (not isFirst):
  28. timeDelta = currDateTime - prevDateTime
  29. # print(timeDelta, prevApp)
  30. if prevApp in appDict:
  31. appDict[prevApp] = appDict[prevApp] + timeDelta.seconds
  32. else:
  33. appDict[prevApp] = 0 + timeDelta.seconds
  34. else:
  35. isFirst = False
  36. prevDateTime = currDateTime
  37. prevApp = event[tokenApp]
  38. return appDict
  39. def decryptFileAndProcess(encryptedFName):
  40. key = "This is a secret"
  41. f = open(encryptedFName, 'rb')
  42. print("Decrypting the file: %s ..." % encryptedFName)
  43. cipher = AES.new(key)
  44. decrypted = cipher.decrypt(f.read())
  45. fileContent = decrypted.decode("utf-8").split("\n")
  46. # positions of tokens in each line
  47. # print("Is this a time? ", fileContent[0][tokenTime])
  48. # print("Is this a year month day? ", fileContent[0][tokenYear], fileContent[0][tokenMonth], fileContent[0][tokenDay])
  49. # print("If not, modify the token values....")
  50. # print(fileContent)
  51. # print([line for line in fileContent if len(line) > 10])
  52. return processAppTimes([line for line in fileContent if len(line) > 10])
  53. def latestUserFile(searchDir, userID):
  54. # search_dir = "/tmp/images/"
  55. print("Searching dir %s for latest file of userID: %s ...." % (searchDir, userID))
  56. files = [f for f in glob.glob(searchDir + "/" + userID + "-*") if os.path.isfile(f)]
  57. return max(files, key = lambda x: os.path.getmtime(x))
  58. if len(sys.argv) != 3:
  59. print("Usage: " + sys.argv[0] + " (directory) (UserID)")
  60. print("Example: " + sys.argv[0] + " /tmp/images 1000")
  61. sys.exit(1)
  62. searchDir = sys.argv[1]
  63. userID = sys.argv[2]
  64. (tokenApp,tokenMonth, tokenDay, tokenYear, tokenTime) = (0,3,4,7,5)
  65. # print(latestUserFile(searchDir,userID))
  66. results = decryptFileAndProcess( latestUserFile(searchDir,userID))
  67. print("Results:\n",results)
  68. # print(decryptFileAndProcess( latestUserFile("5000")))