prepare_xep_list.py

Mon, 20 Mar 2023 11:08:08 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Mon, 20 Mar 2023 11:08:08 +0000
changeset 4
444a46eadb74
parent 0
8e1675826e46
permissions
-rwxr-xr-x

Add canned query 'implementation_counts'

'''
This file is used to download the XEP list and convert it to JSON
'''
from typing import Any

import json
import os
import sys

import requests
from defusedxml.ElementTree import fromstring
from defusedxml.ElementTree import ParseError

XEP_LIST_URL = 'https://xmpp.org/extensions/xeplist.xml'


def build_xep_list() -> None:
    '''
    Download and parse xeplist.xml and build xeplist.json
    '''
    try:
        xeplist_request = requests.get(XEP_LIST_URL)
    except requests.exceptions.RequestException as err:
        sys.exit(f'Error while requesting xeplist.xml ({err}')

    if not 200 >= xeplist_request.status_code < 400:
        sys.exit(f'Error while downloading xeplist.xml '
                 f'({xeplist_request.status_code}')

    try:
        root = fromstring(xeplist_request.content)
    except ParseError:
        sys.exit('Error while parsing xeplist.xml')

    def fix_status(status: str) -> str:
        if status == 'Draft':
            return 'Stable'
        return status

    xeps: list[dict[str, Any]] = []
    for xep in root.findall("xep"):
        xep_data = {
            'title': xep.find('title').text,
            'status': fix_status(xep.find('status').text),
            'number': int(xep.find('number').text) if xep.find('number').text != 'xxxx' else None,
            'last_updated': xep.find('last-revision').find('date').text,
            'version': xep.find('last-revision').find('version').text,
            'type': xep.find('type').text,
            'approver': xep.find('approver').text,
        }
        if xep.get("accepted") == "true":
            xep_data.update({
                'accepted': True,
                'short_name': xep.find('shortname').text if xep.find('shortname') else None,
                'url': f'https://xmpp.org/extensions/xep-{xep_data["number"]:04d}.html',
            })
        else:
            xep_data.update({
                'accepted': False,
                'short_name': xep.find("proto-name").text,
                'url': f'https://xmpp.org/extensions/inbox/{xep.find("proto-name").text}.html',
            })
        xeps.append(xep_data)

    base_path = os.path.dirname(os.path.abspath(sys.argv[0]))

    with open(f'{base_path}/../data/xeplist.json',
              'w',
              encoding='utf-8') as json_file:
        json.dump(xeps, json_file, indent=4)
    print('XEP List prepared successfully')


if __name__ == '__main__':
    build_xep_list()

mercurial