11from os .path import expanduser
2- from os import path , listdir
2+ from os import path , listdir , sep
33from time import sleep
44from platform import system
55from shutil import unpack_archive
@@ -24,62 +24,50 @@ def get_download_path():
2424
2525downloads_path = get_download_path ()
2626
27+ def extractor (file ):
28+ if ".crdownload" in file :
29+ return
30+ formatted_file = file .split (sep )[- 1 ].split ("." )[0 ]
31+ destination_name = f"Extracted { formatted_file } "
32+ destination = downloads_path + sep + destination_name
33+ sleep (0.5 )
34+ try :
35+ unpack_archive (
36+ file ,
37+ destination )
38+
39+ send2trash (file )
40+
41+ except :
42+ try :
43+ with SevenZipFile (file , mode = 'r' ) as archive :
44+ archive .extractall (path = destination )
45+
46+ send2trash (file )
47+ except :
48+ pass
49+
2750class EventHandler (FileSystemEventHandler ):
2851 def __init__ (self ):
2952 self .whitelist = []
3053
3154 def on_modified (self , event ):
3255 if event .is_directory is False :
3356 file = event .src_path
34- if not file in self .whitelist :
57+ if file not in self .whitelist :
3558 self .whitelist .append (file )
36- try :
37- try :
38- unpack_archive (
39- file ,
40- rf"{ downloads_path } " )
41-
42- send2trash (file )
4359
44- except :
45- try :
46- with SevenZipFile (file , mode = 'r' ) as archive :
47- archive .extractall (path = rf"{ downloads_path } " )
48-
49- send2trash (file )
50- except :
51- pass
52- except Exception as error :
53- print (error )
60+ extractor (file )
5461
5562 self .whitelist .remove (file )
5663
5764if __name__ == "__main__" :
5865 files = listdir (downloads_path )
5966 for file in files :
60- try :
61- unpack_archive (
62- rf"{ downloads_path } \{ file } " ,
63- rf"{ downloads_path } " )
64-
65- send2trash (rf"{ downloads_path } \{ file } " )
66-
67- except :
68- try :
69- with SevenZipFile (rf"{ downloads_path } \{ file } " , mode = 'r' ) as archive :
70- archive .extractall (path = rf"{ downloads_path } " )
71-
72- send2trash (rf"{ downloads_path } \{ file } " )
73- except :
74- pass
67+ extractor (downloads_path + sep + file )
7568
7669 event_handler = EventHandler ()
7770 observer = Observer ()
7871 observer .schedule (event_handler , downloads_path , recursive = True )
7972 observer .start ()
80- try :
81- while True :
82- sleep (1 )
83- except KeyboardInterrupt :
84- observer .stop ()
8573 observer .join ()
0 commit comments