-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver_code.py
314 lines (229 loc) · 12.1 KB
/
driver_code.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#Essential
import os
import time
#Selenium
import pyperclip
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
#QR Libraries
import qrcode
from qrcode.image.styledpil import StyledPilImage
from qrcode.image.styles.colormasks import SolidFillColorMask
from matplotlib import colors
#Performance
import threading
from multiprocessing import cpu_count, Process
#Function to create folder to store QR codes
def createFolder():
today = str(time.ctime(time.time()))
today = today.replace(":",".")
folder_name = f'QRCODES {today}'
os.mkdir(folder_name)
return folder_name
#Generating Links for QR Codes
def retriveLink(links_arr, link_password, expirey_date, confirmation_email, custom_message, amount_of_links):
print("Generating Links for QR Codes, please wait...")
cTime = time.ctime(time.time())
driverOpts = Options()
driverOpts.headless = False
driver = webdriver.Chrome(options= driverOpts)
driver.get("https://singleuse.link/")
for iterations in range(amount_of_links):
if iterations >= 1 :
driver.back()
driver.refresh()
else:
pass
# Secret Message Box
try:
if custom_message == "":
driver.find_element("id", "data").send_keys(cTime)
else:
driver.find_element("id", "data").send_keys(custom_message)
except:
pass
# Link Pass
try:
if link_password == "":
pass
else:
driver.find_element("id", "inputPassword").send_keys(link_password)
except:
pass
#Email Alert
try:
if confirmation_email == "":
pass
else:
driver.find_element("id", 'inputEmail').send_keys(confirmation_email)
except:
pass
#Drop down for link expirey
grade_dropdown = Select(driver.find_element("id", "select"))
grade_dropdown.select_by_visible_text(expirey_date)
#Radio Button selection
driver.find_element("id", 'optionsRadios1').click()
#Create Button click
driver.find_element("id", "createbtn2").click()
#Copy to CLipboard
driver.find_element(By.XPATH, "/html/body/div[2]/button").click();
link = pyperclip.paste()
links_arr.append(link)
print(iterations + 1)
driver.close()
#Function to create QR codes
def createQRCodes(data, innerImagePath, folder, foreground_color, background_color):
if len(set(data)) != len(data):
raise Exception("There was an issue ")
foreground = list(colors.to_rgb(foreground_color))
background = list(colors.to_rgb(background_color))
for i in range(len(foreground)):
foreground[i] = int(foreground[i] * 255)
for i in range(len(background)):
background[i] = int(background[i] * 255)
foreground = tuple(foreground)
background = tuple(background)
print("Generating QR Codes, please wait...")
for i in range(len(data)):
qr = qrcode.QRCode(version = 1, error_correction=qrcode.constants.ERROR_CORRECT_H, box_size = 20, border = 2)
qr.add_data(data[i])
qr.make(fit = True)
img = qr.make_image(image_factory=StyledPilImage, embeded_image_path = innerImagePath,
color_mask= SolidFillColorMask(back_color= background, front_color = foreground))
img.save(f"{folder}/QR{i}.png")
def getOneTimeLink(functionToCall, argList, numOfLinks):
numCorrection = lambda threads : numOfLinks % threads
threads = []
if numOfLinks <= 10:
argTuple = tuple(argList.append(numOfLinks))
th1 = threading.Thread(target = functionToCall, args=(argTuple))
threads.append(th1)
elif numOfLinks <= 50:
numOfLinksPerThread = (numOfLinks - numCorrection(2)) / 2
argTuple = tuple(argList.append(numOfLinksPerThread))
argTupleMod = tuple(argList.append(numOfLinksPerThread + numCorrection(2)))
th1 = threading.Thread(target = functionToCall, args=(argTuple))
th2 = threading.Thread(target = functionToCall, args=(argTupleMod))
threads.extend([th1, th2])
elif numOfLinks <= 150:
numOfLinksPerThread = (numOfLinks - numCorrection(3)) / 2
argTuple = tuple(argList.append(numOfLinksPerThread))
argTupleMod = tuple(argList.append(numOfLinksPerThread + numCorrection(3)))
th1 = threading.Thread(target = functionToCall, args=(argTuple))
th2 = threading.Thread(target = functionToCall, args=(argTuple))
th3 = threading.Thread(target = functionToCall, args=(argTupleMod))
threads.extend([th1, th2, th3])
else:
numOfLinksPerThread = (numOfLinks - numCorrection(2)) / 2
argTuple = tuple(argList.append(numOfLinksPerThread))
argTupleMod = tuple(argList.append(numOfLinksPerThread + numCorrection(4)))
th1 = threading.Thread(target = functionToCall, args=(argTuple))
th2 = threading.Thread(target = functionToCall, args=(argTuple))
th3 = threading.Thread(target = functionToCall, args=(argTuple))
th4 = threading.Thread(target = functionToCall, args=(argTupleMod))
threads.extend([th1, th2, th3, th4])
for i in threads:
i.start()
def main() :
try:
linksArr = []
#Welcome Text
print("Welcome to the QR Ticket Generator ::")
print("How it works :: \n #You will be asked to add your customization options \n #The program then will run to create your QR codes \n #Codes will be stored in a folder with today's date in the same directory as the script folder")
print("******************************************************************************************")
#Adding an Image
imagePath = input("If you would like to add a logo to the center of your QR Code please input it here (must be png) (if not leave it blank) :: ")
if imagePath == "":
imagePath = "defaultLogo.png"
else:
if os.path.exists(imagePath):
ext = list(os.path.splitext(imagePath))
ext = ext[-1].lower()
if ext != '.png':
print(f"*X* File{imagePath} is not a png \n *X*defaulting to stock logo \n")
imagePath = "defaultLogo.png"
else:
pass
else:
print(f"*X* File{imagePath} does not exist \n *X*defaulting to stock logo")
imagePath = "defaultLogo.png"
print("******************************************************************************************")
#Numbers of Qr Codes
while True:
try:
numOfLinks = int(input("Please enter the number of links you would like to generate :: "))
except:
print("Please enter a whole number (no decimals, and no characters!) \n Please Try Again :(")
else:
break
print("******************************************************************************************")
#Adding a Password
linkPassword = input("If you would like to add a password to expire your qr codes, add it here (If not leave it blank) :: ")
print("******************************************************************************************")
#Selecting a Expirey Date
expireyDate = {1 : "12 hours", 2 : "1 day", 3 : "2 weeks", 4 : "1 month"}
while True:
try:
optExpireyDate = int(input("Select an expirey date \n (1 = 12 Hours) (2 = 1 Day) (3 = 2 Weeks) (4 = 1 Month) (5 = 3 Months) :: "))
except:
print("Please select one of these options (1 = 12 Hours) (2 = 1 Day) (3 = 2 Weeks) (4 = 1 Month) (5 = 3 Months) \n Please Try Again :( :: ")
else:
break
if expireyDate.get(optExpireyDate) == None :
expireyDate = "3 month"
else:
expireyDate = expireyDate.get(optExpireyDate)
print("******************************************************************************************")
#Adding a Confirmation Email
confirmationEmail = input("If you would like to be sent a confirmation email once a qr code is expired input it here (if not leave it blank) :: ")
print("******************************************************************************************")
#Adding a custom message
customMessage = input("If you would like to add a custom message to be displayed once a code is expired input it here (If not leave it blank; default is today's date) :: ")
print("******************************************************************************************")
colors =['blue', 'green', 'red', 'cyan', 'magenta', 'yellow', 'black', 'white']
#Add Foreground Color
while True:
foregroundColor = str(input("If you would like to add a custom color to your QR code foreground input it here (If not leave it blank; default is black) \n Colors(blue, green, red, cyan, magenta, yellow, black, white) \n or enter a custom color with a hex value (#000000):: "))
if foregroundColor == "":
foregroundColor = "black"
break
elif foregroundColor.lower() in colors or (foregroundColor[0] == "#" and foregroundColor.length() == 7):
break
else:
print(f"You entered {foregroundColor} which is invalid! \n Please select one of the available colors \n or make sure of your hex syntax ex.(#000000) ::")
print("******************************************************************************************")
#Add Background Color
while True:
backgroundColor = str(input("If you would like to add a custom color to your QR code background input it here (If not leave it blank; default is black) \n Colors(blue, green, red, cyan, magenta, yellow, black, white) \n or enter a custom color with a hex value (#000000):: "))
if backgroundColor == "":
backgroundColor = "white"
break
elif backgroundColor.lower() in colors or (backgroundColor[0] == "#" and backgroundColor.length() == 7):
break
else:
print(f"You entered {backgroundColor} which is invalid! \n Please select one of the available colors \n or make sure of your hex syntax ex.(#000000) ::")
print("******************************************************************************************")
#Generating Links For Qr Codes
linkInfo = (linksArr, linkPassword, expireyDate, confirmationEmail, customMessage)
getOneTimeLink(retriveLink, linkInfo, numOfLinks)
print(f" QR Code Details :: \n Amount of Links : {numOfLinks} \n Link Password : {linkPassword} \n Expirey Date : {expireyDate} \n Confirmation Email : {confirmationEmail} \n Custom Message : {customMessage}")
print("******************************************************************************************")
#Generate Qr Codes
qrFolder = createFolder()
createQRCodes(linksArr, imagePath, qrFolder, foregroundColor, backgroundColor)
except:
try:
os.path.exists(qrFolder)
except:
pass
else:
os.rmdir(qrFolder)
print("\n ********************** \n Oops there seems to be a problem :/ \n Please follow all instructions carefully and try again!")
else:
print(f"\n Congratulations your QR codes have been created !! \n You will find them in the '{qrFolder}' folder \n Good Bye :)")
main()
if main == __name__ :
main()