Last active
September 23, 2018 02:09
-
-
Save TimSC/cd2292b35971e1672622fc6946e9f205 to your computer and use it in GitHub Desktop.
Sync freestreetmap replication data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import requests | |
| if __name__=="__main__": | |
| interval = "day" | |
| pth = "/home/tim/replicatefsm" | |
| #Check what already exists | |
| fiList = os.listdir(pth) | |
| max1 = -1 | |
| for fina in fiList: | |
| try: | |
| finaNum = int(fina) | |
| except ValueError: | |
| continue | |
| if finaNum > max1: | |
| max1 = finaNum | |
| print ("max1", max1) | |
| fiList = os.listdir(os.path.join(pth, "{:03d}".format(max1))) | |
| max2 = 0 | |
| for fina in fiList: | |
| try: | |
| finaNum = int(fina) | |
| except ValueError: | |
| continue | |
| if finaNum > max2: | |
| max2 = finaNum | |
| print ("max2", max2) | |
| if max2 > 999: | |
| max1 += 1 | |
| max2 = 0 | |
| pth2 = os.path.join(pth, "{:03d}".format(max1),"{:03d}".format(max2)) | |
| if not os.path.exists(pth2): | |
| os.makedirs(pth2) | |
| fiList = os.listdir(pth2) | |
| max3 = -1 | |
| for fina in fiList: | |
| try: | |
| finaNum = int(os.path.splitext(os.path.splitext(fina)[0])[0]) | |
| except ValueError: | |
| continue | |
| if finaNum > max3: | |
| max3 = finaNum | |
| print ("max3", max3) | |
| while True: | |
| max3 += 1 | |
| print (max1, max2, max3) | |
| if max3 > 999: | |
| max2 += 1 | |
| max3 = 0 | |
| if max2 > 999: | |
| max1 += 1 | |
| max2 = 0 | |
| pth2 = os.path.join(pth, "{:03d}".format(max1),"{:03d}".format(max2)) | |
| if not os.path.exists(pth2): | |
| os.makedirs(pth2) | |
| url = "http://freestreetmap.org/replication/{}/{:03d}/{:03d}/{:03d}.osc.gz".format(interval, max1, max2, max3) | |
| r = requests.get(url) | |
| if r.status_code != 200: | |
| print (r.status_code) | |
| break | |
| url = "http://freestreetmap.org/replication/{}/{:03d}/{:03d}/{:03d}.state.txt".format(interval, max1, max2, max3) | |
| r2 = requests.get(url) | |
| if r.status_code != 200: | |
| print (r2.status_code) | |
| break | |
| fi = open(os.path.join(pth, "{:03d}/{:03d}/{:03d}.osc.gz".format(max1, max2, max3)), "wb") | |
| fi.write(r.content) | |
| fi.close() | |
| fi2 = open(os.path.join(pth, "{:03d}/{:03d}/{:03d}.state.txt".format(max1, max2, max3)), "wb") | |
| fi2.write(r2.content) | |
| fi2.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment