I figured out it is possible to traverse the market navigation endpoint, but it takes about 13h to fetch all markets, because one can make only 30 calls per minute. Here is how it can be done in Python:
https://gist.github.com/mac133k/f15d9a998ac3b25f34886c0029775612
Market data does not include stock codes, but these can be found in instrument data. Instruments can be queried by epic and the endpoint accepts up to 50 epics at time, so at lease this part is faster than the market navigation.