Skip to content

Commit

Permalink
fix: data request from api is now included in the main script
Browse files Browse the repository at this point in the history
  • Loading branch information
ObsidianPresidium committed Feb 26, 2024
1 parent 0f5c7c9 commit 11422d1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 35 deletions.
7 changes: 7 additions & 0 deletions dmi/dmi_func.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import requests
import json
import time
from dmi_get_municipalities import get_municipalities
from dmi_get_municipality_name import get_municipality_name
from PIL import Image, ImageTk
import threading

Expand Down Expand Up @@ -389,3 +391,8 @@ def save_as():
latest_image.save(new_filename, "PNG")
else:
tkinter.messagebox.showerror(window_name, message="Intet billede er tegnet endnu", parent=main_window)

def ensure_data_files():
if not os.path.exists("municipalities_by_id.json") or not os.path.exists("municipalities_unformatted.json"):
get_municipalities()
get_municipality_name()
24 changes: 14 additions & 10 deletions dmi/dmi_get_municipalities.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import requests

url = f"https://boundaries-api.io/api/denmark/geometry/find/level/"
params = {
"level": 2
}
response = requests.get(url, params=params)
def get_municipalities():
url = f"https://boundaries-api.io/api/denmark/geometry/find/level/"
params = {
"level": 2
}
response = requests.get(url, params=params)

print(response.status_code)
print(response.reason)
print("Writing data..")
with open("municipalities_by_id.json", "w") as f:
f.write(response.text)
print(response.status_code)
print(response.reason)
print("Writing data..")
with open("municipalities_by_id.json", "w") as f:
f.write(response.text)

if __name__ == "__main__":
get_municipalities()
54 changes: 29 additions & 25 deletions dmi/dmi_get_municipality_name.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import json
import requests

json_file = "municipalities_by_id.json"
with open(json_file, "r", encoding="utf-8") as f:
my_dict = json.loads("".join(f.readlines()))
def get_municipality_name():
json_file = "municipalities_by_id.json"
with open(json_file, "r", encoding="utf-8") as f:
my_dict = json.loads("".join(f.readlines()))

ids = ""
ids = ""

for municipality in my_dict["geometries"]["features"]:
ids += municipality["properties"]["id"] + ","
for municipality in my_dict["geometries"]["features"]:
ids += municipality["properties"]["id"] + ","

ids = ids[:-1]
print(ids)
ids = ids[:-1]
print(ids)

url = f"https://boundaries-api.io/api/denmark/attributes/find/id/"
params = {
"id": ids
}
url = f"https://boundaries-api.io/api/denmark/attributes/find/id/"
params = {
"id": ids
}

response = requests.get(url, params=params)
response = requests.get(url, params=params)

print(response.status_code)
print(response.reason)
print(response.status_code)
print(response.reason)

print("Writing data (municipalities_by_name)..")
with open("municipalities_by_name.json", "w") as f:
f.write(response.text)
print("Writing data (municipalities_by_name)..")
with open("municipalities_by_name.json", "w") as f:
f.write(response.text)

names = response.json()
with open("municipalities_by_id.json", "r") as id_file:
id_file_contents = id_file.read()
for municipality in names["attributes"]:
id_file_contents = id_file_contents.replace(municipality["id"], municipality["name"]["dan"])
with open("municipalities_unformatted.json", "w") as f:
f.write(id_file_contents)
names = response.json()
with open("municipalities_by_id.json", "r") as id_file:
id_file_contents = id_file.read()
for municipality in names["attributes"]:
id_file_contents = id_file_contents.replace(municipality["id"], municipality["name"]["dan"])
with open("municipalities_unformatted.json", "w") as f:
f.write(id_file_contents)

if __name__ == "__main__":
get_municipality_name()
2 changes: 2 additions & 0 deletions dmi/dmi_gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
row_height = 24



style = ttk.Style()
style.theme_use("default")
style.configure("Treeview", background=treeview_background, foreground=treeview_foreground, rowheight=row_height, fieldbackground=treeview_background)
style.map("Treeview", background=[("selected", treeview_selected)])

df.parseargs(sys.argv[1:])
df.ensure_data_files()
api_key = df.get_key()

progress = tk.IntVar()
Expand Down

0 comments on commit 11422d1

Please sign in to comment.