|
8 | 8 | import csv |
9 | 9 | import geojson |
10 | 10 | import logging |
| 11 | +import re |
11 | 12 | from datetime import datetime |
12 | 13 | from shapely.geometry import Polygon, Point |
13 | 14 | from io import StringIO |
@@ -138,6 +139,70 @@ def get(self, query, responseformat="geojson", verbosity="body", build=True, dat |
138 | 139 | # construct geojson |
139 | 140 | return self._as_geojson(response["elements"]) |
140 | 141 |
|
| 142 | + @staticmethod |
| 143 | + def _api_status() -> dict: |
| 144 | + """ |
| 145 | + :returns: dict describing the client's status with the API |
| 146 | + """ |
| 147 | + endpoint = "https://overpass-api.de/api/status" |
| 148 | + |
| 149 | + r = requests.get(endpoint) |
| 150 | + lines = tuple(r.text.splitlines()) |
| 151 | + |
| 152 | + available_re = re.compile(r'\d(?= slots? available)') |
| 153 | + available_slots = int( |
| 154 | + available_re.search(lines[3]).group() |
| 155 | + if available_re.search(lines[3]) |
| 156 | + else 0 |
| 157 | + ) |
| 158 | + |
| 159 | + waiting_re = re.compile(r'(?<=Slot available after: )[\d\-TZ:]{20}') |
| 160 | + waiting_slots = tuple( |
| 161 | + datetime.strptime( |
| 162 | + waiting_re.search(line).group(), "%Y-%m-%dT%H:%M:%S%z" |
| 163 | + ) |
| 164 | + for line in lines if waiting_re.search(line) |
| 165 | + ) |
| 166 | + |
| 167 | + current_idx = next( |
| 168 | + i for i, word in enumerate(lines) |
| 169 | + if word.startswith('Currently running queries') |
| 170 | + ) |
| 171 | + running_slots = tuple(tuple(line.split()) for line in lines[current_idx + 1:]) |
| 172 | + running_slots_datetimes = tuple( |
| 173 | + datetime.strptime( |
| 174 | + slot[3], "%Y-%m-%dT%H:%M:%S%z" |
| 175 | + ) |
| 176 | + for slot in running_slots |
| 177 | + ) |
| 178 | + |
| 179 | + return { |
| 180 | + "available_slots": available_slots, |
| 181 | + "waiting_slots": waiting_slots, |
| 182 | + "running_slots": running_slots_datetimes, |
| 183 | + } |
| 184 | + |
| 185 | + @property |
| 186 | + def slots_available(self) -> int: |
| 187 | + """ |
| 188 | + :returns: count of open slots the client has on the server |
| 189 | + """ |
| 190 | + return self._api_status()["available_slots"] |
| 191 | + |
| 192 | + @property |
| 193 | + def slots_waiting(self) -> tuple: |
| 194 | + """ |
| 195 | + :returns: tuple of datetimes representing waiting slots and when they will be available |
| 196 | + """ |
| 197 | + return self._api_status()["waiting_slots"] |
| 198 | + |
| 199 | + @property |
| 200 | + def slots_running(self) -> tuple: |
| 201 | + """ |
| 202 | + :returns: tuple of datetimes representing running slots and when they will be freed |
| 203 | + """ |
| 204 | + return self._api_status()["running_slots"] |
| 205 | + |
141 | 206 | def search(self, feature_type, regex=False): |
142 | 207 | """Search for something.""" |
143 | 208 | raise NotImplementedError() |
|
0 commit comments