diff --git a/.gitignore b/.gitignore index f26db73..d6bfd79 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ build/ node_modules/ pages/ +scratch.html diff --git a/README.md b/README.md index 55738a5..c6d7a06 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ # BMLT Data Converter -* Converts BMLT meeting data from a JSON endpoint to CSV +* Converts BMLT meeting data from a JSON endpoint to CSV or KML + * Note that export of KML is only supported with GetSearchResults. \ No newline at end of file diff --git a/assets/js/main.js b/assets/js/main.js index 14085ab..aca5815 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -1,18 +1,35 @@ -function fetchMeetings(query, callback) { +let isProcessingCSV = false; +let isProcessingKML = false; + +const fetchMeetings = (query, callback, isCSV, isKML) => { const script = document.createElement("script"); - script.src = query + "&callback=" + callback.name; + script.src = `${query}&callback=${callback.name}`; document.body.appendChild(script); -} + isProcessingCSV = isCSV; + isProcessingKML = isKML; +}; + +const handleMeetingsData = (meetings) => { + if (isProcessingCSV) { + const csvContent = `data:text/csv;charset=utf-8,${encodeURIComponent( + convertToCSV(meetings), + )}`; + const downloadLink = document.getElementById("downloadLink"); + downloadLink.href = csvContent; + downloadLink.style.display = "block"; + } -function handleMeetingsData(meetings) { - const csvContent = - "data:text/csv;charset=utf-8," + encodeURIComponent(convertToCSV(meetings)); - const downloadLink = document.getElementById("downloadLink"); - downloadLink.href = csvContent; - downloadLink.style.display = "block"; -} + if (isProcessingKML) { + const kmlContent = `data:text/xml;charset=utf-8,${encodeURIComponent( + convertToKML(meetings), + )}`; + const kmlDownloadLink = document.getElementById("kmlDownloadLink"); + kmlDownloadLink.href = kmlContent; + kmlDownloadLink.style.display = "block"; + } +}; -function convertToCSV(data) { +const convertToCSV = (data) => { const csvRows = []; const keys = Object.keys(data[0]); @@ -35,14 +52,127 @@ function convertToCSV(data) { }); return csvRows.join("\n"); -} +}; + +const convertToKML = (data) => { + const kmlHeader = ` + + + + `; + + const kmlFooter = ` + +`; + + const placemarks = data.map((meeting) => { + const name = meeting["meeting_name"].trim() || "NA Meeting"; + const lng = parseFloat(meeting["longitude"]); + const lat = parseFloat(meeting["latitude"]); -function exportToCSV() { + if (lng || lat) { + const description = prepareSimpleLine(meeting); + const address = prepareSimpleLine(meeting, false); + + return ( + ` \n` + + ` ${name}\n` + + (address ? `
${address}
\n` : "") + + (description + ? ` ${description}\n` + : "") + + ` \n` + + ` ${lng},${lat}\n` + + ` \n` + + `
\n` + ); + } + }); + + return kmlHeader + placemarks.join("\n") + kmlFooter; +}; + +const exportData = () => { const query = document.getElementById("query").value; if (!query.includes("/client_interface/jsonp")) { alert("Invalid BMLT query URL, must use jsonp endpoint."); return; } + // Only support GetSearchResults for KML + const isKML = query.includes("GetSearchResults"); + fetchMeetings(query, handleMeetingsData, true, isKML); + if (isKML) { + fetchMeetings(query, handleMeetingsData, true, true); + } +}; + +const prepareSimpleLine = (meeting, withDate = true) => { + const getLocationInfo = () => { + const locationInfo = []; + const addInfo = (property) => { + if (property in meeting) { + const value = meeting[property].trim(); + if (value) { + locationInfo.push(value); + } + } + }; + + addInfo("location_text"); + addInfo("location_street"); + addInfo("location_city_subsection"); + addInfo("location_municipality"); + addInfo("location_neighborhood"); + addInfo("location_province"); + addInfo("location_postal_code_1"); + addInfo("location_nation"); + addInfo("location_info"); + + return locationInfo.join(", "); + }; + + const getDateString = () => { + const weekday_strings = [ + "All", + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", + ]; + const weekday = parseInt(meeting["weekday_tinyint"].trim()); + const weekdayString = weekday_strings[weekday]; + + const startTime = `2000-01-01 ${meeting["start_time"]}`; + const time = new Date(startTime); + + if (weekdayString && withDate) { + let dateString = weekdayString; - fetchMeetings(query, handleMeetingsData); -} + if (!isNaN(time)) { + dateString += `, ${time.toLocaleTimeString("en-US", { + hour: "numeric", + minute: "numeric", + hour12: true, + })}`; + } + + return dateString; + } + + return ""; + }; + + const locationInfo = getLocationInfo(); + const dateString = getDateString(); + + if (withDate && dateString && locationInfo) { + return `${dateString}, ${locationInfo}`; + } else if (dateString) { + return dateString; + } else { + return locationInfo; + } +}; diff --git a/index.html b/index.html index 24fcfd5..53db6fb 100644 --- a/index.html +++ b/index.html @@ -1,21 +1,21 @@ - BMLT CSV Export + BMLT Data Converter -

BMLT CSV Export

+

BMLT Data Converter


- + +
- - \ No newline at end of file +