-
Notifications
You must be signed in to change notification settings - Fork 0
/
Google Lighthouse Helper.pyw
469 lines (303 loc) · 13 KB
/
Google Lighthouse Helper.pyw
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#Version 0.8.5
try:
from tkinter import *
from tkinter import ttk
from tkinter.ttk import *
from tkinter import filedialog
from tkinter import messagebox
import configparser
import threading
from multiprocessing import Process
import os
except:
from Tkinter import *
from Tkinter import filedialog
from Tkinter import messagebox
import threading
import configparser
import os
### Variables ###
CWD = os.getcwd()
keepfiles = 2
filenumber = 0
instantkill = False
config = configparser.ConfigParser()
CheckIn = False
CheckOut = False
config.read("config.ini")
has_started = False
OldCheck = 2
num_lines = 0
### Design Vars ###
#####################################################################
#####################################################################
### Defs ###
def CheckInOut():
global CheckIn
global CheckOut
if CheckIn and CheckOut == True and throttling_menu.current() != -1:
Start_Ligthouse.config(state= NORMAL)
elif CheckIn and CheckOut == False:
if has_started == False:
Start_Ligthouse.config(state= DISABLED)
else:
Start_Ligthouse.config(state= NORMAL)
root.after(100, CheckInOut)
def remember_location():
global OldCheck
global config
if OldCheck != RememberLocationVar.get() or config["LIGHTHOUSE"]["output_path"] != reportlocation:
if RememberLocationVar.get() == 1:
config.set("LIGHTHOUSE", "output_path", "{}".format(reportlocation))
with open('config.ini', 'w') as configfile:
config.write(configfile)
OldCheck = 1
elif OldCheck == 1 and RememberLocationVar.get() == 0:
config.set("LIGHTHOUSE", "output_path", "{}".format("no path"))
with open('config.ini', 'w') as configfile:
config.write(configfile)
OldCheck = 0
root.after(100,remember_location)
def file_open(): # Function to call the Filedialog to then set the Var. for Lighthouse (DOESNT WORK FFS)
global text
global file
global CheckIn
global textEdit
global linkfile
global num_lines
linkfile = filedialog.askopenfile(initialdir = CWD,title = "Links.txt auswaehlen", filetypes=(("Textfile","*.txt"),("Alle Dateien","*.*")))
if linkfile is None: # and textEdit == 0:
CheckIn = False
return
elif linkfile is not None:
file = open(linkfile.name, mode="r")
num_lines = sum(1 for line in file)
file = open(linkfile.name, mode="r")
CheckIn = True
links = linkfile.read()
text.delete(0.0,END)
text.insert(END,links)
def start_lighthouse(): # Function to send google lighthouse command to cmd (works but doesnt get the right input)
global filenumber
global Keepfilesvar
global linkfile
global reportlocation
global instantkill
global file
global CheckIn
global CheckOut
global text
global DevEmuStr
global throttlingVar
global presetVar
LighthouseSettings()
CheckIn = False
CheckOut = False
Quit_All.config(state= NORMAL)
instantkill = False
Start_Ligthouse.config(state= DISABLED)
for url in file:
if instantkill == True:
file = open(linkfile.name, mode="r")
CheckInOut()
break
url = url.rstrip("\n")
print(url)
import time
time.sleep(0.1)
filename = url.replace("https","").replace("/","-").replace("\n","").replace(":","").replace("--","")
if os.path.isfile(reportlocation + "/" + filename + ".html"):
if Keepfilesvar.get() == 1:
print("File exists, will be kept!")
elif Keepfilesvar.get() == 0:
print("File exists, will be replaced!")
filenumber = 2
while Keepfilesvar.get() == 1:
newfilename = filename + "{}".format(filenumber)
if not os.path.isfile(reportlocation + "/" + newfilename + ".html"):
filename = newfilename
break
filenumber += 1
os.system("lighthouse --quiet {} {} {} {} --output-path={}/{}.html ".format(DevEmuStr,throttlingVar,presetVar,url,reportlocation,filename))
has_started = True
file = open(linkfile.name, mode="r")
Start_Ligthouse.config(state=NORMAL)
if instantkill == True:
status.config(state=NORMAL)
status.delete(0.0,END)
status.config(state=DISABLED)
print("################################")
print("You can now safely exit the tool!")
print("################################")
Quit_All.config(state= DISABLED)
def quit_all(): # Explains itself
global instantkill
instantkill = True
print("################################")
print("Stopping, please don't quit yet!")
print("################################")
def report_location():
global CheckOut
global reportlocation
reportlocation = filedialog.askdirectory()
if len(reportlocation) > 0:
print("Reportlocation was set to " + reportlocation)
Remember_Location.config(state=NORMAL)
root.after(100,remember_location)
CheckOut = True
else:
return
def create_thread():
lighthouse_thread = threading.Thread(target=start_lighthouse)
lighthouse_thread.start()
def print(message):
status.config(state=NORMAL)
status.insert(END, message + "\n")
status.config(state=DISABLED)
status.see(END)
#####################################################################
### Window ###
root = Tk()
root.withdraw()
root.geometry("990x340")
root.config(background="white")
root.title("SEO Helper")
root.resizable(width=False, height=False)
root.grid_propagate(False)
#####################################################################
### Frames ###
entry_frame=Frame(root)
entry_frame.config(width=415, height=340)
entry_frame.grid(in_=root, row=1, column=1)
entry_frame.grid_propagate(False)
settings=Frame(root)
settings.config(width=575, height=340)
settings.grid(in_=root, row=1, column=2)
settings.grid_propagate(False)
lighthouse_frame=LabelFrame(settings, text="Google Lighthouse Settings")
lighthouse_frame.config(width=400, height=260)
lighthouse_frame.grid(in_=settings, row = 1, column = 1)
lighthouse_frame.grid_propagate(False)
right_frame=Frame(settings)
right_frame.config(width=175)
right_frame.grid(in_=settings, row = 1, column = 2)
file_options_frame=ttk.LabelFrame(right_frame, text="File Options")
file_options_frame.config(width=160, height=200)
file_options_frame.grid(in_=right_frame, row = 1, column = 2)
file_options_frame.grid_propagate(False)
#####################################################################
### Widgets ###
text=Text(entry_frame)
text.config(wrap="none", width=50, height=21, background="gray64", foreground="black")
text.grid(in_=entry_frame, row = 1, column = 1)
#root.after(100, InputCheck)
status=Text(settings)
status.config(wrap="word", width=50, height=4, background="gray64", foreground="black", state = DISABLED)
status.grid(in_=settings, row = 2, column = 1, sticky = W, padx = 2, pady = 5)
status.see(END)
status_text=Label(settings, text="Status", foreground="black")
status_text.grid(in_=settings, row=2, column=1, sticky = N+E)
#####################################################################
###Buttons###
OpenLink = Button(entry_frame, text="Select Linkfile", command=file_open)
OpenLink.grid(in_=entry_frame, row = 1, column = 1, sticky = S+E)
ReportLocation = Button(file_options_frame, text="Select Savelocation", command=report_location)
ReportLocation.grid(in_=file_options_frame, row = 1, column = 1, sticky = W, padx = 5)
Start_Ligthouse = Button(right_frame, text="Start", command=create_thread, width = 19)
Start_Ligthouse.grid(in_=right_frame, row = 2, column = 2)
Start_Ligthouse.config(state=DISABLED)
root.after(100, CheckInOut)
Quit_All = Button(right_frame, text="Stop", command=quit_all, width = 19)
Quit_All.grid(in_=right_frame, row = 3, column = 2)
Quit_All.config(state= DISABLED)
#####################################################################
### Google Lighthouse Vars ###
Keepfilesvar = IntVar()
RememberLocationVar = IntVar()
DeviceEmulationVar = IntVar()
#####################################################################
def LighthouseSettings():
global DeviceEmulationVar
global DevEmuStr
global throttlingVar
global presetVar
if DeviceEmulationVar.get() == 0:
DevEmuStr = "--emulated-form-factor=desktop"
elif DeviceEmulationVar.get() == 1:
DevEmuStr = "--emulated-form-factor=mobile"
if throttling_menu.current() == 0:
throttlingVar = "--throttling-method=provided"
elif throttling_menu.current() == 1:
throttlingVar = "--throttling-method=devtools"
elif throttling_menu.current() == 2:
throttlingVar = "--throttling-method=simulate"
if preset_menu.current() == 0:
presetVar = "--preset=full"
elif preset_menu.current() == 1:
presetVar = "--preset=perf"
### Google Lighthouse Settings ### # Everything for Google Lighthouse
Keepfiles_Check = Checkbutton(settings, text="Keep duplicate files", variable=Keepfilesvar)
Keepfiles_Check.grid(in_=file_options_frame, row = 3, column = 1, sticky = W, padx = 5)
Remember_Location = Checkbutton(settings, text="Remember Location?", variable=RememberLocationVar)
Remember_Location.grid(in_=file_options_frame, row = 2, column = 1, sticky = W, padx = 5)
Remember_Location.config(state=DISABLED)
Device_Emulation = Checkbutton(lighthouse_frame, text=" Enable Device Emulation?", variable=DeviceEmulationVar)
Device_Emulation.grid(in_=lighthouse_frame, row = 1, column = 1, sticky = W, padx = (10,25), pady = 5)
throttling_label=Label(lighthouse_frame, text="Throttling Preset:", foreground="black")
throttling_label.grid(in_=lighthouse_frame, row = 0, column = 2, sticky=W)
throttling_menu = Combobox(lighthouse_frame, state="readonly", values=["No Throttling","Slow 4G","Fast 4G"])
throttling_menu.grid(in_=lighthouse_frame, row = 1, column = 2)
preset_label=Label(lighthouse_frame, text="Audit Preset:", foreground="black")
preset_label.grid(in_=lighthouse_frame, row = 2, column = 2, sticky=W)
preset_menu = Combobox(lighthouse_frame, state="readonly", values=["Full","Performance"])
preset_menu.grid(in_=lighthouse_frame, row = 3, column = 2)
#adv_seperator=Separator(lighthouse_frame, orient="horizontal")
#adv_seperator.grid(row=4, column=1, sticky="we")
#####################################################################
### First run check ###
while True:
if config["DEFAULT"].getboolean("FirstRun") == True:
config.set("DEFAULT", "FirstRun", "False")
with open('config.ini', 'w') as configfile:
config.write(configfile)
try:
os.system("npm -v")
try:
os.system("npm show lighthouse version")
root.deiconify()
break
except OSError:
answer = messagebox.askyesno("Warning!","It seems like you don't have the Google lighthouse Package installed. Should the program install it for you?")
if answer == True:
os.system("npm install -g lighthouse")
root.deiconify()
elif answer == False:
messagebox.showwarning("Warning","This tool won't work unless the Google Lighthouse Package is installed, please install it yourself!")
print("quit")
root.deiconify()
quit_all()
break
except OSError:
messagebox.showwarning("Warning","It seems like you don't have NPM installed. Please install it and restart the Program!")
SystemExit(0)
elif config["DEFAULT"].getboolean("FirstRun") == False:
root.deiconify()
break
# Check for saved Path #
if config["LIGHTHOUSE"]["output_path"] != "no path":
RememberLocationVar.set(1)
OldCheck = 1
reportlocation = config["LIGHTHOUSE"]["output_path"]
print("Reportlocation was set to: "+reportlocation)
print("Please select the Linkfile and Throttling Method")
CheckOut = True
Remember_Location.config(state=NORMAL)
else:
RememberLocationVar.set(0)
OldCheck = 0
print("No saved report location was found")
print("Please select the Linkfile and Throttling Method and select a Savelocation")
CheckOut = False
Remember_Location.config(state=DISABLED)
#####################################################################
root.mainloop()