From d6951204ef6cdbd53775b19cc331b4d529428dee Mon Sep 17 00:00:00 2001 From: Niklas Halle Date: Sun, 26 Jul 2020 13:46:20 +0200 Subject: Added "year" entry to local state, used for resetting the id on year change, as well as tagging the document accordingly Also reworked the output a bit --- .gitignore | 2 ++ config.ini | 4 +++- main.py | 46 +++++++++++++++++++++++++++++++++++----------- prepare_paperless | 20 ++++++++++++++------ shrink_pdf | 8 +++++++- 5 files changed, 61 insertions(+), 19 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4e0bbdf --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +venv/ +.idea/ diff --git a/config.ini b/config.ini index 7e0fb29..a142c3b 100644 --- a/config.ini +++ b/config.ini @@ -4,4 +4,6 @@ remote_path = /home/niklas/docker/paperless/consume/ user = niklas [STATE] -current_id = 101 \ No newline at end of file +current_id = 101 +year = 2020 + diff --git a/main.py b/main.py index f47af9a..64602d6 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,7 @@ from configparser import ConfigParser def usage(): - print(os.path.basename(sys.argv[0]) + "[-h|--help] [-s|--skip] [-t|--title title] [-c|--correspondent " + print(os.path.basename(sys.argv[0]) + " [-h|--help] [-t|--title title] [-c|--correspondent " "correspondent] [--tags tags] file") print(" -h|--help prints this help and exits") print(" -t|--title title to give to the file") @@ -26,17 +26,26 @@ def read_config(): sever_info = config_object["SERVER_CONFIG"] state = config_object["STATE"] - return sever_info["remote"], sever_info["remote_path"], sever_info["user"], state["current_id"] + # if the year in the config doesn't match the current one, reset id back to 1 + year = state["year"] -def update_id(new_id): + file_id = state["current_id"] + + if str(year) != str(datetime.now().strftime("%Y")): + file_id = 1 + + return sever_info["remote"], sever_info["remote_path"], sever_info["user"], file_id + + +def update_state(new_id): config_object = ConfigParser() config_object.read(os.environ.get('XDG_CONFIG_HOME') + "/paperless_upload.ini") state = config_object["STATE"] - # Update the password state["current_id"] = str(new_id) + state["year"] = str(datetime.now().strftime("%Y")) # Write changes back to file with open(os.environ.get('XDG_CONFIG_HOME') + "/paperless_upload.ini", 'w') as conf: @@ -78,25 +87,40 @@ def upload(remote, remote_path, username, file_id, path, title, correspondent, t # current date and time now = datetime.now() zulu_format = "%Y%m%d%H%M%S" - time1 = now.strftime(zulu_format) + year_format = "%Y" + time_stamp = now.strftime(zulu_format) + check_in_year = now.strftime(year_format) # https://paperless.readthedocs.io/en/latest/guesswork.html?highlight=guesswork # --> "Date - Correspondent - Title - tag,tag,tag.pdf" - guess_work_name = time1 + "Z - " + correspondent + " - " + title + prefix = f"{time_stamp}Z - {correspondent} - {title} - id{file_id},y{check_in_year}" if tags: - guess_work_name = guess_work_name + " - id" + str(file_id) + "," + tags + ".pdf" + guess_work_name = f"{prefix},{tags}.pdf" else: - guess_work_name = guess_work_name + " - id" + str(file_id) + ".pdf" + guess_work_name = f"{prefix}.pdf" + + print("") + print(f" #######################################") + print(f" # file the document \"{title}\" with the following infos") + print(f" # year: {check_in_year}") + print(f" # id: {file_id}") + print(f" # tags: {tags}") + print(f" #######################################") remote_file = (remote_path + guess_work_name).replace(" ", "\\\\ ") command = f"scp {path} {username}@{remote}:\"{remote_file}\"" - print("# use the following command to upload:") + print("") + print(" #######################################") + print(" # use the following command to upload") + print("") print(command) + print("") + print(" #######################################") def main(): try: - opts, args = getopt.getopt(sys.argv[1:], "ht:c:", ["help", "title", "correspondent", "tags"]) + opts, args = getopt.getopt(sys.argv[1:], "ht:c:a:", ["help", "title=", "correspondent=", "tags="]) except getopt.GetoptError as err: # print help information and exit: print(err) # will print something like "option -a not recognized" @@ -137,7 +161,7 @@ def main(): traceback.print_exc() sys.exit(5) else: - update_id(int(file_id) + 1) + update_state(int(file_id) + 1) if __name__ == "__main__": diff --git a/prepare_paperless b/prepare_paperless index eead1c7..5276ce1 100755 --- a/prepare_paperless +++ b/prepare_paperless @@ -1,13 +1,21 @@ #!/bin/env bash -if [ -z "$1" ]; then +filename="${1}" + +if [ -z "${filename}" ]; then echo please provide a file name exit 1 fi -echo "scanning document" -hp-scan --adf -s pdf --size a4 -o "${1}.pdf" +echo +echo " #######################" +echo " # scanning document #" +echo " #######################" +hp-scan --adf -s pdf --size a4 -o "${filename}.pdf" -echo "shrinking pdf" -shrink_pdf "${1}.pdf" -mv "small_${1}.pdf" "${1}.pdf" +echo +echo " #######################" +echo " # shrinking pdf #" +echo " #######################" +shrink_pdf "${filename}.pdf" +mv "small_${filename}.pdf" "${filename}.pdf" diff --git a/shrink_pdf b/shrink_pdf index 66347c3..9f06e30 100755 --- a/shrink_pdf +++ b/shrink_pdf @@ -1,3 +1,9 @@ #!/bin/env bash +filename="${1}" -ps2pdf -dPDFSETTINGS=/ebook "${1}" "small_${1}" +if [ -z "${filename}" ]; then + echo please provide a file name + exit 1 +fi + +ps2pdf -dPDFSETTINGS=/ebook "${filename}" "small_${filename}" -- cgit v1.2.3-54-g00ecf