Skip to content

Instantly share code, notes, and snippets.

@frafra frafra/osmstats.py
Last active Jun 28, 2018

Embed
What would you like to do?
OpenStreetMap "Top 500" from neis-one.org
#!/usr/bin/env python3
import hug # fades
import enum
import html.parser
import json
import urllib.request
class Table(enum.Enum):
STATISTICS = enum.auto()
SCOREBOARD = enum.auto()
class OSMTop500Parser(html.parser.HTMLParser):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tables = iter(Table)
self.results = {e.name: [] for e in Table}
self.FIELDS = (
'members_active',
'nodes_created',
'nodes_modified',
'nodes_deleted',
)
# default values
self.record = False
self.buffer = ""
def handle_starttag(self, tag, attrs):
if tag == 'table':
self.table = next(self.tables)
elif tag == 'tr':
self.skip = 1 # skip first column
elif tag == 'td':
self.record = True
def handle_endtag(self, tag):
if tag == 'table' and self.table == Table.STATISTICS:
name = Table.STATISTICS.name
values = self.results[name]
self.results[name] = dict(zip(self.FIELDS, values))
elif tag == 'td':
self.record = False
self.skip -= 1
if self.buffer:
name, value = self.table.name, self.buffer
if self.table == Table.STATISTICS:
value = int(value)
self.results[name].append(value)
self.buffer = ""
def handle_data(self, data):
if self.record and not self.skip:
self.buffer += data
@hug.cli()
@hug.get("/top500", examples='country=italy', output=hug.output_format.json)
def top500(country: hug.types.text):
""" OpenStreetMap top 500 from neis-one.org """
url = "http://osmstats.neis-one.org/" \
f"?item=countries&country={country}"
with urllib.request.urlopen(url) as response:
result = response.read().decode('utf-8')
parser = OSMTop500Parser()
parser.feed(result)
return parser.results
if __name__ == '__main__':
top500.interface.cli()
from osmstats import __hug_wsgi__ as application
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.