Repository navigation
Expand file tree
/
Copy pathPySN.py
More file actions
1340 lines (1166 loc) · 68.6 KB
/
Copy pathPySN.py
File metadata and controls
1340 lines (1166 loc) · 68.6 KB
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import customtkinter
import hmac
import json
import threading
import hashlib
import requests
import os
import queue
import yaml
import time
import sys
import re
import shutil
import xml.etree.ElementTree as ET
from tkinter import PhotoImage
from pathlib import Path
from fnmatch import fnmatch
from configparser import ConfigParser
from os import makedirs, path
from enum import Enum
from bs4 import BeautifulSoup
from concurrent.futures import ThreadPoolExecutor
#Urls are http rather than https, so we have to disable the warning about unsecure requests.
requests.packages.urllib3.disable_warnings()
#Creates a directory for the game in the download path.
def create_directories(download_path):
if not path.exists(download_path):
makedirs(download_path, exist_ok=True)
return download_path
def resource_path(relative_path):
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
return os.path.join(base_path, relative_path)
#Handles the config file.
class ConfigSettings():
def __init__(self):
super().__init__()
def get_path():
if getattr(sys, 'frozen', False):
if sys.platform.startswith('darwin'):
app_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(sys.executable))))
else:
app_path = os.path.dirname(sys.executable)
else:
app_path = os.path.dirname(os.path.abspath(__file__))
normalized_path = app_path.replace('\\','/')
if sys.platform.startswith('darwin'):
config_dir = Path.home() / 'Library' / 'Application Support' / 'PySN'
config_dir.mkdir(parents=True, exist_ok=True)
return normalized_path, str(config_dir)
elif sys.platform.startswith('linux'):
config_dir = Path.home() / '.config' / 'PySN'
config_dir.mkdir(parents=True, exist_ok=True)
return normalized_path, str(config_dir)
else:
return normalized_path, normalized_path
#Gets settings from the config file.
def get_config():
config = ConfigParser()
normalized_path, config_dir = ConfigSettings.get_path()
config_path = os.path.join(config_dir, 'config.ini')
config.read(config_path)
save_dir = config.get('paths', 'downloads')
rpcs3_dir = config.get('paths', 'RPCS3')
folder_format = config.get('formats', 'folder_format', fallback='id_name')
max_downloads = config.getint('settings', 'max_downloads', fallback=12)
combine_ps4 = config.getboolean('settings', 'combine_ps4', fallback=True)
return save_dir, rpcs3_dir, folder_format, max_downloads, combine_ps4
#Saves the config file.
def save_config(mode, save_dir , rpcs3_dir, folder_format='id_name', max_downloads=12, combine_ps4=True):
config = ConfigParser()
normalized_path, config_dir = ConfigSettings.get_path()
config_path = os.path.join(config_dir, 'config.ini')
with open(config_path, mode) as ini:
config.add_section('paths')
config.set('paths', 'downloads', str(save_dir))
config.set('paths', 'RPCS3', str(rpcs3_dir))
config.add_section('formats')
config.set('formats', 'folder_format', folder_format)
config.add_section('settings')
config.set('settings', 'max_downloads', str(max_downloads))
config.set('settings', 'combine_ps4', str(combine_ps4).lower())
config.write(ini)
#Checks for the config file, and gets the settings from it. Saves default config if none present.
def check_config():
normalized_path, config_dir = ConfigSettings.get_path()
config_path = os.path.join(config_dir, 'config.ini')
if os.path.exists(config_path):
save_dir, rpcs3_dir, folder_format, max_downloads, combine_ps4 = ConfigSettings.get_config()
else:
save_dir = (normalized_path + '/Updates/')
rpcs3_dir = 'No Games.yml Location Set!'
folder_format = 'id_name'
max_downloads = 12
combine_ps4 = True
ConfigSettings.save_config('x', save_dir , rpcs3_dir, folder_format, max_downloads, combine_ps4)
return save_dir, rpcs3_dir, folder_format, max_downloads, combine_ps4
save_dir, rpcs3_dir, folder_format, max_downloads, combine_ps4 = ConfigSettings.check_config()
class ResizableSemaphore:
def __init__(self, capacity):
self._cond = threading.Condition()
self._capacity = capacity
self._in_use = 0
def acquire(self, timeout=None):
with self._cond:
if timeout is None:
while self._in_use >= self._capacity:
self._cond.wait()
else:
if self._in_use >= self._capacity:
got = self._cond.wait_for(lambda: self._in_use < self._capacity, timeout=timeout)
if not got:
return False
self._in_use += 1
return True
def release(self):
with self._cond:
self._in_use -= 1
self._cond.notify()
def resize(self, new_capacity):
with self._cond:
self._capacity = new_capacity
self._cond.notify_all()
def __enter__(self):
self.acquire()
return self
def __exit__(self, *args):
self.release()
#Used to send messages to the queue
class ButtonAction(Enum):
STOP = 1
PAUSE = 2
RESUME = 3
#Window with save and rpcs3/games.yml locations.
class SettingsWindow(customtkinter.CTkToplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.attributes('-topmost', 1)
self.geometry('540x480')
self.resizable(0,0)
self.title('Settings')
self.grid_rowconfigure((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), weight=1)
self.grid_columnconfigure((0, 1, 2, 3, 4, 5), weight=1)
if sys.platform == 'win32':
self.after(200, lambda: self.iconbitmap(resource_path("AphIcon.ico")))
self.temp_save, self.temp_rpcs3, self.temp_folder_format, self.temp_max_downloads, self.temp_combine_ps4 = ConfigSettings.check_config()
self.char_limit = (self.register(self.validate_limit), '%P')
self.save_dir_label = customtkinter.CTkLabel(master=self, text='Download Update PKGs To This Folder:', justify = 'center', anchor='center')
self.save_dir_label.grid(row=0, column=1, columnspan=4, padx=5, pady=(20,0), sticky='sew')
self.save_dir_field = customtkinter.CTkTextbox(master=self, height=25, wrap='none')
self.save_dir_field.grid(row=1, column=1, columnspan=3, padx=5, pady=(0,0), sticky='new')
self.edit_button1 = customtkinter.CTkButton(master=self, width = 50, text='Edit', command = self.button_save_loc)
self.edit_button1.grid(row=1, padx=5, pady=(0,0), column=4, sticky='new')
self.rpcs3_dir_label = customtkinter.CTkLabel(master=self, text='Folder Containing RPCS3 (Windows Only):', anchor='center')
self.rpcs3_dir_label.grid(row=2, column=1, columnspan=4, padx=0, pady=(0,0), sticky='sew')
self.yaml_dir_field = customtkinter.CTkTextbox(master=self, height=25, width = 400, wrap='none')
self.yaml_dir_field.grid(row=3, column=1, columnspan=3, padx=5, pady=(0,0), sticky='new')
self.edit_button2 = customtkinter.CTkButton(master=self, width = 50, text='Edit', command = self.button_yml_loc)
self.edit_button2.grid(row=3, padx=5, pady=(0,0), column=4, sticky='new')
self.folder_format_label = customtkinter.CTkLabel(master=self, text='Folder Naming:', anchor='center')
self.folder_format_label.grid(row=4, column=1, columnspan=4, padx=0, pady=(0,0), sticky='sew')
self.folder_format_toggle = customtkinter.CTkSegmentedButton(master=self, values=['ID - Name', 'Name - ID'])
self.folder_format_toggle.grid(row=5, column=1, columnspan=4, padx=5, pady=(0,0), sticky='new')
self.folder_format_toggle.set('ID - Name' if folder_format == 'id_name' else 'Name - ID')
self.combine_ps4_label = customtkinter.CTkLabel(master=self, text='Combine PS4 Games:', anchor='center')
self.combine_ps4_label.grid(row=6, column=1, columnspan=4, padx=0, pady=(0,0), sticky='sew')
self.combine_ps4_toggle = customtkinter.CTkSegmentedButton(master=self, values=['Yes', 'No'])
self.combine_ps4_toggle.grid(row=7, column=1, columnspan=4, padx=5, pady=(0,0), sticky='new')
self.combine_ps4_toggle.set('Yes' if combine_ps4 else 'No')
self.max_downloads_label = customtkinter.CTkLabel(master=self, text='Maximum Simultaneous Downloads (1-100):', anchor='center')
self.max_downloads_label.grid(row=8, column=1, columnspan=4, padx=0, pady=(0,0), sticky='sew')
self.max_downloads_entry = customtkinter.CTkEntry(master=self, width=25, justify='center', validate='key', validatecommand=self.char_limit)
self.max_downloads_entry.grid(row=9, column=1, columnspan=4, padx=(230,230), pady=(0,30), sticky='new')
self.max_downloads_entry.insert(0, str(max_downloads))
self.save_button = customtkinter.CTkButton(master=self, text='Save', width = 100, command = self.button_save)
self.save_button.grid(row=10, padx=(0,5), column=2, sticky='e')
self.cancel_button = customtkinter.CTkButton(master=self, text='Cancel', width = 100, command=self.destroy)
self.cancel_button.grid(row=10, padx=(5,135), column=3, columnspan=2, sticky='w')
self.save_dir_field.insert('0.0',self.temp_save)
self.save_dir_field.configure(state='disabled')
self.yaml_dir_field.insert('0.0',self.temp_rpcs3)
self.yaml_dir_field.configure(state='disabled')
#Button to change update save location. Assigns the chosen path to a temporary variable and populates the text bar with it.
def button_save_loc(self):
temporary_temp = self.temp_save
self.temp_save=customtkinter.filedialog.askdirectory(parent = self)
if self.temp_save != '':
self.save_dir_field.configure(state='normal')
self.save_dir_field.delete('0.0','end')
self.save_dir_field.insert('0.0', self.temp_save)
self.save_dir_field.configure(state='disabled')
else: self.temp_save = temporary_temp
#Button to change rpcs3 location. Assigns the chosen path to a temporary variable and populates the text bar with it.
def button_yml_loc(self):
temporary_temp = self.temp_rpcs3
self.temp_rpcs3=customtkinter.filedialog.askdirectory(parent = self)
if self.temp_rpcs3 != '':
self.yaml_dir_field.configure(state='normal')
self.yaml_dir_field.delete('0.0','end')
self.yaml_dir_field.insert('0.0', self.temp_rpcs3)
self.yaml_dir_field.configure(state='disabled')
else: self.temp_rpcs3 = temporary_temp
#Save button. assigns the temp variables to global variables for the save directory.
#Some handling to endure the directory is appended with a /.
def button_save(self):
global rpcs3_dir
global save_dir
global folder_format
global max_downloads
global combine_ps4
dl_entry = int(self.max_downloads_entry.get() or 12)
if dl_entry < 1:
dl_entry = 1
elif dl_entry > 100:
dl_entry = 100
max_downloads = dl_entry
app = self.master
app.download_semaphore.resize(max_downloads)
if self.temp_save.endswith('/'):
save_dir = self.temp_save
else:
save_dir = self.temp_save + '/'
if self.temp_rpcs3.endswith('!') or self.temp_rpcs3.endswith('/'):
rpcs3_dir = self.temp_rpcs3
else:
rpcs3_dir = self.temp_rpcs3 + '/'
if self.folder_format_toggle.get() == 'ID - Name':
folder_format = 'id_name'
else:
folder_format = 'name_id'
if self.combine_ps4_toggle.get() == 'Yes':
combine_ps4 = True
else:
combine_ps4 = False
ConfigSettings.save_config('w', save_dir , rpcs3_dir, folder_format, max_downloads, combine_ps4)
self.destroy()
def validate_limit(self, text):
if len(text) <= 3:
return True
return False
#Download All window. I did not create the downall function as part of this class for whatever reason...
class DownloadAllWindow(customtkinter.CTkToplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry('400x240')
self.grid_rowconfigure((0, 1, 2, 3, 4), weight=1)
self.grid_columnconfigure((0, 1, 2, 3, 4, 5), weight=1)
if sys.platform == 'win32':
self.after(200, lambda: self.iconbitmap(resource_path("AphIcon.ico")))
self.save_dir_label = customtkinter.CTkLabel(master=self, text='You are about to download all of the updates in the list!\n\n\nContinue?\n', anchor='center')
self.save_dir_label.grid(row=1, column=2, columnspan=2, padx=20, pady=0, sticky='ew')
self.only_new_check = customtkinter.CTkCheckBox(master=self, text="Only Download Updates That I Don't Have")
self.only_new_check.grid(row=2, column=2, columnspan=2, padx=50, sticky='ew')
self.save_button = customtkinter.CTkButton(master=self, width = 100, text='Okay')
self.save_button.grid(row=4, padx=(55,5), column=2, sticky='e')
self.cancel_button = customtkinter.CTkButton(master=self, width = 100, text='Cancel', command=self.destroy)
self.cancel_button.grid(row=4, padx=(5,55), column=3, sticky='w')
#Frame where all the fun widgets go. This gets put inside the textbox.
class ScrollableLabelButtonFrame(customtkinter.CTkScrollableFrame):
def __init__(self, master, command=None, **kwargs):
super().__init__(master, **kwargs)
self.grid_columnconfigure((0, 1, 2, 3, 4, 5), weight=1)
self.grid_rowconfigure(999, weight=1)
self.command = command
self.title_label_list = []
self.size_label_list = []
self.status_list = []
self.queue_list = []
self.dlbutton_list = []
self.open_button_list = []
self.prog_bar_list = []
if sys.platform in ('win32', 'darwin'):
self.bind_all("<MouseWheel>", self.on_mousewheel)
#Changes scroll speed of the mouse wheel. Default is too slow.
def on_mousewheel(self, event):
delta = event.delta
if sys.platform == "darwin":
delta = delta * 120
self._parent_canvas.yview_scroll(int(-1*(delta)), "units")
#Creates widgets within the frame, adds appropriate ones to the grid, then adds them to a list.
def add_item(self, name, title_id, ver, url, console, update_size, sha1, index, download_path, fileloc, sanitized_name):
downloadable = False
try:
#Truncates the name depending on it's length. Assigns the title id, version, and name to a label on the left side of the frame.
#Firmware
if len(title_id) == 2 and sha1 == 'N/A':
title_label = customtkinter.CTkLabel(self, text= title_id + ver + ' - ' + name, anchor='w')
downloadable = True
#DRM-Free updates and Japanese titles
elif ((ver.startswith(' DRM-Free') and len(name)>9 and not name.startswith('Invalid ID') and not name.startswith('No updates available for') and not name.startswith('PlayStation 5 title') and name != 'No updates found') or
('\u3040' <= name[0] <= '\u30FF' or '\u4E00' <= name[0] <= '\u9FFF' or '\uFF65' <= name[0] <= '\uFF9F')):
title_label = customtkinter.CTkLabel(self, text= title_id + ver + ' - ' + name[:9] + '...', anchor='w')
downloadable = True
#Truncate regular updates with long names
elif len(name)>18 and not name.startswith('Invalid ID') and not name.startswith('No updates available for') and not name.startswith('PlayStation 5 title') and name != 'No updates found':
title_label = customtkinter.CTkLabel(self, text= title_id + ver + ' - ' + name[:18] + '...', anchor='w')
downloadable = True
#Regular updates with short names
elif len(name)<=18 and not name.startswith('Invalid ID') and not name.startswith('No updates available for') and not name.startswith('PlayStation 5 title') and name != 'No updates found':
title_label = customtkinter.CTkLabel(self, text= title_id + ver + ' - ' + name, anchor='w')
downloadable = True
#invalid IDs and titles with no updates available
else:
title_label = customtkinter.CTkLabel(self, text= title_id + ver + name, anchor='center')
#Creates labels for size and status, buttons for downloading and opening the file, a progress bar, and establishes the queue for threading.
size = str(round((update_size/1024000),2)) + ' MB'
size_label = customtkinter.CTkLabel(self, text=size, anchor='e', width = 70)
status = customtkinter.CTkLabel(self, text='', anchor='e', width = 160)
dlbutton = customtkinter.CTkButton(self, text='Download', width=100, height=24)
open_button = customtkinter.CTkButton(self, text='Open', width=100, height=24, state = 'disabled')
prog_bar = customtkinter.CTkProgressBar(self, height=5)
prog_bar.set(0)
q = queue.Queue()
#Configures the buttons to take a command with appropriate variables. Places all of the widgets on the grid.
if self.command is not None:
dlbutton.configure(command=lambda: self.command(sanitized_name, title_id, url, console, update_size, sha1, index, download_path, fileloc))
open_button.configure(command=lambda: self.command(download_path, index, fileloc))
if not name.startswith('Invalid ID') and not name.startswith('No updates available for') and not name.startswith('PlayStation 5 title') and name != 'No updates found':
title_label.grid(row=len(self.title_label_list), column=0, pady=(0, 10), sticky='w')
status.grid(row=len(self.title_label_list), column=2, pady=(0, 10),padx=(0, 0), sticky='e')
size_label.grid(row=len(self.title_label_list), column=3, padx=(10, 5), pady=(0, 10), sticky='e')
prog_bar.grid(row=len(self.dlbutton_list), column=0, columnspan=3, pady=(15, 0), sticky='ew')
dlbutton.grid(row=len(self.dlbutton_list), column=4, pady=(0, 10), padx=0, sticky='e')
open_button.grid(row=len(self.dlbutton_list), column=5, pady=(0, 10), padx=(0, 0), sticky='e')
else:
title_label.grid(row=len(self.title_label_list), column=0, columnspan=8, pady=(0, 10))
#Appends the list of widgets, so that we can refer to them specifically later.
if downloadable == True:
self.title_label_list.append(title_label)
self.size_label_list.append(size_label)
self.status_list.append(status)
self.queue_list.append(q)
self.dlbutton_list.append(dlbutton)
self.open_button_list.append(open_button)
self.prog_bar_list.append(prog_bar)
else:
self.title_label_list.append(None)
self.size_label_list.append(None)
self.status_list.append(None)
self.queue_list.append(None)
self.dlbutton_list.append(None)
self.open_button_list.append(None)
self.prog_bar_list.append(None)
except Exception:
return
#Iterates through the widgets looking for specific strings. If one is found, the whole line will be deleted.
def clear_items(self):
for title_label, size_label, status, dlbutton, open_button, prog_bar, q in zip(self.title_label_list, self.size_label_list, self.status_list, self.dlbutton_list, self.open_button_list, self.prog_bar_list, self.queue_list):
if title_label.cget('text').startswith('Invalid ID') or title_label.cget('text').startswith('No updates available for'):
title_label.destroy()
size_label.destroy()
status.destroy()
dlbutton.destroy()
open_button.destroy()
prog_bar.destroy()
self.title_label_list.remove(title_label)
self.size_label_list.remove(size_label)
self.status_list.remove(status)
self.queue_list.remove(q)
self.dlbutton_list.remove(dlbutton)
self.open_button_list.remove(open_button)
self.prog_bar_list.remove(prog_bar)
#Main window and some button functionality. Vaguely named widgets are in order of how they appear on screen.
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.running = True
self.protocol("WM_DELETE_WINDOW", self.on_closing)
self.geometry('760x640')
self.title('PySN')
self.resizable(0,1)
self.minsize(760, 0)
self.maxsize(760, 99999)
self.toplevel_window = None
self.grid_rowconfigure((0, 2, 3), weight=0)
self.grid_rowconfigure((1), weight=1)
self.grid_columnconfigure((0, 1, 2, 3, 4), weight=1)
self.bind('<Return>', lambda event: self.button_search())
if sys.platform == 'win32':
self.iconbitmap(resource_path("AphIcon.ico"))
elif sys.platform in ['linux', 'darwin']:
icon = PhotoImage(file=resource_path("AphIcon.png"))
self.after(0, lambda: self.iconphoto(True, icon))
self.hash_executor = ThreadPoolExecutor(max_workers=4)
self.download_semaphore = ResizableSemaphore(max_downloads)
self._search_thread = None
self.session = requests.Session()
self.total_download_size = 0
self.completed_download_size = 0
self._download_lock = threading.Lock()
self.entry = customtkinter.CTkEntry(master=self, placeholder_text='Enter Serial', width = 125)
self.entry.grid(row=0, column=0, padx=(4,2), pady=(6,0), sticky='ew')
self.combobox = customtkinter.CTkComboBox(master=self, values=['PlayStation 3', 'PlayStation 4', 'PlayStation Vita', 'PlayStation 5'], width = 125)
self.combobox.grid(row=0, column=1, columnspan=1, padx=(2,2), pady=(6,0), sticky='ew')
self.combobox.configure(state = 'readonly')
self.checkbox = customtkinter.CTkCheckBox(master=self, text='Search Games.yml')
self.checkbox.grid(row=0, column=2, columnspan=2, padx=(2,4), pady=(6,0), sticky='w')
self.button1 = customtkinter.CTkButton(master=self, command=self.button_search, text='Search', width = 125)
self.button1.grid(row=0, column=4, padx=4, pady=(6,0), sticky='ew')
self.textbox = ScrollableLabelButtonFrame(master=self, command=self.frame_button_download, corner_radius=5)
self.textbox.grid(row=1, column=0, columnspan=5, padx=4, pady=(5,5), sticky='nsew')
self.prog_label = customtkinter.CTkLabel(self, text= 'Total Progress:', anchor='w')
self.prog_label.grid(row=2, column=0, pady=(0, 25), padx=(15, 0), sticky='w')
self.prog_status_label = customtkinter.CTkLabel(self, text= '0/0 MB', anchor='e')
self.prog_status_label.grid(row=2, column=4, pady=(0, 25), padx=(0, 15), sticky='e')
self.total_prog_bar = customtkinter.CTkProgressBar(self, height=5)
self.total_prog_bar.set(0)
self.total_prog_bar.grid(row=2, column=0, columnspan=5, pady=(15, 15), padx=(15, 15), sticky='ew')
self.after(50, self.update_total_progress_ui)
self.button2 = customtkinter.CTkButton(master=self, command=self.button_downall, text='Download All', width = 125)
self.button2.grid(row=3, column=0, padx=(4,2), pady=(0,6), sticky='ew')
self.button3 = customtkinter.CTkButton(master=self, command=self.button_clear, text='Clear', width = 125)
self.button3.grid(row=3, column=1, padx=(2,2), pady=(0,6), sticky='ew')
self.clearbox = customtkinter.CTkCheckBox(master=self, text='Clear List On Search')
self.clearbox.grid(row=3, column=2, columnspan=2, padx=(2,4), pady=(0,6), sticky='w')
self.clearbox.select()
self.button4 = customtkinter.CTkButton(master=self, command=self.button_settings, text='Settings', width = 125)
self.button4.grid(row=3, column=4, padx=4, pady=(0,6), sticky='ew')
#Handle closing the window while threads are running.
def on_closing(self):
self.running = False
self.session.close()
try:
self.hash_executor.shutdown(wait=False)
except Exception:
pass
self.wait_for_thread()
#Waits for thread to finish what it's doing before closing the window.
def wait_for_thread(self):
if self._search_thread is not None and self._search_thread.is_alive():
self.after(100, self.wait_for_thread)
else:
try:
self.destroy()
except Exception:
pass
#Checks the hash of the file in a separate thread to avoid freezing the UI.
def hash_check_worker(self, index, fileloc, console, sha1, piece=None):
try:
hash_obj = hashlib.sha1()
with open(fileloc, 'rb') as f:
if console in ('PlayStation 3', 'PlayStation Vita'):
filesize = os.path.getsize(fileloc)
remaining = max(0, filesize - 32)
while remaining > 0:
chunk = f.read(min(4 * 1024 * 1024, remaining))
if not chunk:
break
hash_obj.update(chunk)
remaining -= len(chunk)
else:
for chunk in iter(lambda: f.read(4 * 1024 * 1024), b''):
hash_obj.update(chunk)
if piece is not None:
match = (piece['hash'].upper() == hash_obj.hexdigest().upper())
else:
match = (sha1.upper() == hash_obj.hexdigest().upper())
except Exception:
match = False
#Updates the UI based on the hash check result.
def update_ui():
try:
self.textbox.dlbutton_list[index].configure(state='normal')
self.textbox.open_button_list[index].configure(state='normal')
if match:
self.textbox.status_list[index].configure(text_color='green', text='Already Owned!')
else:
self.textbox.status_list[index].configure(text_color='red', text='HASH MISMATCH DETECTED!')
except Exception:
pass
self.after(0, update_ui)
#Update the total progress bar and label.
def update_total_progress_ui(self):
with self._download_lock:
total_bytes = self.total_download_size
completed_bytes = self.completed_download_size
if total_bytes > 0:
progress = completed_bytes / total_bytes
else:
progress = 0
total_mb = total_bytes / 1024000
completed_mb = completed_bytes / 1024000
self.total_prog_bar.set(progress)
self.prog_status_label.configure(text=f"{completed_mb:.2f}/{total_mb:.2f}MB")
self.after(50, self.update_total_progress_ui)
#Checks if the download path already exists and if hashes match. Changes the buttons and status label accordingly.
def is_shit_there(self, name, title_id, download_path, index, fileloc, console, sha1, expected_size=None, url=None):
if not path.exists(fileloc) and not path.exists(fileloc.replace('.pkg', '_0.pkg')):
if title_id and name:
if folder_format == 'id_name':
alt_download_path = save_dir + console + '/' + name + ' [' + title_id + ']'
else:
alt_download_path = save_dir + console + '/[' + title_id + '] ' + name
alt_fileloc = alt_download_path + '/' + path.basename(fileloc)
legacy_path = save_dir + console + '/' + title_id + ' ' + name
legacy_fileloc = legacy_path + '/' + path.basename(fileloc)
if path.exists(alt_fileloc):
download_path = alt_download_path
fileloc = alt_fileloc
elif path.exists(legacy_fileloc):
download_path = legacy_path
fileloc = legacy_fileloc
else:
return 0
else:
return 0
self.textbox.status_list[index].configure(text_color='yellow',text='Checking Hash...')
self.textbox.dlbutton_list[index].configure(text='Redownload', state='disabled')
self.textbox.open_button_list[index].configure(
text='Open',
state='disabled',
command=lambda: self.open_loc(download_path)
)
self.textbox.prog_bar_list[index].set(1)
if url and not combine_ps4:
sizeloc = fileloc.replace('.pkg', '_0.pkg')
for n, piece in enumerate(url):
if n > 0:
sizeloc = fileloc.replace('.pkg', f'_{n}.pkg')
if not path.exists(sizeloc):
self.textbox.status_list[index].configure(text_color='red', text='Missing Split File!')
self.textbox.dlbutton_list[index].configure(state='normal')
self.textbox.open_button_list[index].configure(state='normal')
return 1
else:
actual_size = os.path.getsize(sizeloc)
expected_size = piece['size']
if actual_size != expected_size:
self.textbox.status_list[index].configure(text_color='red', text='Size Mismatch!')
self.textbox.dlbutton_list[index].configure(state='normal')
self.textbox.open_button_list[index].configure(state='normal')
return 1
else:
actual_size = os.path.getsize(fileloc)
if actual_size != expected_size:
self.textbox.status_list[index].configure(text_color='red', text='Size Mismatch!')
self.textbox.dlbutton_list[index].configure(state='normal')
self.textbox.open_button_list[index].configure(state='normal')
return 1
if url and not combine_ps4:
location = fileloc.replace('.pkg', '_0.pkg')
for n, piece in enumerate(url):
if n > 0:
location = fileloc.replace('.pkg', f'_{n}.pkg')
self.hash_executor.submit(self.hash_check_worker, index, location, console, sha1, piece)
elif sha1 in ('N/A', 'PS4_SPLIT', 'PS4_DELTA'):
self.textbox.status_list[index].configure(text_color='green', text='Already Owned!')
self.textbox.dlbutton_list[index].configure(state='normal')
self.textbox.open_button_list[index].configure(state='normal')
return 1
else:
self.hash_executor.submit(self.hash_check_worker, index, fileloc, console, sha1, None)
return 1
#Opens the file location. Used with the open button.
def open_loc(self, download_path):
customtkinter.filedialog.askopenfilenames(initialdir=download_path)
def search_ps3_ps4_vita(self, title_id, console):
root, name = self.request_update(title_id, console)
self.search(title_id, console, root, name)
if console == 'PlayStation 3':
self.search_no_drm(title_id, console, root, name)
#If the "search games.yml" box is not checked, assign the title id and console based on the users input, the search. If the console is PS3, search for DRM-free updates too.
#If the "search games.yml" box is checked, automatically assign the console to PS3, and search for each title id on the games.yml.
def search_type(self):
if self.checkbox.get() == 0:
title_id = (self.entry.get()).upper()
console = self.combobox.get()
if title_id.upper() == 'FIRMWARE' or title_id.upper() == 'FW':
if console == 'PlayStation 3':
self.search_ps3_fw(console)
else: self.search_ps4_ps5_vita_fw(console)
elif console == 'PlayStation 5':
self.search_ps5_update(title_id)
else:
self.search_ps3_ps4_vita(title_id, console)
else:
console = 'PlayStation 3'
if sys.platform.startswith('linux'):
yml_dir = Path.home() / '.config' / 'rpcs3' / 'games.yml'
elif sys.platform.startswith('darwin'):
yml_dir = Path.home() / 'Library' / 'Application Support' / 'rpcs3' / 'games.yml'
else: yml_dir = Path(rpcs3_dir) / 'config' / 'games.yml'
if os.path.isfile(yml_dir):
with open(yml_dir, 'r') as f:
file = yaml.safe_load(f)
alist = list(file)
with ThreadPoolExecutor(max_workers=6) as executor:
for index in alist:
if not self.running:
break
executor.submit(self.search_ps3_ps4_vita, index, console)
self.after(0, lambda: (
self.button1.configure(state='normal'),
self.button2.configure(state='normal'),
self.button3.configure(state='normal'),
self.button4.configure(state='normal')
))
#Assigns hashes and urls based on console and title id, checks if the url is valid, returns the xml and game name.
def request_update(self, title_id, console):
root = 0
id = bytes('np_' + title_id, 'UTF-8')
if console == 'PlayStation Vita':
key = bytearray.fromhex('E5E278AA1EE34082A088279C83F9BBC806821C52F2AB5D2B4ABD995450355114')
hash = (hmac.new(key, id, hashlib.sha256).hexdigest())
xml_url = ('https://gs-sec.ww.np.dl.playstation.net/pl/np/' + title_id + '/' + hash + '/' + title_id + '-ver.xml')
elif console == 'PlayStation 4':
key = bytearray.fromhex('AD62E37F905E06BC19593142281C112CEC0E7EC3E97EFDCAEFCDBAAFA6378D84')
hash = (hmac.new(key, id, hashlib.sha256).hexdigest())
xml_url = ('https://gs-sec.ww.np.dl.playstation.net/plo/np/' + title_id + '/' + hash + '/' + title_id + '-ver.xml')
else:
xml_url = 'https://a0.ww.np.dl.playstation.net/tpl/np/' + title_id + '/' + title_id + '-ver.xml'
try:
var_url = self.session.get(xml_url, stream=True, verify=False, timeout=10)
except Exception:
return 0, 'Invalid ID'
if var_url.status_code == 200 and var_url.text != '':
root = ET.fromstring(var_url.content)
name = (root.find('./tag/package/paramsfo/').text).replace('\n', ' ')
for item in root.iter('titlepatch'):
title_id = item.get('titleid')
elif var_url.status_code == 200 and var_url.text == '':
name = 'No updates available for '
else:
name = 'Invalid ID'
return root, name
#Checks if the url is valid, returns a list of xml and txt contents.
def request_fw(self, console):
url_list = []
if console == 'PlayStation Vita':
locale_list = ['us', 'eu', 'jp', 'kr', 'uk', 'mx', 'au', 'sa', 'tw', 'ru', 'cn']
elif console == 'PlayStation 3':
locale_list = ['us', 'eu', 'jp', 'kr', 'uk', 'mx', 'au', 'sa', 'tw', 'ru', 'br']
else:
locale_list = ['us', 'eu', 'jp', 'kr', 'uk', 'mx', 'au', 'sa', 'tw', 'ru', 'cn', 'br']
for locale in locale_list:
if console == 'PlayStation Vita':
info_url = 'https://f' + locale + '01.psp2.update.playstation.net/update/psp2/list/' + locale + '/psp2-updatelist.xml'
elif console == 'PlayStation 4':
info_url = 'https://f' + locale + '01.ps4.update.playstation.net/update/ps4/list/' + locale + '/ps4-updatelist.xml'
elif console == 'PlayStation 5':
obf = 'tJMRE80IbXnE9YuG0jzTXgKEjIMoabr6'
info_url = 'http://f' + locale + '01.ps5.update.playstation.net/update/ps5/official/'+ obf + '/list/'+ locale + '/updatelist.xml'
else:
info_url = 'https://f' + locale + '01.ps3.update.playstation.net/update/ps3/list/' + locale + '/ps3-updatelist.txt'
url_list.append(info_url)
def fetch_locale(info_url):
try:
var_url = self.session.get(info_url, stream=True, verify=False, timeout=10)
if var_url.status_code == 200 and var_url.text != '':
if console == 'PlayStation 3':
return BeautifulSoup(var_url.text, 'html.parser')
else:
return ET.fromstring(var_url.content)
except Exception:
return None
with ThreadPoolExecutor(max_workers=6) as executor:
results = list(executor.map(fetch_locale, url_list))
return [r for r in results if r is not None]
#Requests game/update info and creates widgets in the frame based on that info.
def search(self, title_id, console, root, game_name):
if not self.running:
return
#If the xml exists, iterate through the package element to get info about the update. If it's a PS4 title, load and iterate through the JSON for data.
if root != 0:
for item in root.iter('package'):
ver = (item.get('version'))
if console == 'PlayStation 4':
man_url = (item.get('manifest_url'))
json_url = self.session.get(man_url, stream=True, verify=False, timeout=10)
json_cont = json.loads(json_url.content)
pieces = [
{'url': p.get('url'), 'offset': int(p.get('fileOffset')),
'size': int(p.get('fileSize')), 'hash': p.get('hashValue')}
for p in json_cont['pieces']
]
pieces.sort(key=lambda p: p['offset'])
url = pieces
sha1 = 'PS4_SPLIT'
update_size = int(json_cont.get('originalFileSize', sum(p['size'] for p in pieces)))
delta_elem = item.find('delta_info_set')
if delta_elem is not None:
delta_url = delta_elem.get('url')
else:
delta_url = None
delta_size = 0
if delta_url:
delta_size = int(delta_elem.get('size', 0) or 0)
if delta_size == 0:
try:
delta_head = self.session.get(delta_url, stream=True, verify=False, timeout=10)
delta_size = int(delta_head.headers.get('Content-Length', 0))
except Exception:
delta_size = 0
else:
url = (item.get('url'))
sha1 = (item.get('sha1sum'))
update_size = int((item.get('size')))
delta_url = None
#Assign a download path with some handling for odd characters in the game name. Add Widgets to the textbox and check if the file already exists.
name = re.sub(r'[^a-zA-Z0-9_\- \u3000-\u303F\u3040-\u30FF\u4E00-\u9FFF\uFF00-\uFFEF]', '', game_name).strip()
name = re.sub(r'[\s.]+$', '', name)
if folder_format == 'id_name':
download_path = save_dir + console + '/[' + title_id + '] ' + name
else:
download_path = save_dir + console + '/' + name + ' [' + title_id + ']'
if console == 'PlayStation 4':
first_piece_name = path.basename(url[0]['url']).replace('_0.pkg', '.pkg')
update_file = re.sub(r'_0(\.pkg)$', r'\1', first_piece_name)
else:
update_file = path.basename(url)
fileloc = (download_path + '/' + update_file)
if console == 'PlayStation 4':
if delta_url:
delta_fileloc = (download_path + '/' + path.basename(delta_url))
else:
delta_fileloc = None
def add_ps4_items(gn=game_name, tid=title_id, v=ver, u=url, c=console, us=update_size, s=sha1,
dp=download_path, fl=fileloc, n=name,
d_url=delta_url, d_size=delta_size, d_fl=delta_fileloc):
if d_url:
d_idx = len(self.textbox.dlbutton_list)
self.textbox.add_item(gn, tid, ' Delta Patch', d_url, c, d_size, 'PS4_DELTA', d_idx, dp, d_fl, n)
self.is_shit_there(n, tid, dp, d_idx, d_fl, c, 'PS4_DELTA', d_size, None)
tu_idx = len(self.textbox.dlbutton_list)
self.textbox.add_item(gn, tid, ' v' + v, u, c, us, s, tu_idx, dp, fl, n)
self.is_shit_there(n, tid, dp, tu_idx, fl, c, s, us, u)
self.after(0, add_ps4_items)
else:
self.after(0, lambda n=name, gn=game_name, tid=title_id, v=ver, u=url, c=console, us=update_size, s=sha1, dp=download_path, fl=fileloc:
(self.textbox.add_item(gn, tid, ' v' + v, u, c, us, s, len(self.textbox.dlbutton_list), dp, fl, n),
self.is_shit_there(n, tid, dp, len(self.textbox.dlbutton_list) - 1, fl, c, s, us, None)))
elif game_name == 'Invalid ID':
self.after(0, lambda: self.textbox.add_item('Invalid ID: ' + title_id, '', '', '', '', 0, '', '', '', '', ''))
else:
self.after(0, lambda gn=game_name, tid=title_id: self.textbox.add_item(gn + tid, '', '', '', '', 0, '', '', '', '', ''))
#Searches specifically for PS3 DRM-free update info, and populates the widgets in the frame based on that info.
def search_no_drm(self, title_id, console, root, game_name):
if not self.running:
return
#If the XML exists, make sure that there is a URL element before continuing.
if root != 0:
drm_free_check = False
element_list = root.findall('.//')
if fnmatch(str(element_list),'*url*') == True:
drm_free_check = True
#If a URL element is found, create a list for the elements.
if drm_free_check == True:
i=0
package_list = []
drmfree_list = []
sha1_list = []
update_size_list = []
game_name_list = []
name_list = []
url_list = []
#Append lists with version info from the package element, and update info from the URL element.
#Then iterate through the lists for the download path and widget info. Also places widgets in the textbox.
for item in root.iter('package'):
package_list.append(item.get('version'))
for item in root.iter('url'):
drmfree_list.append(root.get('url'))
url_list.append(item.get('url'))
sha1_list.append(item.get('sha1sum'))
update_size_list.append(int((item.get('size'))))
game_name_list.append(game_name)
name_list = game_name_list.copy()
for name in name_list:
name = re.sub(r'[^a-zA-Z0-9_\- \u3000-\u303F\u3040-\u30FF\u4E00-\u9FFF\uFF00-\uFFEF]', '', name).strip()
name = re.sub(r'[\s.]+$', '', name)
for version in package_list:
if folder_format == 'id_name':
download_path = (save_dir + console + '/[' + title_id + '] ' + name_list[i])
else:
download_path = (save_dir + console + '/' + name_list[i] + ' [' + title_id + ']')
update_file = 'DRM-Free ' + path.basename(url_list[i])
fileloc = (download_path + '/' + update_file)
self.after(0, lambda n=name_list[i], gn=game_name_list[i], tid=title_id, v=version, u=url_list[i], c=console, us=update_size_list[i], s=sha1_list[i], dp=download_path, fl=fileloc:
(self.textbox.add_item(gn, tid, ' v' + v + ' DRM-Free', u, c, us, s, len(self.textbox.dlbutton_list), dp, fl, n),
self.is_shit_there(n, tid, dp, len(self.textbox.dlbutton_list) - 1, fl, c, s, us, None)))
i = i+1
else: pass
else: pass
#Handles users searching for PS5 updates.
def search_ps5_update(self, title_id):
name = 'PlayStation 5 title updates are not supported yet.'
self.after(0, lambda: self.textbox.add_item(name, '', '', '', '', 0, '', '', '', '', ''))
#Searches for PS3 firmware info, and populates the widgets in the frame based on that info.
def search_ps3_fw(self, console):
root_list = self.request_fw(console)
#Split the text in the text file by ; and pull strings that match certain criteria.
if root_list:
for root in root_list:
text = root.string.split(';')
for item in text:
if fnmatch(str(item),'*CompatibleSystemSoftwareVersion*') == True:
ver = item[32:36]
if fnmatch(str(item),'*# *') == True:
region = item[2:4].upper()
if region == 'SO':
region = 'SA'
if fnmatch(str(item),'*UPDAT.PUP') == True:
url = item[4:]
update_url = self.session.get(url, stream=True, verify=False, timeout=10)
update_size = int(update_url.headers.get('Content-Length', 0))
#There's no hash available to check, so sha1 gets assigned N/A. Title ID becomes region for formatting. set paths and populate widgets.
sha1 = 'N/A'
title_id = region
game_name = console + ' Firmware'
download_path = save_dir + console + '/' + game_name + '/' + region
update_file = 'v' + ver + ' ' + path.basename(url)
fileloc = (download_path + '/' + update_file)
self.after(0, lambda n=game_name, gn=game_name, tid=title_id, v=ver, u=url, c=console, us=update_size, s=sha1, dp=download_path, fl=fileloc:
(self.textbox.add_item(gn, tid, ' v' + v, u, c, us, s, len(self.textbox.dlbutton_list), dp, fl, n),
self.is_shit_there(gn, tid, dp, len(self.textbox.dlbutton_list) - 1, fl, c, s, us, None)))
else: self.after(0, lambda: self.textbox.add_item('Error Connecting to Server', '', '', '', '', 0, '', '', '', '', ''))
#Searches for PS4 or Vita update info and populates the widgets in the frame based on that info.
def search_ps4_ps5_vita_fw(self, console):
root_list = self.request_fw(console)
update_size_list = []
url_list = []
ver_list = []
region_list = []
update_data_list = []
i=0
#Parse the elements of the XML and get their attributes. Also pull the loose text in the xml (this contains the download URLs).
if root_list:
for root in root_list:
for item in root.iter('image'):
update_size_list.append(int(item.get('size')))
url_list.append((''.join(item.itertext())[:-8].strip()))
for item in root.iter('region'):
region_list.append(item.get('id').upper())
if console == 'PlayStation Vita':
for item in root.iter('version'):
ver_list.append(item.get('label'))
for item in root.iter('update_data'):
update_data_list.append(item.get('update_type'))
for item in root.iter('recovery'):
update_data_list.append(item.get('spkg_type'))
elif console == 'PlayStation 5':
for item in root.iter('system_pup'):
full_label = item.get('label')
auto_ver = item.get('auto_update_version', '00.00')
try:
parts = full_label.split('-')
yy_ss = parts[0]
version_main = parts[1]
v_parts = version_main.split('.')
major = v_parts[0]
minor = v_parts[1]
nn = "00" if auto_ver == "00.00" else "01"
short_ver = f"{yy_ss}-{major}.{minor}.{nn}"
ver_list.append(short_ver)
except:
ver_list.append(full_label)
for item in root.iter('update_data'):
update_data_list.append(item.get('update_type'))
for item in root.iter('recovery_pup'):
update_data_list.append(item.get('type'))
else:
for item in root.iter('system_pup'):
ver_list.append(item.get('label'))
for item in root.iter('update_data'):
update_data_list.append(item.get('update_type'))
for item in root.iter('recovery_pup'):
update_data_list.append(item.get('type'))
#Do some renaming so we know what we're actually looking at in the UI.
for url in url_list:
if update_data_list[i] == 'full':
update_data_list[i] = 'Firmware'
elif update_data_list[i] == 'systemdata':
update_data_list[i] = 'Fonts'
elif update_data_list[i] == 'preinst':
update_data_list[i] = 'Preinst'
elif update_data_list[i] == 'default':
update_data_list[i] = 'Recovery'
#Assign hash to N/A, assign title_id to region and make the game name look nice.