From cb7e0802c28c57ea7e3d2778a5a075545c79f757 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Fri, 17 Nov 2023 12:12:06 +0000 Subject: [PATCH] Add random and delete switches. --- src/main.py | 131 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 81 insertions(+), 50 deletions(-) diff --git a/src/main.py b/src/main.py index 7f37aa6..5bc8808 100644 --- a/src/main.py +++ b/src/main.py @@ -1,73 +1,104 @@ import load_config import image_processing -import os, sys, time +import os, sys, time, argparse + +def parse_arguments(): + parser = argparse.ArgumentParser(description="Social Photos Uploader") + parser.add_argument('path', help="Directory or file path") + parser.add_argument('-r', '--random', action='store_true', help="Randomly select one photo to upload") + parser.add_argument('-d', '--delete', action='store_true', help="Delete the image file once uploaded") + return parser.parse_args() def main(): - if len(sys.argv) < 2: - print("Usage: social-photos ", file=sys.stderr) - sys.exit(1) - else: - path = sys.argv[1] + args = parse_arguments() - # Check whether path points to a file or a directory. If neither, exit. - - if not os.path.exists(path): - print(f"'{path}' does not exist.", file=sys.stderr) + path = args.path + random_mode = args.random + delete_mode = args.delete + + # Check path exists, whether file or directory and output a list of files + if not os.path.exists(path): + print(f"'{path}' does not exist.", file=sys.stderr) + sys.exit(1) + elif os.path.isfile(path): + print(f"'{path}' is a file.") + files = [path] + elif os.path.isdir(path): + print(f"'{path}' is a directory.") + files = list_files_in_directory(path) + else: + print(f"'{path}' is neither a file nor a directory.", file=sys.stderr) + sys.exit(1) + + # If random_mode, select random image from the files list and store it as the files list. + if random_mode: + if not files: + print("No files found", file=sys.stderr) sys.exit(1) - elif os.path.isfile(path): - print(f"'{path}' is a file.") - files = [path] - elif os.path.isdir(path): - print(f"'{path}' is a directory.") - files = list_files_in_directory(path) - else: - print(f"'{path}' is neither a file nor a directory.", file=sys.stderr) - sys.exit(1) - - print("These files will be uploaded:") - for item in files: - print(item) - confirmation = input("Proceed? (y/n): ").strip().lower() - - if confirmation != 'y': - print("Cancelling operation") + selected_file = random.choice(files) + files = [selected_file] + + print("These files will be uploaded:") + for item in files: + print(item) + confirmation = input("Proceed? (y/n): ").strip().lower() + + if confirmation != 'y': + print("Cancelling operation") + sys.exit(0) + + if delete_mode: + delete_confirm = input("Files will be PERMANENTLY DELETED once uploads are complete! Proceed? (y/n): ").strip().lower() + if delete_confirm != 'y': + print("Cancelling Operation") sys.exit(0) - file_data = [] - for item in files: - item_data = image_processing.get_image_data(item) - file_data.append(item_data) + file_data = [] + for item in files: + item_data = image_processing.get_image_data(item) + file_data.append(item_data) - # Collection of post URL's which can be used for sharing features - # Where the API used doesn't support responding with a URL, a status - # message is provided. - posts = {} + # Collection of post URL's which can be used for sharing features + # Where the API used doesn't support responding with a URL, a status + # message is provided. + posts = {} - if load_config.config['pixelfed']['enable']: - print("Pixelfed publishing enabled") - import publish_pixelfed - posts['pixelfed'] = publish_pixelfed.upload(file_data) + if load_config.config['pixelfed']['enable']: + print("Pixelfed publishing enabled") + import publish_pixelfed + posts['pixelfed'] = publish_pixelfed.upload(file_data) - if load_config.config['flickr']['enable']: - print("Flickr publishing enabled") - import publish_flickr - posts['flickr'] = publish_flickr.upload(file_data) + if load_config.config['flickr']['enable']: + print("Flickr publishing enabled") + import publish_flickr + posts['flickr'] = publish_flickr.upload(file_data) - ## Wait before boosing to ensure media has been processed - print("Waiting to ensure media has been uploaded and processed before boosting") - time.sleep(20) + ## Wait before boosing to ensure media has been processed + print("Waiting to ensure media has been uploaded and processed before boosting") + time.sleep(20) - if load_config.config['boost_mastodon']['enable']: - print("Mastodon Boost enabled") - import boost_mastodon - posts['mastodon_boost'] = boost_mastodon.boost(posts['pixelfed']) + if load_config.config['boost_mastodon']['enable']: + print("Mastodon Boost enabled") + import boost_mastodon + posts['mastodon_boost'] = boost_mastodon.boost(posts['pixelfed']) + + if delete_mode: + delete_files(files) def list_files_in_directory(directory): top_level_items = os.listdir(directory) full_paths = [os.path.join(directory, item) for item in top_level_items] return full_paths +def delete_files(files): + for file_path in files: + try: + os.remove(file_path) + print(f"Deleted: {file_path}") + except Exception as e: + print(f"Error deleting {file_path}: {e}", file=sys.stderr) + if __name__ == "__main__": main() \ No newline at end of file