mirror of
https://github.com/tuomaskivioja/File-Downloads-Automator.git
synced 2026-08-24 10:04:09 -05:00
Merge pull request #4 from KP64/main
Static Imports and a bit more flexibility
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
# File-Downloads-Automator
|
||||
Sends downloaded files to user-defined folders automatically. Avoids cluttering your downloads folder.
|
||||
|
||||
Sends downloaded files to user-defined folders automatically. Avoids cluttering your downloads folder.
|
||||
|
||||
+77
-44
@@ -1,62 +1,95 @@
|
||||
import os
|
||||
import shutil
|
||||
import time
|
||||
from os import scandir, rename
|
||||
from os.path import splitext, exists
|
||||
from shutil import move
|
||||
from time import sleep
|
||||
|
||||
import logging
|
||||
|
||||
from watchdog.observers import Observer
|
||||
from watchdog.events import FileSystemEventHandler
|
||||
|
||||
## FILL IN BELOW
|
||||
source_dir = ## folder to track # example for Windows "C:\\Users\\Name\\Downloads"
|
||||
dest_dir_sfx = ## folder for images
|
||||
dest_dir_music = ## etc
|
||||
dest_dir_video = ## etc
|
||||
dest_dir_image = ## etc
|
||||
dest_dir_pdf = #example "C:\\Users\\Name\\Desktop\\Pdfs"
|
||||
# ! FILL IN BELOW
|
||||
# ? folder to track e.g. Windows: "C:\\Users\\UserName\\Downloads"
|
||||
source_dir = ""
|
||||
dest_dir_sfx = ""
|
||||
dest_dir_music = ""
|
||||
dest_dir_video = ""
|
||||
dest_dir_image = ""
|
||||
dest_dir_documents = ""
|
||||
|
||||
## ADD MORE TYPES
|
||||
# ? supported image types
|
||||
image_extensions = [".jpg", ".jpeg", ".jpe", ".jif", ".jfif", ".jfi", ".png", ".gif", ".webp", ".tiff", ".tif", ".psd", ".raw", ".arw", ".cr2", ".nrw",
|
||||
".k25", ".bmp", ".dib", ".heif", ".heic", ".ind", ".indd", ".indt", ".jp2", ".j2k", ".jpf", ".jpf", ".jpx", ".jpm", ".mj2", ".svg", ".svgz", ".ai", ".eps", ".ico"]
|
||||
# ? supported Video types
|
||||
video_extensions = [".webm", ".mpg", ".mp2", ".mpeg", ".mpe", ".mpv", ".ogg",
|
||||
".mp4", ".mp4v", ".m4v", ".avi", ".wmv", ".mov", ".qt", ".flv", ".swf", ".avchd"]
|
||||
# ? supported Audio types
|
||||
audio_extensions = [".m4a", ".flac", "mp3", ".wav", ".wma", ".aac"]
|
||||
# ? supported Document types
|
||||
document_extensions = [".doc", ".docx", ".odt",
|
||||
".pdf", ".xls", ".xlsx", ".ppt", ".pptx"]
|
||||
|
||||
def makeUnique(path):
|
||||
filename, extension = os.path.splitext(path)
|
||||
|
||||
def make_unique(path):
|
||||
filename, extension = splitext(path)
|
||||
counter = 1
|
||||
## IF FILE EXISTS, ADDS NUMBER TO THE END OF THE FILENAME
|
||||
while os.path.exists(path):
|
||||
path = filename + " (" + str(counter) + ")" + extension
|
||||
# * IF FILE EXISTS, ADDS NUMBER TO THE END OF THE FILENAME
|
||||
while exists(path):
|
||||
path = f"{filename} ({counter}){extension}"
|
||||
counter += 1
|
||||
|
||||
return path
|
||||
|
||||
def move(dest, entry, name):
|
||||
file_exists = os.path.exists(dest + "/" + name)
|
||||
if file_exists:
|
||||
unique_name = makeUnique(name)
|
||||
os.rename(entry, unique_name)
|
||||
shutil.move(entry,dest)
|
||||
|
||||
def move_file(dest, entry, name):
|
||||
if exists(f"{dest}/{name}"):
|
||||
unique_name = make_unique(name)
|
||||
rename(entry, unique_name)
|
||||
move(entry, dest)
|
||||
|
||||
|
||||
class MoverHandler(FileSystemEventHandler):
|
||||
## THIS FUNCTION WILL RUN WHENEVER THERE IS A CHANGE IN "source_dir"
|
||||
# ? THIS FUNCTION WILL RUN WHENEVER THERE IS A CHANGE IN "source_dir"
|
||||
# ? .upper is for not missing out on files with uppercase extensions
|
||||
def on_modified(self, event):
|
||||
with os.scandir(source_dir) as entries:
|
||||
with scandir(source_dir) as entries:
|
||||
for entry in entries:
|
||||
name = entry.name
|
||||
dest = source_dir
|
||||
## ADD MORE IF STATEMENTS FOR DIFFERENT FILETYPES
|
||||
if name.endswith('.wav') or name.endswith('.mp3'):
|
||||
if entry.stat().st_size < 25000000 or "SFX" in name:
|
||||
dest = dest_dir_sfx
|
||||
else:
|
||||
dest = dest_dir_music
|
||||
move(dest, entry, name)
|
||||
elif name.endswith('.mov') or name.endswith('.mp4'):
|
||||
dest = dest_dir_video
|
||||
move(dest, entry, name)
|
||||
elif name.endswith('.jpg') or name.endswith('.jpeg') or name.endswith('.png') or name.endswith('.PNG'): # Some images png are in uppercase
|
||||
dest = dest_dir_image
|
||||
move(dest, entry, name)
|
||||
elif name.endswith('.pdf'): # Add pdfs
|
||||
dest = dest_dir_pdf
|
||||
move(dest, entry, name)
|
||||
self.check_audio_files(entry, name)
|
||||
self.check_video_files(entry, name)
|
||||
self.check_image_files(entry, name)
|
||||
self.check_document_files(entry, name)
|
||||
|
||||
## NO NEED TO CHANGE BELOW CODE
|
||||
def check_audio_files(self, entry, name): # * Checks all Audio Files
|
||||
for audio_extension in audio_extensions:
|
||||
if name.endswith(audio_extension) or name.endswith(audio_extension.upper()):
|
||||
if entry.stat().st_size < 10_000_000 or "SFX" in name: # ? 10Megabytes
|
||||
dest = dest_dir_sfx
|
||||
else:
|
||||
dest = dest_dir_music
|
||||
move_file(dest, entry, name)
|
||||
logging.info(f"Moved audio file: {name}")
|
||||
|
||||
def check_video_files(self, entry, name): # * Checks all Video Files
|
||||
for video_extension in video_extensions:
|
||||
if name.endswith(video_extension) or name.endswith(video_extension.upper()):
|
||||
move_file(dest_dir_video, entry, name)
|
||||
logging.info(f"Moved video file: {name}")
|
||||
|
||||
def check_image_files(self, entry, name): # * Checks all Image Files
|
||||
for image_extension in image_extensions:
|
||||
if name.endswith(image_extension) or name.endswith(image_extension.upper()):
|
||||
move_file(dest_dir_image, entry, name)
|
||||
logging.info(f"Moved image file: {name}")
|
||||
|
||||
def check_document_files(self, entry, name): # * Checks all Document Files
|
||||
for documents_extension in document_extensions:
|
||||
if name.endswith(documents_extension) or name.endswith(documents_extension.upper()):
|
||||
move_file(dest_dir_documents, entry, name)
|
||||
logging.info(f"Moved document file: {name}")
|
||||
|
||||
|
||||
# ! NO NEED TO CHANGE BELOW CODE
|
||||
if __name__ == "__main__":
|
||||
logging.basicConfig(level=logging.INFO,
|
||||
format='%(asctime)s - %(message)s',
|
||||
@@ -68,7 +101,7 @@ if __name__ == "__main__":
|
||||
observer.start()
|
||||
try:
|
||||
while True:
|
||||
time.sleep(10)
|
||||
sleep(10)
|
||||
except KeyboardInterrupt:
|
||||
observer.stop()
|
||||
observer.join()
|
||||
observer.join()
|
||||
Reference in New Issue
Block a user