summaryrefslogtreecommitdiff
path: root/osm_proposals/proposals.py
blob: fff8c51ea16f869ca474690cf6d3a10812e943ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#!/usr/bin/env python3
"""Queries wiki.openstreetmap.org for proposals and writes a JSON list of them to the given file."""
import argparse
import datetime
import html
import json
import sys
import time
import logging
from collections.abc import Container

import logformat
import pywikiapi
import mwparserfromhell
import requests

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")
    args = arg_parser.parse_args()

    try:
        proposals = find_proposals()
    except requests.exceptions.ConnectionError:
        # For some reason the OSM wiki gives connection errors on average one per day (with this running hourly).
        # Some days none, some days several.

        with open(args.out_file, 'r') as f:
            last_updated = datetime.datetime.fromtimestamp(json.load(f)["last_updated"])

        age = datetime.datetime.now() - last_updated

        if age > datetime.timedelta(days=1):
            logger.error(
                f"connection error escalated because last successful update was {age} ago",
                traceback=True,
                data_age=age.total_seconds(),
            )
        else:
            logger.info(
                f"connection error deemed ok because last successful update was {age} ago",
                traceback=True,
                data_age=age.total_seconds(),
            )
        return

    with open(args.out_file, 'w') as f:
        json.dump({"last_updated": int(time.time()), "proposals": proposals}, f)

    logger.info(f"updated {args.out_file}")


def find_proposals():
    res = requests.get(
        OSMWIKI_ENDPOINT,
        params=dict(
            action='expandtemplates',
            prop='wikitext',
            format='json',
            text='{{#invoke:languages/table|json}}',
        ),
    )
    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',
        geilimit='max',
        prop='revisions',
        rvprop='content',
        rvslots='main',
    ):
        proposal = parse_proposal(
            page_title=page['title'],
            text=page['revisions'][0]['slots']['main']['content'],
            langs=langs,
        )
        if proposal:
            proposals.append(proposal)

    proposals.sort(key=sort_key, reverse=True)
    return [{k: v for k, v in p.items() if v is not None} for p in proposals]


def get_template_val(tpl, name):
    param = tpl.get(name, None)
    if param:
        value = param.value.strip()
        if value:
            # turn empty strings into None
            return value


def is_stub(doc):
    if any(
        doc.ifilter_templates(matches=lambda t: t.name.matches('Archived proposal'))
    ):
        return False

    if not any(doc.ifilter_headings()):
        # detect proposals without headings as stubs
        return True

    if not any(
        n
        for n in doc.nodes
        # any text
        if isinstance(n, mwparserfromhell.nodes.text.Text)
        # other than newlines
        and n.strip()
        # and "Please comment on the [[{{TALKPAGENAME}}|discussion page]]."
        and n.strip() not in ('Please comment on the', '.')
    ):
        # detect proposals without text as stubs
        return True

    return False


def parse_proposal(page_title: str, text: str, langs: Container[str]) -> dict | None:
    doc = mwparserfromhell.parse(text)
    proposal_page_templates = doc.filter_templates(
        matches=lambda t: t.name.matches('Proposal page')
        or t.name.matches('Proposal Page')
    )

    if not proposal_page_templates:
        logger.info('{{Proposal Page}} not found', page=page_title)
        return None

    for comment in doc.ifilter_comments():
        # remove comments like <!-- Date the RFC email is sent to the Tagging list: YYYY-MM-DD -->
        doc.remove(comment)

    tpl = proposal_page_templates[0]

    status = get_template_val(tpl, 'status')
    if status:
        status = status.lower()

    if is_stub(doc):
        if status in ('approved', 'rejected'):
            logger.info(f'{status} proposal is a stub', page=page_title)
        else:
            logger.info('skipping stub', page=page_title)
            return None

    name = get_template_val(tpl, 'name')
    if name:
        name = html.unescape(name)

    draft_start = get_template_val(tpl, 'draftStartDate')
    if draft_start in ('*', '-'):
        draft_start = None

    rfc_start = get_template_val(tpl, 'rfcStartDate')
    if rfc_start in ('*', '-'):
        rfc_start = None

    vote_start = get_template_val(tpl, 'voteStartDate')
    if vote_start in ('*', '-'):
        vote_start = None

    definition = get_template_val(tpl, 'definition')
    users = get_template_val(tpl, 'users') or get_template_val(tpl, 'user')

    parts = page_title.split(':', maxsplit=1)
    parts[0] = parts[0].lower()

    lang = None
    if parts[0] in langs:
        lang = parts[0]

    return dict(
        page_title=page_title,
        lang=lang,
        name=name,
        status=status,
        definition=definition,
        draft_start=draft_start,
        rfc_start=rfc_start,
        vote_start=vote_start,
        authors=users,
    )


STATUSES = {
    'voting': 0,
    'post-vote': 1,
    'proposed': 2,
    'draft': 3,
    'approved': 4,
    'inactive': 5,
    'rejected': 6,
    'abandoned': 7,
    'canceled': 8,
    'obsoleted': 9,
}


def sort_key(proposal):
    status = proposal['status']

    if status in ('voting', 'approved', 'rejected'):
        date = proposal['vote_start'] or ''
    elif status == 'proposed':
        date = proposal['rfc_start'] or ''
    else:
        date = proposal['draft_start'] or ''

    return (-STATUSES.get(proposal['status'], 10), date)