-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreenshot.py
69 lines (54 loc) · 1.74 KB
/
screenshot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
"""Grabbing screenshots and transfer using FTP
Initial command: pip install wxpython
* Not compatiable with python3.10, fix is use python3.9
* Problematic, switched to PIL
FTP command: python -m pyftpdlib --verbose -d ftp -u admin -P admin -w
Usage: $ python3 screenshot.py <filename>
"""
import ftplib
import os
import socket
import sys
from PIL import ImageGrab
def transfer(screenshot: str) -> None:
"""Transfers screenshot over FTP
Args:
screenshot (str): Filename of the screenshot
"""
if not os.path.isdir("ftp"):
os.makedirs("ftp")
try:
ipaddr = socket.gethostbyname(socket.gethostname())
session = ftplib.FTP(ipaddr, "admin", "admin")
file = open(screenshot, "rb")
session.storbinary(f"STOR {screenshot}", file)
file.close()
session.quit()
except Exception as e:
print(f"Error: {e}")
print("FTP transfer failed, exiting...")
sys.exit(1)
def screenshot(filename: str) -> str:
"""Takes a screenshot in the current context
Args:
filename (str): The name of the screenshot to be saved to
Returns:
str: The filename if successful else None
"""
try:
# Set up screenshot and save it
# Source: https://pillow.readthedocs.io/en/stable/reference/ImageGrab.html
screenshot = ImageGrab.grab()
screenshot.save(f"{filename}")
return filename
except Exception as e:
print(f"Error: {e}")
print("Screenshot failed, exiting...")
sys.exit(1)
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: python3 {sys.argv[0]} <filename>")
sys.exit(1)
result = screenshot(sys.argv[1])
transfer(result)