From 6c2f5819e8916034fe30fa0e679006c97f99df6d Mon Sep 17 00:00:00 2001 From: fz0x1 Date: Fri, 17 Jan 2025 19:19:47 +0100 Subject: [PATCH] 20250117.1737137987 --- global/scripts/bin/diary.py | 53 ++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 16 deletions(-) diff --git a/global/scripts/bin/diary.py b/global/scripts/bin/diary.py index 644536f..5a9720a 100755 --- a/global/scripts/bin/diary.py +++ b/global/scripts/bin/diary.py @@ -85,7 +85,7 @@ def convert_diary_date(date_str): return None -def fetch_data(url, headers, data=None): +def fetch_data(url, headers={}, data=None, rjson=True, log=True): logit = make_logger() method = "POST" if data else "GET" encoded_data = urllib.parse.urlencode(data).encode("utf-8") if data else None @@ -98,9 +98,11 @@ def fetch_data(url, headers, data=None): 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")) + response_data = response.read().decode("utf-8") + logit.info(response_data) if log else None + if not rjson: + return response_data + return json.loads(response_data) except Exception as e: logit.error(str(e)) raise @@ -279,7 +281,7 @@ def doctor(): 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]}") + print(f"There is no location info about {m[0]} - {m[1]}") def make_hash(file: Path): @@ -357,8 +359,9 @@ def insert(): "Usage: script.py insert [bulk|single (default)] 'content'" ) - if len(sys.argv) == 5 and sys.argv[3] != "single": - sys.exit("Invalid usage for bulk insert.") + # TODO is this really need? + # if len(sys.argv) == 5 and sys.argv[3] != "single": + # sys.exit("Invalid usage for bulk insert.") diary_name = sys.argv[2] insert_type = ( @@ -369,18 +372,24 @@ def insert(): conn = db_connection(diary_path) initialize_db(conn) + # generating and converting current time datenow = datetime.now(timezone.utc) datenow_timestamp = datenow.strftime("%Y-%m-%dT%H:%M:%SZ") metadata_id = insert_metadata(conn, make_tz_unixtime(datenow_timestamp)) + # fetching geo-data closest_entry = fetch_geo( metadata_id, make_tz_unixtime(datenow_timestamp), conn ) + # fetching weather data fetch_weather( metadata_id, closest_entry, make_tz_unixtime(datenow_timestamp), conn ) if insert_type == "single": + """ + Inserting a string from the terminal + """ content = shlex.quote(sys.argv[4]) if not content: print("There is no text") @@ -398,6 +407,9 @@ def insert(): raise elif insert_type == "bulk": + """ + Inserting entry from your editor + """ fd, temp_file_path = tempfile.mkstemp() os.close(fd) @@ -416,7 +428,9 @@ def insert(): check=True, ) except subprocess.CalledProcessError as e: - print(f"Error during bulk import: {e.stderr}") + print( + f"Error during bulk import: {e.stderr}, file: {temp_file_path}" + ) raise os.remove(temp_file_path) @@ -432,12 +446,19 @@ def insert(): if __name__ == "__main__": - if sys.argv[1] == "export": - export() - elif sys.argv[1] == "insert": - insert() - elif sys.argv[1] == "doctor": - doctor() + try: + fetch_data(url="https://google.com", rjson=False, log=False) + ... + except Exception as e: + print(f"Connection problem: {e}") + raise else: - print("Unknown command") - sys.exit(1) + if sys.argv[1] == "export": + export() + elif sys.argv[1] == "insert": + insert() + elif sys.argv[1] == "doctor": + doctor() + else: + print("Unknown command") + sys.exit(1)