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
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
|
||
|
|
|
||
|
4 weeks ago
|
media_dir = "media"
|
||
|
|
stories_dir = os.path.join(media_dir, "stories")
|
||
|
|
posts_dir = os.path.join(media_dir, "posts")
|
||
|
4 weeks ago
|
|
||
|
4 weeks ago
|
os.makedirs(stories_dir, exist_ok=True)
|
||
|
|
os.makedirs(posts_dir, exist_ok=True)
|
||
|
4 weeks ago
|
|
||
|
|
|
||
|
9 months ago
|
def is_story(width, height, tolerance=0.02):
|
||
|
|
if width == 0 or height == 0:
|
||
|
|
return False
|
||
|
4 weeks ago
|
ratio = min(width, height) / max(width, height)
|
||
|
|
return abs(ratio - (9 / 16)) <= (9 / 16 * tolerance)
|
||
|
|
|
||
|
9 months ago
|
|
||
|
|
def determine_post_type(filepath):
|
||
|
4 weeks ago
|
lower = filepath.lower()
|
||
|
4 weeks ago
|
if "posts" in lower:
|
||
|
|
return "posts"
|
||
|
9 months ago
|
try:
|
||
|
|
width, height = get_media_dimensions(filepath)
|
||
|
4 weeks ago
|
except Exception as e:
|
||
|
|
print(f"Error getting dimensions for {filepath}: {e}")
|
||
|
9 months ago
|
return None
|
||
|
4 weeks ago
|
return "stories" if is_story(width, height) else "posts"
|
||
|
|
|
||
|
11 months ago
|
|
||
|
|
class DownloadHandler(FileSystemEventHandler):
|
||
|
|
def process_file(self, file_path):
|
||
|
4 weeks ago
|
file = os.path.basename(file_path)
|
||
|
4 weeks ago
|
|
||
|
3 weeks ago
|
# Ignore incomplete or weird temp names
|
||
|
4 weeks ago
|
if "crdownload" in file or file.count("~") != 3:
|
||
|
9 months ago
|
return
|
||
|
|
|
||
|
4 weeks ago
|
if not os.path.exists(file_path):
|
||
|
9 months ago
|
return
|
||
|
|
|
||
|
|
post_type = determine_post_type(file_path)
|
||
|
4 weeks ago
|
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
|
|
||
|
4 weeks ago
|
output_path = os.path.join(dest_dir, file)
|
||
|
3 months ago
|
|
||
|
4 weeks ago
|
if os.path.exists(output_path):
|
||
|
4 weeks ago
|
print(f"File already exists {output_path}. Removing...")
|
||
|
4 weeks ago
|
os.remove(file_path)
|
||
|
3 months ago
|
return
|
||
|
9 months ago
|
|
||
|
4 weeks ago
|
shutil.move(file_path, output_path)
|
||
|
4 weeks ago
|
print(f"Moved {file_path} → {output_path}")
|
||
|
11 months ago
|
|
||
|
|
def on_created(self, event):
|
||
|
4 weeks ago
|
if not event.is_directory:
|
||
|
11 months ago
|
self.process_file(event.src_path)
|
||
|
|
|
||
|
|
def on_moved(self, event):
|
||
|
4 weeks ago
|
if not event.is_directory:
|
||
|
11 months ago
|
self.process_file(event.dest_path)
|
||
|
|
|
||
|
4 weeks ago
|
|
||
|
11 months ago
|
if __name__ == "__main__":
|
||
|
4 weeks ago
|
download_path = os.path.join(os.path.expanduser("~"), "Downloads")
|
||
|
11 months ago
|
event_handler = DownloadHandler()
|
||
|
3 weeks ago
|
|
||
|
|
# 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()
|
||
|
4 weeks ago
|
observer.schedule(event_handler, download_path, recursive=False)
|
||
|
11 months ago
|
observer.start()
|
||
|
|
|
||
|
|
try:
|
||
|
4 weeks ago
|
observer.join()
|
||
|
11 months ago
|
except KeyboardInterrupt:
|
||
|
|
observer.stop()
|
||
|
4 weeks ago
|
observer.join()
|