Skip to content

Instantly share code, notes, and snippets.

@oripwk
Created November 26, 2022 10:34
Show Gist options
  • Save oripwk/2d08556725963327a7c4203ef07678bf to your computer and use it in GitHub Desktop.
Save oripwk/2d08556725963327a7c4203ef07678bf to your computer and use it in GitHub Desktop.
Extracts sights information from Lonely Planet EPUB books and generates a KML file which then can be imported to a map software
let pois = document.querySelectorAll(".poi")
for (poi of pois) {
try {
let link = poi.querySelectorAll(".map-links")[0]
.querySelectorAll(".link_map")[1].parentElement.getAttribute("href");
let coordinatesText = new URL(link).searchParams.get('q');
let coordinatesList = coordinatesText.split(",")
let coordinates = `${coordinatesList[1]},${coordinatesList[0]}`;
let info = poi.querySelectorAll(".poi-line")[0].querySelectorAll(".name");
let name = info[0].firstChild.data
let type = info[0].querySelectorAll(".subtype")[0].innerText
let elPlacemark = document.createElement("Placemark")
let elName = document.createElement("name")
elName.textContent = `(${type}) ${name}`;
let elPoint = document.createElement("Point")
let elCoordinates = document.createElement("coordinates")
elCoordinates.textContent = `${coordinates},0`;
elPoint.appendChild(elCoordinates);
elPlacemark.appendChild(elName);
elPlacemark.appendChild(elPoint)
console.log(elPlacemark.outerHTML)
} catch (error) {
}
}
@JeckPeppinger
Copy link

Thanks, sounds great. Please can you explain how to execute / run this code on windows installations?

I played around an hour but cannot find a way to execute a JS with windows script host, or java, or cmd / power shell, or in firefox or chrome.... sorry, not the expert for that. Any hint would be fine. Thx.

@JeckPeppinger
Copy link

I did some work and now know how that works.

ePUB is a zipped file of xHTML files, which are the individual chapters of the eBook.
Extract and then find the chapters (xHTML files) that contain "Points of Interest".
Then edit the XHTML file and add the script in the body.
Open a browser, start the web developer tools (or similar) to see the "console" tab.
Now drop the scripted xHTML file in the browser and the console will fill with all the extracted kml placemark entries.
Copy these, remove any added text from the console (other output, or line/source numbers), and paste into a template empty kml file that has the header and footer of kml.
Thats it.
Note, the kml tags must for strange reason follow a capital scheme: name, Placemark, point, etc. Otherwise the file is unreadable for apps/tools.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment