-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwakey.py
85 lines (67 loc) · 2.96 KB
/
wakey.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import keyboard, time, sys
import os, logging
import ctypes
from datetime import datetime
def main(duration, frequency, verbose, log):
starttime = time.perf_counter()
if log:
# Setup logging
file, ext = os.path.splitext(__file__)
logfile = file+'.log'
logging.basicConfig(filename=logfile, filemode='w', format='%(asctime)s - %(levelname)s - %(message)s', level=logging.INFO, datefmt='%b-%d-%y %H:%M:%S')
if sys.platform == "win32":
#cmd = 'mode 30,10'
#os.system(cmd)
ctypes.windll.kernel32.SetConsoleTitleW('Wakey')
keypress = 'f15'
if sys.platform == 'linux':
if os.getuid() != 0:
print("Wakey needs to run as root/sudo. Please try relaunch")
sys.exit()
else:
keypress = 'shift'
# time.sleep() is in seconds. Multiple minutes wanted by 60 seconds to get delay
delay = frequency * 60
thumb = "\U0001F44D"
computer = "\U0001F5A5\U0000FE0F"
print(f"Keeping {computer} awake {thumb}")
if verbose:
print(f'Pressing {keypress} every {delay} seconds')
while True:
try:
if sys.platform == "win32":
keyboard.press_and_release(keypress)
if verbose:
print(f'Button {keypress} was pressed at {currenttime()}')
if log:
logging.info(f'Button {keypress} was pressed at {currenttime()}')
if sys.platform == "linux":
keyboard.press_and_release(keypress)
time.sleep(delay) # Sleep for the amount of seconds generated
if duration:
scriptduration = time.perf_counter() - starttime
if scriptduration > duration:
print("Time duration reached. Ending Wakey.")
sys.exit()
except KeyboardInterrupt:
# quit
sys.exit()
def currenttime():
t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)
return current_time
# If we are running the file directly, lets add some arguments for our script.
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="Simply keep your computer awake and session active")
parser.add_argument("-d", "--duration", type=int, help="Length of time in minutes, default is forever")
parser.add_argument("-f", "--frequency", type=int, help="Frequency of keypress in minutes, default is 1 minute", default=1)
parser.add_argument("-v", "--verbose", action='store_true', help="Verbosely see the output in the console")
parser.add_argument("-l", "--log", action='store_true', help="Creates a log of the application for later reference")
# gather arguments
args = parser.parse_args()
duration = args.duration
frequency = args.frequency
verbose = args.verbose
log = args.log
main(duration, frequency, verbose, log)