Skip to content

Commit 21562f8

Browse files
Much more efficient
great, but has a tendency to chuck files into parent directory - easy fix: "Extract {} at {time}"
1 parent e730e57 commit 21562f8

File tree

2 files changed

+56
-14
lines changed

2 files changed

+56
-14
lines changed

main.py

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from send2trash import send2trash
88
from py7zr import SevenZipFile
9+
from watchdog.observers import Observer
10+
from watchdog.events import FileSystemEventHandler
911

1012
def get_download_path():
1113
"""Returns the default downloads path for linux or windows"""
@@ -24,23 +26,62 @@ def get_download_path():
2426

2527
basic_archive_types = [".zip", ".tar", ".gztar", ".bztar", ".xztar"]
2628

27-
while True:
28-
try:
29-
files = listdir(downloads_path)
30-
for file in files:
31-
if any(x in file for x in basic_archive_types):
32-
unpack_archive(
33-
rf"{downloads_path}\{file}",
34-
rf"{downloads_path}")
29+
class EventHandler(FileSystemEventHandler):
30+
def __init__(self):
31+
self.whitelist = []
3532

36-
send2trash(rf"{downloads_path}\{file}")
33+
def on_modified(self, event):
34+
if event.is_directory is False:
35+
file = event.src_path
36+
if not file in self.whitelist:
37+
self.whitelist.append(file)
38+
try:
39+
try:
40+
unpack_archive(
41+
file,
42+
rf"{downloads_path}")
43+
44+
send2trash(file)
45+
46+
except:
47+
try:
48+
with SevenZipFile(file, mode='r') as archive:
49+
archive.extractall(path=rf"{downloads_path}")
50+
51+
send2trash(file)
52+
except:
53+
pass
54+
except Exception as error:
55+
print(error)
3756

38-
elif ".7z" in file:
57+
self.whitelist.remove(file)
58+
59+
if __name__ == "__main__":
60+
files = listdir(downloads_path)
61+
for file in files:
62+
try:
63+
unpack_archive(
64+
rf"{downloads_path}\{file}",
65+
rf"{downloads_path}")
66+
67+
send2trash(rf"{downloads_path}\{file}")
68+
69+
except:
70+
try:
3971
with SevenZipFile(rf"{downloads_path}\{file}", mode='r') as archive:
4072
archive.extractall(path=rf"{downloads_path}")
4173

4274
send2trash(rf"{downloads_path}\{file}")
43-
except Exception as error:
44-
print(error)
75+
except:
76+
pass
4577

46-
sleep(10)
78+
event_handler = EventHandler()
79+
observer = Observer()
80+
observer.schedule(event_handler, downloads_path, recursive=True)
81+
observer.start()
82+
try:
83+
while True:
84+
sleep(1)
85+
except KeyboardInterrupt:
86+
observer.stop()
87+
observer.join()

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
send2trash
2-
py7zr
2+
py7zr
3+
watchdog

0 commit comments

Comments
 (0)