You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.6 KiB
Python

11 months ago
from watchdog.events import FileSystemEventHandler
11 months ago
from watchdog.observers import Observer
import shutil
import os
9 months ago
from funcs import get_media_dimensions
media_dir = "media"
stories_dir = os.path.join(media_dir, "stories")
posts_dir = os.path.join(media_dir, "posts")
os.makedirs(stories_dir, exist_ok=True)
os.makedirs(posts_dir, exist_ok=True)
9 months ago
def is_story(width, height, tolerance=0.02):
if width == 0 or height == 0:
return False
ratio = min(width, height) / max(width, height)
return abs(ratio - (9 / 16)) <= (9 / 16 * tolerance)
9 months ago
def determine_post_type(filepath):
lower = filepath.lower()
if "posts" in lower:
return "posts"
9 months ago
try:
width, height = get_media_dimensions(filepath)
except Exception as e:
print(f"Error getting dimensions for {filepath}: {e}")
9 months ago
return None
return "stories" if is_story(width, height) else "posts"
11 months ago
class DownloadHandler(FileSystemEventHandler):
def process_file(self, file_path):
file = os.path.basename(file_path)
# Ignore incomplete or weird temp names
if "crdownload" in file or file.count("~") != 3:
9 months ago
return
if not os.path.exists(file_path):
9 months ago
return
post_type = determine_post_type(file_path)
if post_type == "posts":
dest_dir = posts_dir
elif post_type == "stories":
dest_dir = stories_dir
6 months ago
else:
print(f"Could not determine post type for {file}. Skipping...")
return
9 months ago
output_path = os.path.join(dest_dir, file)
3 months ago
if os.path.exists(output_path):
print(f"File already exists {output_path}. Removing...")
os.remove(file_path)
3 months ago
return
9 months ago
shutil.move(file_path, output_path)
print(f"Moved {file_path}{output_path}")
11 months ago
def on_created(self, event):
if not event.is_directory:
11 months ago
self.process_file(event.src_path)
def on_moved(self, event):
if not event.is_directory:
11 months ago
self.process_file(event.dest_path)
11 months ago
if __name__ == "__main__":
download_path = os.path.join(os.path.expanduser("~"), "Downloads")
11 months ago
event_handler = DownloadHandler()
# Initial scan for files already in Downloads
for f in os.listdir(download_path):
full_path = os.path.join(download_path, f)
if os.path.isfile(full_path):
event_handler.process_file(full_path)
11 months ago
observer = Observer()
observer.schedule(event_handler, download_path, recursive=False)
11 months ago
observer.start()
try:
observer.join()
11 months ago
except KeyboardInterrupt:
observer.stop()
observer.join()