20250117.1737137987
This commit is contained in:
parent
0de118b032
commit
6c2f5819e8
1 changed files with 37 additions and 16 deletions
|
@ -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 <diary_name> [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,6 +446,13 @@ def insert():
|
|||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
fetch_data(url="https://google.com", rjson=False, log=False)
|
||||
...
|
||||
except Exception as e:
|
||||
print(f"Connection problem: {e}")
|
||||
raise
|
||||
else:
|
||||
if sys.argv[1] == "export":
|
||||
export()
|
||||
elif sys.argv[1] == "insert":
|
||||
|
|
Loading…
Add table
Reference in a new issue