import json import requests from argparse import ArgumentParser from pytimeparse.timeparse import timeparse base_url = "https://paste.heufneutje.net/" def upload(filename, sunset): with open(filename, "rb") as file: files = {"c": file} data = {} headers = {"Accept": "application/json"} sunset_seconds = timeparse(sunset) if sunset_seconds and sunset_seconds > 0: data["sunset"] = sunset_seconds return requests.post(base_url, files=files, data=data, headers=headers) def delete(uuid): headers = {"Accept": "application/json"} url = f"{base_url}{uuid}" return requests.delete(url, headers=headers) def try_load_json(myjson): try: return json.loads(myjson) except ValueError: return None if __name__ == '__main__': parser = ArgumentParser(prog="PBUploader", description="A tool for uploading to a pb instance.") group = parser.add_argument_group("Main options", f"The main options for interacting with {base_url}.") required_group = group.add_mutually_exclusive_group(required=True) required_group.add_argument("-f", "--filename", help="the file to upload") required_group.add_argument("-d", "--delete", help="uuid of the paste that should be deleted") group = parser.add_argument_group("Optional flags") group.add_argument("-s", "--sunset", default="1h", help="time to keep the file on the server (defaults to 1 hour)") group.add_argument("-v", "--verbose", action="store_true", help="if enabled the entire JSON result will be printed") args = parser.parse_args() if args.delete: response = delete(args.delete) else: response = upload(args.filename, args.sunset) json_data = try_load_json(response.content) if not json_data: print(response.content.decode()) elif args.verbose: print(json.dumps(json_data, indent=4)) elif response.status_code != 200: print(json_data["status"]) elif args.delete: print(json_data["status"]) else: print(json_data["url"])