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
-
Gnome 47.1 Released with a Few Fixes
The latest release of the Gnome desktop is all about fixing a few nagging issues and not about bringing new features into the mix.
-
System76 Unveils an Ampere-Powered Thelio Desktop
If you're looking for a new desktop system for developing autonomous driving and software-defined vehicle solutions. System76 has you covered.
-
VirtualBox 7.1.4 Includes Initial Support for Linux kernel 6.12
The latest version of VirtualBox has arrived and it not only adds initial support for kernel 6.12 but another feature that will make using the virtual machine tool much easier.
-
New Slimbook EVO with Raw AMD Ryzen Power
If you're looking for serious power in a 14" ultrabook that is powered by Linux, Slimbook has just the thing for you.
-
The Gnome Foundation Struggling to Stay Afloat
The foundation behind the Gnome desktop environment is having to go through some serious belt-tightening due to continued financial problems.
-
Thousands of Linux Servers Infected with Stealth Malware Since 2021
Perfctl is capable of remaining undetected, which makes it dangerous and hard to mitigate.
-
Halcyon Creates Anti-Ransomware Protection for Linux
As more Linux systems are targeted by ransomware, Halcyon is stepping up its protection.
-
Valve and Arch Linux Announce Collaboration
Valve and Arch have come together for two projects that will have a serious impact on the Linux distribution.
-
Hacker Successfully Runs Linux on a CPU from the Early ‘70s
From the office of "Look what I can do," Dmitry Grinberg was able to get Linux running on a processor that was created in 1971.
-
OSI and LPI Form Strategic Alliance
With a goal of strengthening Linux and open source communities, this new alliance aims to nurture the growth of more highly skilled professionals.