Go retrieves GPS data from the komoot app
Cowboys
This handy, if cowboyish, drilling method is wrapped up in the drill()
function starting in line 36 of Listing 5. It takes an array of keys, descends into the sub-hashmaps, and outputs the data found at the end of the keychain. Listing 5 collects the numeric IDs of all the tours the user has in their account. These can be tours planned on a virtual drawing board – which the user has added to the map with komoot's idiosyncratic browser interface – as well as completed hikes that the user has already taken and recorded with the app or even another GPS tracker.
From JSON to GPX
Using the IDs, the main program, supported by the kTour()
function in Listing 4, can then fetch the data of individual tours from komoot. Finally, Listing 6 converts the fetched JSON data into GPX format, which popular trackers from Garmin and others understand, and which komoot also accepts when you are uploading new tours. This is the restore part of the backup solution. The result of the conversion is shown in Figure 6 in the XML typical of GPX, while Figure 7 shows that komoot accepts the downloaded data (after conversion to GPX) as a new tour, without any complaints.
However, converting Go data to XML in a prescriptive manner, much like the JSON conversion before it, requires a huge number of type
declarations linking its sub-elements in the various layers. Ultimately, the Go code would have to declare the entire GPX syntax. The structure could then be populated with the parsed data and turned into GPX-compliant XML using the encoding/xml package. To make things simpler – if also cowboyish – Listing 6 simply writes the XML as a parameterized text string instead.
Lines 13 through 20 of Listing 6 again use the drill()
function defined in Listing 5, which works its way into the sub-hashmaps of the unpacked JSON data. Listing 6 saves the geo-coordinates of the tour in line 19 in the items
array slice after a type assertion confirming that it is an array of unknown content ([]interface{}
).
Timing
The timestamps of the recorded waypoints are a special case because the JSON format in komoot lists the start time of a tour once, only at the beginning, in RFC 3339 format. Komoot then gives the individual times of the track points each in thousandths of a second relative to the start time. Unfortunately, the GPX format lists the respective absolute time for each waypoint, meaning that Listing 6 has to do some math.
Line 21 reads the time found in the tour start field and converts it to Go's internal time format. While the for
loop then rattles through the excavated track points starting in line 27, line 29 divides the time delta found there in milliseconds by 1,000 and calls Add()
to add the resulting seconds value to the start time in line 30. The result is the timestamp for the respective waypoint, which line 37 converts back into RFC 3339 format and injects into the XML as a string.
Added to this are the entries for lat
(latitude) and lng
(longitude, but lon
in XML), which are present as generic interface{}
s but are converted to a float64 format by the %f
placeholder of the Sprintf()
function via type assertion. The same applies to the alt
(altitude) above sea level, which goes into the ele
(elevation) field of the GPX format. What the main program gets back from toGpx()
is a byte array containing the XML data, which it writes to the backup file on disc and that completes the backup. If you upload the generated .gpx
file again by way of a test, you will see with growing enthusiasm that komoot recognizes it as a new tour just as if you had walked it: a perfect backup solution.
It must be said that the disadvantage of officially unmaintained web scrapers such as this one is that even the smallest layout changes to the provider's website can break the program and would require small adaptations. You have to live with that. But maybe komoot will eventually take pity and decide to grant hobbyists access to the API and register an OAuth2 client ID. That would be much cleaner.
Infos
- "Get Komoot tour data without API": https://python.plainenglish.io/get-komoot-tour-data-without-api-143df64e51fa
- GPX format: https://en.wikipedia.org/wiki/GPS_Exchange_Format
- Listings for this article: ftp://ftp.linux-magazine.com/pub/listings/linux-magazine.com/252/
- "Programming Snapshot – Colly" by Mike Schilli, Linux Magazine, issue 223, June 2019, https://www.linux-magazine.com/Issues/2019/223/Data-Scraper/(language)/eng-US
« Previous 1 2 3
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Wine 10 Includes Plenty to Excite Users
With its latest release, Wine has the usual crop of bug fixes and improvements, along with some exciting new features.
-
Linux Kernel 6.13 Offers Improvements for AMD/Apple Users
The latest Linux kernel is now available, and it includes plenty of improvements, especially for those who use AMD or Apple-based systems.
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.