Last active
November 10, 2018 06:51
-
-
Save TimSC/fd59d4013b6af7dff43c15df99a96f44 to your computer and use it in GitHub Desktop.
Download osc diffs using custom pycrocosm api method
This file contains 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
#Download osc diffs using custom pycrocosm api method | |
#Released under CC0 license | |
import requests | |
import datetime | |
import os | |
import gzip | |
def DownloadDiffs(epoch = datetime.date(2016, 9, 27), | |
dt=datetime.timedelta(1), | |
outCount = 100000, | |
outPathBase = "diff", | |
apiDiffUrl = "http://api.fosm.org/replication/diff"): | |
cursor = epoch | |
while cursor < datetime.date.today(): | |
outPath = os.path.join(outPathBase, str(outCount // 1000)) | |
try: | |
os.makedirs(outPath) | |
except OSError as err: | |
if not os.path.exists(outPath): | |
raise err | |
outFina = os.path.join(outPath, "{:03d}.osc.gz".format(outCount % 1000)) | |
cursor2 = cursor + dt | |
if os.path.exists(outFina): | |
cursor = cursor2 | |
outCount += 1 | |
continue | |
url = apiDiffUrl+"?start={}&end={}".format(cursor, cursor2) | |
print (url) | |
r = requests.get(url) | |
if r.status_code != 200: | |
print ("Server responsed with code", r.status_code) | |
return -1 | |
outFi = gzip.open(outFina, "wb") | |
outFi.write(r.content) | |
outFi.close() | |
cursor = cursor2 | |
outCount += 1 | |
return 0 | |
if __name__=="__main__": | |
DownloadDiffs() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment