From 0de118b032fe9f04ac59bf95586403bcde68cbb3 Mon Sep 17 00:00:00 2001 From: fz0x1 Date: Fri, 17 Jan 2025 19:00:26 +0100 Subject: [PATCH] 20250117.1737136826 --- global/.zshrc | 6 ++-- global/scripts/bin/diary.py | 58 +++++++++++++++++++++++++++++-------- 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/global/.zshrc b/global/.zshrc index c0f9099..6fa4fb9 100644 --- a/global/.zshrc +++ b/global/.zshrc @@ -49,7 +49,7 @@ ex () DIARY="default" -dis () +diary_string () { "${HOME:?}/scripts/bin/diary.py" insert "${DIARY:?}" single "$*" } @@ -125,9 +125,9 @@ alias relmacs="doom sync" ## diary # alias di="diary.sh" alias m2jrnl="diary.py export ${DIARY:?} diaryf" -alias di="diary.py insert" alias dib="diary.py insert ${DIARY:?} bulk" -alias dis=" dis" +alias dis=" diary_string" +alias dit="diary.py doctor ${DIARY:?}" # bindkeys ## autosuggest diff --git a/global/scripts/bin/diary.py b/global/scripts/bin/diary.py index ad08c88..644536f 100755 --- a/global/scripts/bin/diary.py +++ b/global/scripts/bin/diary.py @@ -3,6 +3,7 @@ import base64 import hashlib import json +import logging import os import re import shlex @@ -36,6 +37,17 @@ class Config: Config.validate() +def make_logger(): + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s - %(levelname)s - %(message)s", + handlers=[ + logging.FileHandler(Path.home() / ".diary_requests.log", mode="a"), + ], + ) + return logging.getLogger("urllib_logger") + + def get_diary_path_by_name(name: str): result = subprocess.run(["jrnl", "--list"], capture_output=True, text=True) if result.stderr: @@ -74,23 +86,38 @@ def convert_diary_date(date_str): def fetch_data(url, headers, data=None): + logit = make_logger() method = "POST" if data else "GET" encoded_data = urllib.parse.urlencode(data).encode("utf-8") if data else None req = urllib.request.Request(url, headers=headers, data=encoded_data, method=method) req.add_header("Content-Type", "application/x-www-form-urlencoded") - with urllib.request.urlopen(req) as response: - if response.status != 200: - sys.exit(f"HTTP error {response.status}") - return json.loads(response.read().decode("utf-8")) + try: + with urllib.request.urlopen(req) as response: + if response.status != 200: + logit.error(response.read()) + sys.exit(f"HTTP error {response.status}") + data = response.read() + logit.info(data.decode("utf-8")) + return json.loads(data.decode("utf-8")) + except Exception as e: + logit.error(str(e)) + raise def delete_entity(url, headers): + logit = make_logger() req = urllib.request.Request(url, headers=headers, method="DELETE") - with urllib.request.urlopen(req) as response: - if response.status != 200: - sys.exit(f"HTTP delete error {response.status}") + try: + with urllib.request.urlopen(req) as response: + if response.status != 200: + logit.error(response.read()) + sys.exit(f"HTTP delete error {response.status}") + logit.info(response.read().decode("utf-8")) + except Exception as e: + logit.error(str(e)) + raise def db_connection(diary_path: Path): @@ -230,7 +257,10 @@ def fetch_weather( def doctor(): - diary_name = sys.argv[1] + if len(sys.argv) < 3: + sys.exit("Usage: script.py doctor ") + + diary_name = sys.argv[2] diary_path = get_diary_path_by_name(diary_name).parent conn = db_connection(diary_path) @@ -246,7 +276,10 @@ def doctor(): "SELECT * FROM location WHERE metadata_id = ?", (m[0],) ).fetchall() - print(weather, location) + if not weather: + print(f"There is no weather info about {m[0]} - {m[1]}") + if not location: + print(f"There is no weather info about {m[0]} - {m[1]}") def make_hash(file: Path): @@ -399,11 +432,12 @@ def insert(): if __name__ == "__main__": - # doctor() - if "export" in sys.argv: + if sys.argv[1] == "export": export() - elif "insert" in sys.argv: + elif sys.argv[1] == "insert": insert() + elif sys.argv[1] == "doctor": + doctor() else: print("Unknown command") sys.exit(1)