Created
May 7, 2014 18:51
-
-
Save mvexel/375236658e47cb651b2b to your computer and use it in GitHub Desktop.
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
def get_current_state_from_server(): | |
# I dislike PyYAML, that is why | |
import requests | |
from dateutil.parser import parse | |
current_state_file_url =\ | |
"http://planet.openstreetmap.org/replication/changesets/state.yaml" | |
state_file = requests.get(current_state_file_url).text | |
current_state = {} | |
for line in state_file.split('\n'): | |
elems = line.split(':') | |
if len(elems) > 1: | |
current_state[elems[0].strip()] = ":".join(elems[1:]).strip() | |
if not 'last_run' and 'sequence' in current_state: | |
return {} | |
current_state['sequence'] = int(current_state['sequence']) | |
current_state['last_run'] = parse(current_state['last_run']) | |
return current_state | |
def get_changeset_path_for(utctime): | |
import os | |
from math import floor | |
current_state = get_current_state_from_server() | |
if 'last_run' not in current_state: | |
return "" | |
parts = [] | |
minutes = int(floor( | |
(current_state['last_run'] - utctime).total_seconds() / 60)) | |
rem = current_state['sequence'] - minutes | |
if rem < 0: | |
rem = 0 | |
while rem > 0: | |
parts.insert(0, (str(rem % 1000).zfill(3))) | |
rem = (rem - rem % 1000) / 1000 | |
while len(parts) < 3: | |
parts.insert(0, "000") | |
parts.insert(0, "http://planet.osm.org/replication/changesets/") | |
print parts | |
return '{path}.osm.gz'.format(path=os.path.join(*parts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment