Browse Source

first commit

Rafael Arce Nazario 6 years ago
commit
2b5aaca768
2 changed files with 107 additions and 0 deletions
  1. 14
    0
      README.md
  2. 93
    0
      tp-smart-process.py

+ 14
- 0
README.md View File

@@ -0,0 +1,14 @@
1
+Para que funcione, debes descargar el contenido de `/var/www/html/images` de rarceresearch.fun a tu compu.
2
+Puedes usar el siguiente comando desde tu compu en linux:
3
+
4
+```
5
+rsync -avzh root@rarceresearch.fun:/var/www/html/images /tmp
6
+```
7
+
8
+Eso syncroniza un directorio local de tu compu (`/tmp/images`) con el directorio `/var/www/html/images`  de rarceresarch.fun
9
+
10
+Luego puedes correr el programa de python localmente usando:
11
+
12
+```
13
+python tp-smart-process.py /tmp/images 4000
14
+```

+ 93
- 0
tp-smart-process.py View File

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