Skip to content

Commit 7720541

Browse files
optimized it
1 parent 8a1a5dd commit 7720541

File tree

8 files changed

+86
-63
lines changed

8 files changed

+86
-63
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
.venv/
22
.vscode/
33
build_windows/
4-
__pycache__/
4+
__pycache__/
5+
/.idea/
Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import os
22
import shutil
33
import variable as var
4-
import save as sv
54
import tray_icon as ti
5+
import save as sv
66
import time as time
77
import json
8+
9+
810
def load_variables_from_json():
9-
dirname = os.path.dirname(__file__)
11+
dirname = os.path.join(os.getenv("APPDATA"), "Auto-Download-Cleaner")
1012
filename = os.path.join(dirname, 'config.json')
1113
try:
1214
with open(filename, "r") as json_file:
@@ -15,38 +17,48 @@ def load_variables_from_json():
1517
# If the config file doesn't exist, return an empty dictionary
1618
return {}
1719
return loaded_vars
20+
21+
1822
def move_file(main_directory, move_folder, filename):
1923
if move_folder is None:
2024
raise ValueError("Invalid move_folder: None")
21-
25+
2226
if not os.path.exists(move_folder):
2327
os.makedirs(move_folder)
24-
28+
2529
shutil.move(os.path.join(main_directory, filename), os.path.join(move_folder, filename))
30+
31+
2632
def check_file_extension(file_ext, mov_dir, filename):
2733
if filename.endswith(file_ext):
2834
time.sleep(1)
2935
move_file(main_directory=sv.get_default_download_folder(), move_folder=mov_dir, filename=filename)
36+
37+
3038
def main_stuff():
3139
sv.main()
3240
loaded_vars = load_variables_from_json()
3341
print(loaded_vars)
3442
print(sv.get_default_download_folder())
3543
print('Program started.')
3644
while var.run:
37-
for filename in os.listdir(sv.get_default_download_folder()):
38-
try:
39-
check_file_extension(".pdf", mov_dir=loaded_vars.get(var.PDF), filename=filename)
40-
check_file_extension(".docx", mov_dir=loaded_vars.get(var.DOCX), filename=filename)
41-
check_file_extension('.zip', mov_dir=loaded_vars.get(var.ZIP), filename=filename)
42-
check_file_extension(var.Image, mov_dir=loaded_vars.get(var.IMAGE), filename=filename)
43-
check_file_extension('.py', mov_dir=loaded_vars.get(var.Python), filename=filename)
44-
check_file_extension(var.Video, mov_dir=loaded_vars.get(var.VIDEO), filename=filename)
45-
check_file_extension(var.audio_formats,mov_dir=loaded_vars.get(var.MUSIC), filename=filename)
46-
check_file_extension('.torrent', mov_dir=loaded_vars.get(var.Torrent), filename=filename)
47-
except PermissionError:
48-
continue
45+
print("running")
46+
try:
47+
folder_content = os.listdir(sv.get_default_download_folder())
48+
except PermissionError:
49+
continue
50+
for filename in folder_content:
51+
check_file_extension(".pdf", mov_dir=loaded_vars.get(var.PDF), filename=filename)
52+
check_file_extension(".docx", mov_dir=loaded_vars.get(var.DOCX), filename=filename)
53+
check_file_extension(var.Zip, mov_dir=loaded_vars.get(var.ZIP), filename=filename)
54+
check_file_extension(var.Image, mov_dir=loaded_vars.get(var.IMAGE), filename=filename)
55+
check_file_extension('.py', mov_dir=loaded_vars.get(var.Python), filename=filename)
56+
check_file_extension(var.Video, mov_dir=loaded_vars.get(var.VIDEO), filename=filename)
57+
check_file_extension(var.audio_formats, mov_dir=loaded_vars.get(var.MUSIC), filename=filename)
58+
check_file_extension('.torrent', mov_dir=loaded_vars.get(var.Torrent), filename=filename)
59+
time.sleep(0.5)
60+
61+
4962
if __name__ == "__main__":
50-
5163
ti.start()
52-
main_stuff()
64+
main_stuff()

config.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pystray>=0.19.4
2+
Pillow>=10.1.0

save.py

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
import json
33
import getpass
44
import variable
5-
def get_default_download_folder():
65

6+
7+
def get_default_download_folder():
78
# try to automatically detect the download folder
89
default_download_folder = None
910

@@ -18,44 +19,52 @@ def get_default_download_folder():
1819

1920
return default_download_folder
2021

22+
2123
def create_default_directories():
22-
# If username is "codecraft", use the hardcoded download folder
24+
# If username is "codecraft", use the hardcoded download folder this is for my personal use
2325
username = getpass.getuser()
24-
if username=="CodeCraft":
26+
if username == "CodeCraft":
2527
default_dirs = default_dirs = {
26-
"PDF": os.path.join(os.path.expanduser("~"),"Documents", "PDF"),
27-
"DOCX": os.path.join(os.path.expanduser("~"),"Documents", "DOCX"),
28-
"ZIP": variable.ZIP_DIR,
29-
"Image": os.path.join(os.path.expanduser("~"),"Pictures", "Photos"),
30-
"Python": variable.PYTHON_DIR,
31-
"Video": os.path.join(os.path.expanduser("~"),"Videos"),
32-
"Music": os.path.join(os.path.expanduser("~"), "Music"),
33-
"Torrent": os.path.join(get_default_download_folder(), "Torrents")
28+
"PDF": os.path.join(os.path.expanduser("~"), "Documents", "PDF"),
29+
"DOCX": os.path.join(os.path.expanduser("~"), "Documents", "DOCX"),
30+
"ZIP": variable.ZIP_DIR,
31+
"Image": os.path.join(os.path.expanduser("~"), "Pictures", "Photos"),
32+
"Python": variable.PYTHON_DIR,
33+
"Video": os.path.join(os.path.expanduser("~"), "Videos"),
34+
"Music": os.path.join(os.path.expanduser("~"), "Music"),
35+
"Torrent": os.path.join(get_default_download_folder(), "Torrents")
3436
}
3537
return default_dirs
3638
# Define default directories for different file types
3739
default_dirs = {
38-
"PDF": os.path.join(os.path.expanduser("~"),"Documents", "PDF"),
39-
"DOCX": os.path.join(os.path.expanduser("~"),"Documents", "DOCX"),
40+
"PDF": os.path.join(os.path.expanduser("~"), "Documents", "PDF"),
41+
"DOCX": os.path.join(os.path.expanduser("~"), "Documents", "DOCX"),
4042
"ZIP": os.path.join(get_default_download_folder(), "ZIP"),
41-
"Image": os.path.join(os.path.expanduser("~"),"Pictures", "Photos"),
42-
"Python": os.path.join(os.path.expanduser("~"),"Code", "Python"),
43-
"Video": os.path.join(os.path.expanduser("~"),"Videos"),
43+
"Image": os.path.join(os.path.expanduser("~"), "Pictures", "Photos"),
44+
"Python": os.path.join(os.path.expanduser("~"), "Code", "Python"),
45+
"Video": os.path.join(os.path.expanduser("~"), "Videos"),
4446
"Music": os.path.join(os.path.expanduser("~"), "Music"),
4547
"Torrent": os.path.join(get_default_download_folder(), "Torrents")
4648
}
4749

4850
return default_dirs
4951

52+
5053
def save_variables_to_json(vars_dict):
51-
with open("config.json", "w") as json_file:
54+
appdata = os.getenv("APPDATA")
55+
appdir_config = os.path.join(appdata, "Auto-Download-Cleaner")
56+
if not os.path.exists(appdir_config):
57+
os.makedirs(appdir_config)
58+
filename = os.path.join(appdir_config, "config.json")
59+
with open(filename, "w") as json_file:
5260
json.dump(vars_dict, json_file, indent=4)
5361

62+
5463
def main():
5564
username = getpass.getuser()
5665
# Create default directories for different file types
5766
default_directories = create_default_directories()
5867

5968
# Save the variables to a JSON file
60-
if (os.path.exists("config.json"))==False:
61-
save_variables_to_json(default_directories)
69+
if (os.path.exists("config.json")) == False:
70+
save_variables_to_json(default_directories)

setup.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
# Dependencies are automatically detected, but it might need
55
# fine tuning.
6-
build_options = {'packages': ['os','getpass'], 'excludes': [],'build_exe': 'build_windows','include_files':['icon.ico']}
6+
build_options = {'packages': ['os', 'getpass'], 'excludes': [], 'build_exe': 'build_windows',
7+
'include_files': ['icon.ico']}
78

8-
base = 'Win32GUI' if sys.platform=='win32' else None
9+
base = 'Win32GUI' if sys.platform == 'win32' else None
910

1011
executables = [
11-
Executable('auto_download_cleaner.py', base=base, target_name = 'Auto Download Cleanup',icon='icon.ico')
12+
Executable('auto_download_sorter.py', base=base, target_name='Auto Download Sorter', icon='icon.ico')
1213
]
13-
14-
setup(name='Auto-Download-Cleanup',
15-
version = '1.0',
16-
description = 'Automatic sorter for downloads folder',
17-
options = {'build_exe': build_options},
18-
executables = executables)
14+
setup(name='Auto Download Sorter',
15+
version='1.0',
16+
description='Auto Download Sorter',
17+
options={'build_exe': build_options},
18+
executables=executables)

tray_icon.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
import PIL.Image
33
import variable as vars
44

5-
image = PIL.Image.open("icon.ico")
5+
image = PIL.Image.open('icon.ico')
66

7-
def on_click(icon, item):
7+
8+
def on_click(main_icon, item):
89
if str(item) == 'Quit':
910
print('Closing the app.')
10-
icon.stop()
11+
main_icon.stop()
1112
vars.run = False
1213

14+
1315
icon = pystray.Icon("name", image, "Auto Download Cleanup", menu=pystray.Menu(pystray.MenuItem("Quit", on_click)))
16+
17+
1418
def start():
1519
icon.run_detached()

variable.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
run = True
22
ZIP_DIR = 'D:\\Zip'
3-
Image = ('.png', '.jpg', '.jpeg', '.gif', '.tiff', '.psd', '.raw', '.bmp', '.heif', '.indd', '.svg', '.ai', '.eps', '.ico', '.webp')
3+
Image = (
4+
'.png', '.jpg', '.jpeg', '.gif', '.tiff', '.psd', '.raw', '.bmp', '.heif', '.indd', '.svg', '.ai', '.eps', '.ico',
5+
'.webp')
6+
Zip = ('.rar', '.zip', '.tar', '.gz', '.7z')
47
PYTHON_DIR = 'D:\\Python'
5-
Video = ('.mp4', '.mov', '.wmv', '.avi', '.avchd', '.flv', '.f4v', '.swf', '.mkv', '.webm', '.mng', '.gifv', '.mpg', '.mp2', '.mpeg', '.mpe', '.mpv', '.ogg', '.m4p', '.m4v', '.wmv', '.mov', '.qt', '.flv', '.swf', '.avchd')
8+
Video = (
9+
'.mp4', '.mov', '.wmv', '.avi', '.avchd', '.flv', '.f4v', '.swf', '.mkv', '.webm', '.mng', '.gifv', '.mpg', '.mp2',
10+
'.mpeg', '.mpe', '.mpv', '.ogg', '.m4p', '.m4v', '.wmv', '.mov', '.qt', '.flv', '.swf', '.avchd')
611
audio_formats = ('.mp3', '.wav', '.aiff', '.flac', '.aac', '.ogg', '.wma', '.m4a', '.ape', '.alac', '.dsd')
712
PDF = "PDF"
813
DOCX = "DOCX"
@@ -11,4 +16,4 @@
1116
Python = "Python"
1217
VIDEO = "Video"
1318
MUSIC = "Music"
14-
Torrent = "Torrent"
19+
Torrent = "Torrent"

0 commit comments

Comments
 (0)