Skip to content

Commit 7640dde

Browse files
added a save feature using json
1 parent 4753bb2 commit 7640dde

File tree

7 files changed

+121
-33
lines changed

7 files changed

+121
-33
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.venv/
22
.vscode/
3-
build_windows/
3+
build_windows/
4+
__pycache__/

auto_download_cleaner.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
import os
22
import shutil
33
import variable as var
4+
import save as sv
45
import tray_icon as ti
56
import time as time
6-
def move_the_file_to_right_place(main_directory, move_folder, filename):
7+
def move_file(main_directory, move_folder, filename):
78
if not os.path.exists(move_folder):
89
os.makedirs(move_folder)
910
shutil.move(os.path.join(main_directory, filename), os.path.join(move_folder, filename))
10-
def check_file_extension(file_ext, dir, mov_dir, filename):
11+
def check_file_extension(file_ext, mov_dir, filename):
1112
if filename.endswith(file_ext):
1213
time.sleep(1)
13-
move_the_file_to_right_place(main_directory=dir, move_folder=mov_dir, filename=filename)
14+
move_file(main_directory=sv.get_default_download_folder(), move_folder=mov_dir, filename=filename)
1415
def main_stuff():
16+
loaded_vars = sv.load_variables_from_json()
17+
print(loaded_vars)
18+
print(sv.get_default_download_folder())
19+
print(loaded_vars['DOCX'])
1520
print('Program started.')
1621
while var.run:
17-
for filename in os.listdir(var.directory):
18-
check_file_extension(".pdf", dir=var.directory, mov_dir=var.PDF, filename=filename)
19-
check_file_extension(".docx", dir=var.directory, mov_dir=var.DOCX, filename=filename)
20-
check_file_extension('.zip', dir=var.directory, mov_dir=var.ZIP, filename=filename)
21-
check_file_extension(tuple(var.Image), dir=var.directory, mov_dir=var.IMAGE, filename=filename)
22-
check_file_extension('.py', dir=var.directory, mov_dir=var.PYTHON, filename=filename)
23-
check_file_extension(tuple(var.Video), dir=var.directory, mov_dir=var.VIDEO, filename=filename)
24-
check_file_extension(tuple(var.audio_formats), dir=var.directory, mov_dir=var.MUSIC, filename=filename)
25-
check_file_extension('.torrent', dir=var.directory, mov_dir=var.TORRENT, filename=filename)
26-
27-
28-
if __name__ == '__main__':
22+
for filename in os.listdir(sv.get_default_download_folder()):
23+
try:
24+
check_file_extension(".pdf", mov_dir=loaded_vars[var.PDF], filename=filename)
25+
check_file_extension(".docx", mov_dir=loaded_vars[var.DOCX], filename=filename)
26+
check_file_extension('.zip', mov_dir=loaded_vars[var.ZIP], filename=filename)
27+
check_file_extension(var.Image, mov_dir=loaded_vars[var.IMAGE], filename=filename)
28+
check_file_extension('.py', mov_dir=loaded_vars[var.Python], filename=filename)
29+
check_file_extension(var.Video, mov_dir=loaded_vars[var.VIDEO], filename=filename)
30+
check_file_extension(var.audio_formats, mov_dir=loaded_vars[var.MUSIC], filename=filename)
31+
check_file_extension('.torrent', mov_dir=loaded_vars[var.Torrent], filename=filename)
32+
except PermissionError:
33+
continue
34+
if __name__ == "__main__":
35+
sv.main()
2936
ti.start()
30-
main_stuff()
37+
main_stuff()

config.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"PDF": "C:\\Users\\CodeCraft\\Documents\\PDF",
3+
"DOCX": "C:\\Users\\CodeCraft\\Documents\\DOCX",
4+
"ZIP": "D:\\Zip",
5+
"Image": "C:\\Users\\CodeCraft\\Pictures\\Photos",
6+
"Python": "D:\\Python",
7+
"Video": "C:\\Users\\CodeCraft\\Videos",
8+
"Music": "C:\\Users\\CodeCraft\\Music",
9+
"Torrent": "C:\\Users\\CodeCraft\\Downloads\\Torrents"
10+
}

save.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
import json
3+
import getpass
4+
import variable
5+
def get_default_download_folder():
6+
7+
# try to automatically detect the download folder
8+
default_download_folder = None
9+
10+
# Platform-specific logic to detect the download folder
11+
if os.name == "posix": # Linux/Mac
12+
default_download_folder = os.path.expanduser("~/Downloads")
13+
elif os.name == "nt": # Windows
14+
default_download_folder = os.path.join(os.path.expanduser("~"), "Downloads")
15+
else:
16+
print("Unsupported operating system.")
17+
exit(1)
18+
19+
return default_download_folder
20+
21+
def create_default_directories():
22+
# If username is "codecraft", use the hardcoded download folder
23+
username = getpass.getuser()
24+
if username=="CodeCraft":
25+
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")
34+
}
35+
return default_dirs
36+
# Define default directories for different file types
37+
default_dirs = {
38+
"PDF": os.path.join(os.path.expanduser("~"),"Documents", "PDF"),
39+
"DOCX": os.path.join(os.path.expanduser("~"),"Documents", "DOCX"),
40+
"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"),
44+
"Music": os.path.join(os.path.expanduser("~"), "Music"),
45+
"Torrent": os.path.join(get_default_download_folder(), "Torrents")
46+
}
47+
48+
return default_dirs
49+
50+
def save_variables_to_json(vars_dict):
51+
with open("config.json", "w") as json_file:
52+
json.dump(vars_dict, json_file, indent=4)
53+
def load_variables_from_json():
54+
dirname = os.path.dirname(__file__)
55+
filename = os.path.join(dirname, 'config.json')
56+
try:
57+
with open(filename, "r") as json_file:
58+
loaded_vars = json.load(json_file)
59+
except FileNotFoundError:
60+
# If the config file doesn't exist, return an empty dictionary
61+
return {}
62+
return loaded_vars
63+
def main():
64+
username = getpass.getuser()
65+
# Create default directories for different file types
66+
default_directories = create_default_directories()
67+
68+
# Save the variables to a JSON file
69+
if (os.path.exists("config.json"))==False:
70+
save_variables_to_json(default_directories)

setup.py

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

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

8-
base = 'Win32GUI' if sys.platform=='win32' else None
8+
base = "Console"
99

1010
executables = [
11-
Executable('auto_download_cleaner.py', base=base, target_name = 'Auto-Download-Cleanup',icon='icon.ico')
11+
Executable('auto_downloader_clearner.py', base=base, target_name = 'Auto-Download-Cleanup',icon='icon.ico')
1212
]
1313

1414
setup(name='Auto-Download-Cleanup',

tray_icon.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ def on_click(icon, item):
1010
icon.stop()
1111
vars.run = False
1212

13-
icon = pystray.Icon("name", image, "title", menu=pystray.Menu(pystray.MenuItem("Quit", on_click)))
13+
icon = pystray.Icon("name", image, "Auto Download Cleanup", menu=pystray.Menu(pystray.MenuItem("Quit", on_click)))
1414
def start():
1515
icon.run_detached()

variable.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
run = True
2-
3-
directory = 'C:\\Users\\CodeCraft\\Downloads'
4-
PDF = 'C:\\Users\\CodeCraft\\Documents\\PDF'
5-
DOCX = 'C:\\Users\\CodeCraft\\Documents\\DOC'
6-
TORRENT = 'C:\\Users\\CodeCraft\\Downloads\\TORRENT'
7-
ZIP = 'D:\\Zip'
8-
Image = ['.png', '.jpg', '.jpeg', '.gif', '.tiff', '.psd', '.raw', '.bmp', '.heif', '.indd', '.svg', '.ai', '.eps', '.pdf', '.ico', '.webp']
9-
IMAGE = 'C:\\Users\\CodeCraft\\Pictures\\Photos'
10-
PYTHON = 'D:\\Python'
11-
VIDEO = 'C:\\Users\\CodeCraft\\Videos'
12-
MUSIC = 'C:\\Users\\CodeCraft\\Music'
13-
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']
2+
ZIP_DIR = 'D:\\Zip'
3+
Image = ('.png', '.jpg', '.jpeg', '.gif', '.tiff', '.psd', '.raw', '.bmp', '.heif', '.indd', '.svg', '.ai', '.eps', '.ico', '.webp')
4+
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')
146
audio_formats = ('.mp3', '.wav', '.aiff', '.flac', '.aac', '.ogg', '.wma', '.m4a', '.ape', '.alac', '.dsd')
7+
PDF = "PDF"
8+
DOCX = "DOCX"
9+
ZIP = "ZIP"
10+
IMAGE = "Image"
11+
Python = "Python"
12+
VIDEO = "Video"
13+
MUSIC = "Music"
14+
Torrent = "Torrent"

0 commit comments

Comments
 (0)