-
Notifications
You must be signed in to change notification settings - Fork 2
/
flasks.py
142 lines (116 loc) · 4.24 KB
/
flasks.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import logging
import time
import random
from utils.configs import get_config
from utils.listener import KeyboardListener, LegalListener
from utils.iterators import KeyIterator
from threading import Thread, Event
from pynput.keyboard import Controller
class FlaskSequence:
def __init__(self, keys: list, interval: tuple, sequence_interval: tuple):
self.keys = keys
self.interval = interval
self.sequence_interval = sequence_interval
self._iter_keys = KeyIterator(keys)
self.sequence_end = False
@property
def next_flask(self):
if not self.sequence_end:
delay = random.uniform(self.interval[0], self.interval[1])
logging.debug(f"Sleeping for {delay}s")
time.sleep(delay)
else:
delay = time.sleep(random.uniform(self.sequence_interval[0], self.sequence_interval[1]))
logging.debug(f"Sleeping for {delay}s")
self.sequence_end = False
key = next(self._iter_keys)
if key[1]:
self.sequence_end = True
return key[0]
class LegalSequence:
def __init__(self, keys: list):
self.keys = keys
self._iter_keys = KeyIterator(keys)
self.sequence_end = False
@property
def next_flask(self):
if self.sequence_end:
self.sequence_end = False
key = next(self._iter_keys)
if key[1]:
self.sequence_end = True
return key[0]
class FlaskMacro:
def __init__(self, flasks: FlaskSequence, release_delay: tuple = (0.04, 0.17)):
self.controller = Controller()
self.is_paused = Event()
self.is_paused.set()
self.flasks = flasks
self.release_delay = release_delay
self.keyboard_listener = KeyboardListener(self)
@property
def is_macro_paused(self):
return not self.is_paused.is_set()
def pause(self):
self.is_paused.clear()
def resume(self):
self.is_paused.set()
def press_key(self, key: str, delay_range: tuple = None):
try:
self.is_paused.wait()
self.controller.press(key)
if delay_range:
delay = random.uniform(delay_range[0], delay_range[1])
logging.debug(f"Sleeping for {delay}s")
time.sleep(delay)
self.controller.release(key)
except (ValueError, AttributeError) as e:
logging.error(f"{key} is not a valid press-able key.")
def start(self):
self.keyboard_listener.start()
while True:
# Check if we're paused, if yes, block till Event is set again.
logging.info("waiting for pause to be set")
self.is_paused.wait()
self.press_key(self.flasks.next_flask, self.release_delay)
class LegalMacro:
def __init__(self, flasks: LegalSequence, activate_key):
self.controller = Controller()
self.flasks = flasks
self.keyboard_listener = LegalListener(self)
self.is_paused = Event()
self.is_paused.set()
self.activate_key = activate_key
@property
def is_macro_paused(self):
return not self.is_paused.is_set()
def pause(self):
self.is_paused.clear()
def resume(self):
self.is_paused.set()
def press_next_key(self):
if self.is_macro_paused:
return
try:
key = self.flasks.next_flask
logging.debug(f"Pressing {key}")
self.controller.press(key)
delay = random.uniform(0.04, 0.17)
time.sleep(delay)
self.controller.release(key)
except (ValueError, AttributeError) as e:
logging.error(f"Invalid key in macro sequence")
def start(self):
self.keyboard_listener.start()
while True:
self.is_paused.wait()
if __name__ == "__main__":
config = get_config()
if config['legal_macro_key']:
sequence = LegalSequence(config['keys'])
macro = LegalMacro(sequence, config['legal_macro_key'])
else:
sequence = FlaskSequence(config['keys'], tuple(config['keys_delay']), tuple(config['sequence_interval']))
macro = FlaskMacro(sequence, tuple(config['press_release_delay']))
logging.basicConfig(level=logging.DEBUG)
macro.start()