Add random and delete switches.

This commit is contained in:
Fred Boniface 2023-11-17 12:12:06 +00:00
parent e912b72382
commit cb7e0802c2

View File

@ -1,17 +1,23 @@
import load_config import load_config
import image_processing 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(): def main():
if len(sys.argv) < 2: args = parse_arguments()
print("Usage: social-photos <directory_or_file_path>", file=sys.stderr)
sys.exit(1)
else:
path = sys.argv[1]
# Check whether path points to a file or a directory. If neither, exit. 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): if not os.path.exists(path):
print(f"'{path}' does not exist.", file=sys.stderr) print(f"'{path}' does not exist.", file=sys.stderr)
sys.exit(1) sys.exit(1)
@ -25,6 +31,14 @@ def main():
print(f"'{path}' is neither a file nor a directory.", file=sys.stderr) print(f"'{path}' is neither a file nor a directory.", file=sys.stderr)
sys.exit(1) 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)
selected_file = random.choice(files)
files = [selected_file]
print("These files will be uploaded:") print("These files will be uploaded:")
for item in files: for item in files:
print(item) print(item)
@ -34,6 +48,12 @@ def main():
print("Cancelling operation") print("Cancelling operation")
sys.exit(0) 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 = [] file_data = []
for item in files: for item in files:
item_data = image_processing.get_image_data(item) item_data = image_processing.get_image_data(item)
@ -64,10 +84,21 @@ def main():
import boost_mastodon import boost_mastodon
posts['mastodon_boost'] = boost_mastodon.boost(posts['pixelfed']) posts['mastodon_boost'] = boost_mastodon.boost(posts['pixelfed'])
if delete_mode:
delete_files(files)
def list_files_in_directory(directory): def list_files_in_directory(directory):
top_level_items = os.listdir(directory) top_level_items = os.listdir(directory)
full_paths = [os.path.join(directory, item) for item in top_level_items] full_paths = [os.path.join(directory, item) for item in top_level_items]
return full_paths 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__": if __name__ == "__main__":
main() main()