-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassifyFileFormat.py
More file actions
170 lines (132 loc) · 6.84 KB
/
classifyFileFormat.py
File metadata and controls
170 lines (132 loc) · 6.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import os
import shutil
import time
import re
import logging
from datetime import datetime
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from configDB import (my_folder, classification_rules, GREEN, RESET, log_filename, DAYS_BEFORE_ARCHIVE,
ARCHIVE_ACTION, ARCHIVE_FOLDER, doArchive)
from datetime import timedelta
# Configure logging
logging.basicConfig(filename=log_filename, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# Function to determine the folder to move the file to based on its extension
def classify_file(file, rules):
file_extension = os.path.splitext(file)[1]
for folder, extensions in rules.items():
if file_extension in extensions:
return folder
return None
# Function to check if a file is a duplicate based on its name pattern
def is_duplicate(file):
file_name, file_extension = os.path.splitext(file)
duplicate_pattern = re.compile(r'^(.+)\s\(\d+\)$')
match = duplicate_pattern.match(file_name)
if match:
original_file_name = match.group(1) + file_extension
return original_file_name
else:
return None
def get_date_folder(file_path):
last_modified = os.path.getmtime(file_path)
# print(datetime.fromtimestamp(last_modified))
date = datetime.fromtimestamp(last_modified).strftime('%Y-%m') # if want in day level use strftime('%Y-%m-%d')
return date
# Function to classify and move existing files in the download folder
def classify_existing_files():
for file in os.listdir(my_folder):
file_path = os.path.join(my_folder, file)
if os.path.isfile(file_path):
target_folder = classify_file(file, classification_rules)
if target_folder:
date_folder = get_date_folder(file_path)
target_path = os.path.join(my_folder, target_folder, date_folder, file)
if not os.path.exists(os.path.dirname(target_path)):
os.makedirs(os.path.dirname(target_path))
shutil.move(file_path, target_path)
print(f'{GREEN}Moved {file} to {target_folder}/{date_folder}{RESET}')
logging.info(f'Moved {file} to {target_folder}/{date_folder}')
# Check if the file is a duplicate and delete it if the original exists
original_file = is_duplicate(file)
if original_file:
original_file_path = os.path.join(my_folder, target_folder, date_folder, original_file)
if os.path.exists(original_file_path):
os.remove(target_path)
print(f'{GREEN}Deleted duplicate file {file}{RESET}')
logging.info(f'Deleted duplicate file {file}')
# Check folders for old files and either move them
# to an archive folder or delete them based on user preferences
def folder_cleanup():
current_time = datetime.now()
for folder, extensions in classification_rules.items():
folder_path = os.path.join(my_folder, folder)
for root, _, files in os.walk(folder_path):
for file in files:
file_path = os.path.join(root, file)
if os.path.getmtime(file_path) < (current_time - timedelta(days=DAYS_BEFORE_ARCHIVE)).timestamp():
if ARCHIVE_ACTION == "move":
archive_path = os.path.join(my_folder, ARCHIVE_FOLDER)
if not os.path.exists(archive_path):
os.makedirs(archive_path)
shutil.move(file_path, os.path.join(archive_path, file))
print(f'{GREEN}Moved old file {file} to {ARCHIVE_FOLDER}{RESET}')
logging.info(f'Moved old file {file} to {ARCHIVE_FOLDER}')
elif ARCHIVE_ACTION == "delete":
os.remove(file_path)
print(f'{GREEN}Deleted old file {file}{RESET}')
logging.info(f'Deleted old file {file}')
# Custom event handler to handle file creation events
class DownloadHandler(FileSystemEventHandler):
def on_created(self, event):
file = event.src_path
if os.path.isfile(file):
target_folder = classify_file(file, classification_rules)
if target_folder:
date_folder = get_date_folder(file)
target_path = os.path.join(my_folder, target_folder, date_folder, os.path.basename(file))
if not os.path.exists(os.path.dirname(target_path)):
os.makedirs(os.path.dirname(target_path))
shutil.move(file, target_path)
print(f'{GREEN}Moved {file} to {target_folder}/{date_folder}{RESET}')
logging.info(f'Moved {file} to {target_folder}/{date_folder}')
# Check if the file is a duplicate and delete it if the original exists
original_file = is_duplicate(os.path.basename(file))
if original_file:
original_file_path = os.path.join(my_folder, target_folder, date_folder, original_file)
if os.path.exists(original_file_path):
os.remove(target_path)
print(f'{GREEN}Deleted duplicate file {os.path.basename(file)}{RESET}')
logging.info(f'Deleted duplicate file {os.path.basename(file)}')
def main():
# Create the necessary folders if they don't exist
for folder in classification_rules.keys():
folder_path = os.path.join(my_folder, folder)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
# Also ensure the ARCHIVE_FOLDER exists if the action is set to "move"
if ARCHIVE_ACTION == "move":
archive_path = os.path.join(my_folder, ARCHIVE_FOLDER)
if not os.path.exists(archive_path):
os.makedirs(archive_path)
# Classify existing files in the directory
classify_existing_files()
# Create and start the observer to monitor my_folder
event_handler = DownloadHandler()
observer = Observer()
observer.schedule(event_handler, path=my_folder, recursive=False)
observer.start()
last_cleanup_time = time.time()
try:
print(f'doArchive set to : {doArchive}')
while doArchive: #doArchive--> to set archive action from configDB file.
# If 24 hours have passed since the last cleanup
if time.time() - last_cleanup_time >= 86400: #86400
folder_cleanup()
last_cleanup_time = time.time()
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
if __name__ == '__main__':
main()