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
|
return None
|
||||||
|
|
||||||
|
|
||||||
def fetch_data(url, headers, data=None):
|
def fetch_data(url, headers={}, data=None, rjson=True, log=True):
|
||||||
logit = make_logger()
|
logit = make_logger()
|
||||||
method = "POST" if data else "GET"
|
method = "POST" if data else "GET"
|
||||||
encoded_data = urllib.parse.urlencode(data).encode("utf-8") if data else None
|
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:
|
if response.status != 200:
|
||||||
logit.error(response.read())
|
logit.error(response.read())
|
||||||
sys.exit(f"HTTP error {response.status}")
|
sys.exit(f"HTTP error {response.status}")
|
||||||
data = response.read()
|
response_data = response.read().decode("utf-8")
|
||||||
logit.info(data.decode("utf-8"))
|
logit.info(response_data) if log else None
|
||||||
return json.loads(data.decode("utf-8"))
|
if not rjson:
|
||||||
|
return response_data
|
||||||
|
return json.loads(response_data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logit.error(str(e))
|
logit.error(str(e))
|
||||||
raise
|
raise
|
||||||
|
@ -279,7 +281,7 @@ def doctor():
|
||||||
if not weather:
|
if not weather:
|
||||||
print(f"There is no weather info about {m[0]} - {m[1]}")
|
print(f"There is no weather info about {m[0]} - {m[1]}")
|
||||||
if not location:
|
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):
|
def make_hash(file: Path):
|
||||||
|
@ -357,8 +359,9 @@ def insert():
|
||||||
"Usage: script.py insert <diary_name> [bulk|single (default)] 'content'"
|
"Usage: script.py insert <diary_name> [bulk|single (default)] 'content'"
|
||||||
)
|
)
|
||||||
|
|
||||||
if len(sys.argv) == 5 and sys.argv[3] != "single":
|
# TODO is this really need?
|
||||||
sys.exit("Invalid usage for bulk insert.")
|
# if len(sys.argv) == 5 and sys.argv[3] != "single":
|
||||||
|
# sys.exit("Invalid usage for bulk insert.")
|
||||||
|
|
||||||
diary_name = sys.argv[2]
|
diary_name = sys.argv[2]
|
||||||
insert_type = (
|
insert_type = (
|
||||||
|
@ -369,18 +372,24 @@ def insert():
|
||||||
conn = db_connection(diary_path)
|
conn = db_connection(diary_path)
|
||||||
initialize_db(conn)
|
initialize_db(conn)
|
||||||
|
|
||||||
|
# generating and converting current time
|
||||||
datenow = datetime.now(timezone.utc)
|
datenow = datetime.now(timezone.utc)
|
||||||
datenow_timestamp = datenow.strftime("%Y-%m-%dT%H:%M:%SZ")
|
datenow_timestamp = datenow.strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||||
metadata_id = insert_metadata(conn, make_tz_unixtime(datenow_timestamp))
|
metadata_id = insert_metadata(conn, make_tz_unixtime(datenow_timestamp))
|
||||||
|
|
||||||
|
# fetching geo-data
|
||||||
closest_entry = fetch_geo(
|
closest_entry = fetch_geo(
|
||||||
metadata_id, make_tz_unixtime(datenow_timestamp), conn
|
metadata_id, make_tz_unixtime(datenow_timestamp), conn
|
||||||
)
|
)
|
||||||
|
# fetching weather data
|
||||||
fetch_weather(
|
fetch_weather(
|
||||||
metadata_id, closest_entry, make_tz_unixtime(datenow_timestamp), conn
|
metadata_id, closest_entry, make_tz_unixtime(datenow_timestamp), conn
|
||||||
)
|
)
|
||||||
|
|
||||||
if insert_type == "single":
|
if insert_type == "single":
|
||||||
|
"""
|
||||||
|
Inserting a string from the terminal
|
||||||
|
"""
|
||||||
content = shlex.quote(sys.argv[4])
|
content = shlex.quote(sys.argv[4])
|
||||||
if not content:
|
if not content:
|
||||||
print("There is no text")
|
print("There is no text")
|
||||||
|
@ -398,6 +407,9 @@ def insert():
|
||||||
raise
|
raise
|
||||||
|
|
||||||
elif insert_type == "bulk":
|
elif insert_type == "bulk":
|
||||||
|
"""
|
||||||
|
Inserting entry from your editor
|
||||||
|
"""
|
||||||
fd, temp_file_path = tempfile.mkstemp()
|
fd, temp_file_path = tempfile.mkstemp()
|
||||||
os.close(fd)
|
os.close(fd)
|
||||||
|
|
||||||
|
@ -416,7 +428,9 @@ def insert():
|
||||||
check=True,
|
check=True,
|
||||||
)
|
)
|
||||||
except subprocess.CalledProcessError as e:
|
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
|
raise
|
||||||
|
|
||||||
os.remove(temp_file_path)
|
os.remove(temp_file_path)
|
||||||
|
@ -432,6 +446,13 @@ def insert():
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
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":
|
if sys.argv[1] == "export":
|
||||||
export()
|
export()
|
||||||
elif sys.argv[1] == "insert":
|
elif sys.argv[1] == "insert":
|
||||||
|
|
Loading…
Add table
Reference in a new issue