-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_codes_main.py
92 lines (67 loc) · 2.56 KB
/
extract_codes_main.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
import cv2
import pytesseract as pt
import time
import re
import pyautogui
import os
import pync
SCREENSHOT_NAME = "my_screenshot.png"
code_list = []
def regexMatch(text: str) -> (bool, str):
# \w matches a-z, A-Z and 0-9
# pattern = re.compile(r"\w\w\w\w\w[-]\w\w\w\w\w")
pattern = re.compile(r"[A-Z]{5}[-][A-Z]{5}")
matches = re.findall(pattern, text)
if len(matches) == 0:
return False, []
else:
return True, matches
def handleScreenshot():
if os.path.exists(SCREENSHOT_NAME):
os.remove(SCREENSHOT_NAME)
print("DELETED CURRENT SCREENSHOT")
screenshot = pyautogui.screenshot(SCREENSHOT_NAME)
print("TOOK NEW SCREENSHOT")
else:
print("TAKE SCREENSHOT")
screenshot = pyautogui.screenshot(SCREENSHOT_NAME)
def writeToFile(code: str):
with open("rabatt_codes.txt", "a") as codesFile:
codesFile.write(code + "\n")
if __name__ == "__main__":
while True:
# start time measurment for one loop
print("------------START------------")
start = time.time()
# take or delete screenshot
handleScreenshot()
# image as an np.array
img = cv2.imread(SCREENSHOT_NAME)
# extract text from image
text = pt.image_to_string(img)
# extract code from string with regex
is_valid_code, codes = regexMatch(text)
if is_valid_code:
for code in codes:
# notification if a valid code is found -> by click on show in the
# notification window, google will be opened automatically
if code in code_list:
pass
else:
# Save the code to the clipboard
os.system("echo '%s' | pbcopy" % code)
# pushup notification with button to open google
pync.notify("Code {0} is in clipboard".format(code), open='http://google.com/')
# add code to list, to prefent endless notification loop
code_list.append(code)
# write code to txt file
writeToFile(code)
print("Found Codes:")
print(code_list)
# wait for user input to continue
user_input = input("Press enter to continue...")
# mesure time, how long one loop took
print("------------END------------")
end = time.time()
print("Time needed: {0}sec".format(round(end-start, 4)))
# time.sleep(4)