survey-extraction/etl/osmosis_google_maps_.py
2025-05-14 14:59:08 +00:00

108 lines
No EOL
3.4 KiB
Python

from monday import MondayClient
import json
import requests
import time
from tqdm import tqdm
board_id = "3584401309"
monday_key = "eyJhbGciOiJIUzI1NiJ9.eyJ0aWQiOjQ5ODc2ODQxOCwiYWFpIjoxMSwidWlkIjozNjE3ODAzNCwiaWFkIjoiMjAyNS0wNC0xMVQxMToyMzoxNy40NjdaIiwicGVyIjoibWU6d3JpdGUiLCJhY3RpZCI6MTM5OTc4MjMsInJnbiI6InVzZTEifQ.-2Lit4s46ZF6AXuMW9t0TxIaFLkHqD4Yo-PyM9i2XZY"
monday = MondayClient(monday_key)
def get_all_items(board_id, monday):
# Parameters
limit = 25 # Adjust the limit based on how many items you want per request
all_items = [] # List to store all fetched items
cursor = None # Start without a cursor for the first page
# Loop through pages
while True:
# Fetch items for the current page
response = monday.boards.fetch_items_by_board_id(
board_ids=board_id,
limit=limit,
cursor=cursor
)
items = response['data']['boards'][0]['items_page']['items']
# If no items are returned, stop the loop
if not items:
break
# Append items from this page to the all_items list
all_items.extend(items)
# Get the cursor for the next page (if there is one)
cursor = response['data']['boards'][0]['items_page'].get('cursor') # Get the current cursor
# If there's no cursor, we've reached the last page
if not cursor:
break
print(f"cursor {cursor}")
print(f"len all_itemms {len(all_items)}")
return all_items
def get_coords_from_address(address):
url = "https://nominatim.openstreetmap.org/search"
params = {
"q": address,
"format": "json",
"limit": 1
}
headers = {
"User-Agent": "YourAppNameHere"
}
response = requests.get(url, params=params, headers=headers)
data = response.json()
if data:
return {
"lat": float(data[0]["lat"]),
"lng": float(data[0]["lon"]),
"address": address
}
else:
return None
# Step 1: Fetch column IDs
board_data = monday.boards.fetch_boards_by_id(board_id)
columns = board_data["data"]["boards"][0]["columns"]
col_id_map = {col["title"].lower(): col["id"] for col in columns}
postcode_col_id = col_id_map.get("postcode") # Replace with actual title if different
location_col_id = col_id_map.get("location") # Replace with actual title if different
if not postcode_col_id or not location_col_id:
raise Exception("Could not find 'postcode' or 'location' columns")
items = get_all_items(board_id, monday)
for item in tqdm(items):
item_name = item["name"]
item_id = item["id"]
# Get postcode value
postcode = None
for val in item["column_values"]:
if val["id"] == postcode_col_id:
postcode = val["text"]
break
if postcode:
try:
location = get_coords_from_address(f"{item_name}, {postcode}, UK")
if location:
print(f"Updating '{item_name}' with coords {location}")
# Step 3: Update location field
monday.items.change_multiple_column_values(
board_id,
item_id,
{location_col_id: location},
)
except Exception as e:
print(f"Error updating item '{item_name}': {e}")
else:
print(f"No postcode found for item '{item_name}'")