prepare_xep_list.py

changeset 0
8e1675826e46
equal deleted inserted replaced
-1:000000000000 0:8e1675826e46
1 '''
2 This file is used to download the XEP list and convert it to JSON
3 '''
4 from typing import Any
5
6 import json
7 import os
8 import sys
9
10 import requests
11 from defusedxml.ElementTree import fromstring
12 from defusedxml.ElementTree import ParseError
13
14 XEP_LIST_URL = 'https://xmpp.org/extensions/xeplist.xml'
15
16
17 def build_xep_list() -> None:
18 '''
19 Download and parse xeplist.xml and build xeplist.json
20 '''
21 try:
22 xeplist_request = requests.get(XEP_LIST_URL)
23 except requests.exceptions.RequestException as err:
24 sys.exit(f'Error while requesting xeplist.xml ({err}')
25
26 if not 200 >= xeplist_request.status_code < 400:
27 sys.exit(f'Error while downloading xeplist.xml '
28 f'({xeplist_request.status_code}')
29
30 try:
31 root = fromstring(xeplist_request.content)
32 except ParseError:
33 sys.exit('Error while parsing xeplist.xml')
34
35 def fix_status(status: str) -> str:
36 if status == 'Draft':
37 return 'Stable'
38 return status
39
40 xeps: list[dict[str, Any]] = []
41 for xep in root.findall("xep"):
42 xep_data = {
43 'title': xep.find('title').text,
44 'status': fix_status(xep.find('status').text),
45 'number': int(xep.find('number').text) if xep.find('number').text != 'xxxx' else None,
46 'last_updated': xep.find('last-revision').find('date').text,
47 'version': xep.find('last-revision').find('version').text,
48 'type': xep.find('type').text,
49 'approver': xep.find('approver').text,
50 }
51 if xep.get("accepted") == "true":
52 xep_data.update({
53 'accepted': True,
54 'short_name': xep.find('shortname').text if xep.find('shortname') else None,
55 'url': f'https://xmpp.org/extensions/xep-{xep_data["number"]:04d}.html',
56 })
57 else:
58 xep_data.update({
59 'accepted': False,
60 'short_name': xep.find("proto-name").text,
61 'url': f'https://xmpp.org/extensions/inbox/{xep.find("proto-name").text}.html',
62 })
63 xeps.append(xep_data)
64
65 base_path = os.path.dirname(os.path.abspath(sys.argv[0]))
66
67 with open(f'{base_path}/../data/xeplist.json',
68 'w',
69 encoding='utf-8') as json_file:
70 json.dump(xeps, json_file, indent=4)
71 print('XEP List prepared successfully')
72
73
74 if __name__ == '__main__':
75 build_xep_list()

mercurial