Skip to content

Commit 345d515

Browse files
authored
Update sorty.py
1 parent 5e94d79 commit 345d515

File tree

1 file changed

+87
-54
lines changed

1 file changed

+87
-54
lines changed

sorty.py

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
11
import json
22
import tkinter as tk
33
from tkinter import filedialog
4-
4+
import threading
55
import PIL.Image
66
import PIL.ImageTk
77
import pystray
88
from notifypy import Notify
9+
import time
10+
import os
11+
import shutil
12+
import mimetypes
913

1014
image = PIL.Image.open("ico.png")
1115

12-
enable_notifications = True
16+
NOTIFY_ENABLE = True
17+
18+
DIR_DESTINATION = ""
1319

14-
destination = ""
20+
AUTO_SORT = False
1521

22+
NOTIFY = Notify()
1623

17-
def create_settings():
24+
25+
def SendNotify(title: str, msg:str) -> None:
26+
NOTIFY.application_name = "Sorty!"
27+
NOTIFY.title = title
28+
NOTIFY.message = msg
29+
NOTIFY.icon = "ico.png"
30+
NOTIFY.send()
31+
32+
def create_settings(icon: pystray.Icon) -> None:
1833
root = tk.Tk()
1934
root.withdraw()
2035
ico = PIL.Image.open('ico.png')
@@ -23,8 +38,12 @@ def create_settings():
2338

2439
download_folder = filedialog.askdirectory(title="Please select the downloads folder.")
2540
if not download_folder:
26-
print("The folder is not selected. Exit the program.")
27-
return
41+
SendNotify("😭", "I cant work correctly without folder!")
42+
icon.stop()
43+
44+
45+
46+
2847
notifications_enabled = True
2948
settings = {
3049
"notificationsenabled": notifications_enabled,
@@ -33,13 +52,7 @@ def create_settings():
3352
with open("settings.sorty", "w") as settings_file:
3453
json.dump(settings, settings_file)
3554

36-
37-
import os
38-
import shutil
39-
import mimetypes
40-
41-
42-
def sort_files(directory):
55+
def sort_files(directory: str) -> int:
4356
folders = {
4457
"Images": os.path.join(directory, "Images"),
4558
"Audio": os.path.join(directory, "Audio"),
@@ -86,66 +99,86 @@ def sort_files(directory):
8699
target_folder = folders["Other"]
87100

88101
shutil.move(file_path, os.path.join(target_folder, filename))
89-
print(f"Перемещен: {filename} в {target_folder}")
90102
moved_files_count += 1
91103
except Exception as e:
92-
print(f"Не удалось переместить файл {filename}: {e}")
104+
pass
93105

94106
return moved_files_count
95107

108+
def sort(icon: pystray.Icon) -> None:
109+
if NOTIFY_ENABLE:
110+
SendNotify("Sorted", f"We have sorted {sort_files(DIR_DESTINATION)} files!")
111+
else:
112+
sort_files(DIR_DESTINATION)
96113

97-
def sort(icon, item):
98-
if enable_notifications:
99-
notification = Notify()
100-
notification.application_name = "Sorty!"
101-
notification.title = "Sorted!"
102-
notification.message = f"We have sorted {sort_files(destination)} files!"
103-
notification.icon = "ico.png"
114+
def exit_action(icon: pystray.Icon) -> None:
115+
icon.stop()
116+
exit(0)
104117

105-
notification.send()
118+
def change_dir(icon: pystray.Icon) -> None:
119+
global NOTIFY_ENABLE, DIR_DESTINATION
120+
create_settings(icon)
121+
with open("settings.sorty", "r") as settings_file:
122+
settings = json.load(settings_file)
123+
124+
NOTIFY_ENABLE = settings.get("notificationsenabled", NOTIFY_ENABLE)
125+
DIR_DESTINATION = settings.get("download_folder", DIR_DESTINATION)
126+
127+
def autoswitcher():
128+
global AUTO_SORT
129+
130+
if AUTO_SORT:
131+
AUTO_SORT = False
132+
SendNotify("Switched", "Auto is off now!")
106133

107134
else:
108-
sort_files(destination)
135+
AUTO_SORT = True
136+
SendNotify("Switched", "Auto is on now!")
109137

110-
print("sorted!")
111138

112139

113-
def exit_action(icon: pystray.Icon):
114-
icon.stop()
115-
exit(0)
140+
def Auto() -> None:
141+
while True:
142+
if AUTO_SORT:
143+
prev_mtime = os.path.getmtime(DIR_DESTINATION)
144+
while True:
145+
mtime = os.path.getmtime(DIR_DESTINATION)
146+
if AUTO_SORT:
147+
if mtime != prev_mtime:
148+
sort_files(DIR_DESTINATION)
149+
prev_mtime = mtime
150+
else:
151+
break
152+
time.sleep(1)
116153

154+
def Main() -> None:
117155

118-
def change_settings():
119-
global enable_notifications, destination
120-
create_settings()
121-
with open("settings.sorty", "r") as settings_file:
122-
settings = json.load(settings_file)
156+
icon = pystray.Icon("Sorty", image, menu=pystray.Menu(
157+
pystray.MenuItem("Sort!", sort),
158+
pystray.MenuItem("Сhange Directory",lambda: change_dir(icon)),
159+
pystray.MenuItem("Auto ON/OF", autoswitcher),
160+
pystray.MenuItem("Exit", lambda: exit_action(icon))
161+
))
162+
global NOTIFY_ENABLE, DIR_DESTINATION
123163

124-
enable_notifications = settings.get("notificationsenabled", enable_notifications)
125-
destination = settings.get("download_folder", destination)
164+
if os.path.exists("settings.sorty"):
165+
with open("settings.sorty", "r") as settings_file:
166+
settings = json.load(settings_file)
167+
NOTIFY_ENABLE = settings.get("notificationsenabled", NOTIFY_ENABLE)
168+
DIR_DESTINATION = settings.get("download_folder", DIR_DESTINATION)
126169

127-
print(destination)
128-
print(enable_notifications)
170+
else:
171+
create_settings(icon)
129172

130173

131-
icon = pystray.Icon("Sorty", image, menu=pystray.Menu(
132-
pystray.MenuItem("Sort!", sort),
133-
pystray.MenuItem("Сhange Directory", change_settings),
134-
pystray.MenuItem("Exit", lambda: exit_action(icon))
135-
))
136174

137-
if os.path.exists("settings.sorty"):
138-
with open("settings.sorty", "r") as settings_file:
139-
settings = json.load(settings_file)
140-
enable_notifications = settings.get("notificationsenabled", enable_notifications)
141-
destination = settings.get("download_folder", destination)
175+
listener_thread = threading.Thread(target=Auto)
176+
listener_thread.daemon = True
177+
listener_thread.start()
178+
142179

143-
print(destination)
144-
print(enable_notifications)
145-
else:
146-
create_settings()
147180

148-
print(destination)
149-
print(enable_notifications)
181+
icon.run()
150182

151-
icon.run()
183+
if __name__ == "__main__":
184+
Main()

0 commit comments

Comments
 (0)