diff options
Diffstat (limited to 'osm_proposals/proposals.py')
-rwxr-xr-x | osm_proposals/proposals.py | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/osm_proposals/proposals.py b/osm_proposals/proposals.py index 1bb1f14..edcb453 100755 --- a/osm_proposals/proposals.py +++ b/osm_proposals/proposals.py @@ -4,8 +4,10 @@ import argparse import html import json import sys +import logging from collections.abc import Container +import logformat import pywikiapi import mwparserfromhell import requests @@ -14,7 +16,13 @@ OSMWIKI_ENDPOINT = 'https://wiki.openstreetmap.org/w/api.php' # https://wiki.openstreetmap.org/w/index.php?title=Template:Proposal_page&action=edit +logfmt_handler = logging.StreamHandler() +logfmt_handler.setFormatter(logformat.LogfmtFormatter()) +logging.basicConfig(handlers=[logfmt_handler], level=logging.INFO) +logger = logformat.get_logger() + +@logger.log_uncaught def run(): arg_parser = argparse.ArgumentParser(description=__doc__) arg_parser.add_argument("out_file") @@ -29,11 +37,17 @@ def run(): text='{{#invoke:languages/table|json}}', ), ) - langs: dict[str, dict] = json.loads(res.json()['expandtemplates']['wikitext']) + if not res.ok: + logger.error("expandtemplates request failed", status=res.status_code) + sys.exit(1) + + data = res.json() + langs: dict[str, dict] = json.loads(data['expandtemplates']['wikitext']) osmwiki = pywikiapi.Site(OSMWIKI_ENDPOINT) proposals = [] + # TODO: catch exception raised if HTTP request fails for page in osmwiki.query_pages( generator='embeddedin', geititle='Template:Proposal page', @@ -55,6 +69,8 @@ def run(): with open(args.out_file, 'w') as f: json.dump([{k: v for k, v in p.items() if v is not None} for p in proposals], f) + logger.info(f"updated {args.out_file}") + def get_template_val(tpl, name): param = tpl.get(name, None) @@ -65,10 +81,6 @@ def get_template_val(tpl, name): return value -def eprint(*args): - print(*args, file=sys.stderr) - - def is_stub(doc): if any( doc.ifilter_templates(matches=lambda t: t.name.matches('Archived proposal')) @@ -103,7 +115,7 @@ def parse_proposal(page_title: str, text: str, langs: Container[str]) -> dict | ) if not proposal_page_templates: - eprint('{{Proposal Page}} not found in', page_title) + logger.info('{{Proposal Page}} not found', page=page_title) return None for comment in doc.ifilter_comments(): @@ -118,9 +130,9 @@ def parse_proposal(page_title: str, text: str, langs: Container[str]) -> dict | if is_stub(doc): if status in ('approved', 'rejected'): - eprint(f'WARNING {status} proposal is a stub', page_title) + logger.info(f'{status} proposal is a stub', page=page_title) else: - eprint('skipping stub', page_title) + logger.info('skipping stub', page=page_title) return None name = get_template_val(tpl, 'name') |