Get value from online xml

113 Views Asked by At

I want to get the value of the 'latest' version tag from here: https://papermc.io/repo/repository/maven-public/com/destroystokyo/paper/paper-api/maven-metadata.xml

I tried using this python:

import urllib.request
from xml.etree import ElementTree

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

data = opener.open('https://papermc.io/repo/repository/maven-public/com/destroystokyo/paper/paper-api/maven-metadata.xml').

root = ElementTree.fromstring(data)

versioning = root.find("versioning")
latest = versioning.find("latest")
snip.rv = latest.text

The problem is, using this inside of vim (I'm trying to make UltiSnips snippets with it) makes the whole of vim extremely slow after the code has finished running.

What's causing my program to slow down just when I add that ^^ code?

1

There are 1 best solutions below

0
On

I don't know if this will solve the performance issue in vim, but the code was not running for me due to errors in it.

opener.open returns a file-like object, so you should read it using ElementTree.parse instead of ElementTree.fromstring (actually there is a trailing dot after opener.open(...), so I don't know if you missed a read() thereafter. In that case the return value is indeed a string).

Apart from that, you could try to close the opener to see if that frees up some resources (or use the with).

I attach an example of the improved code:

import urllib.request
from xml.etree import ElementTree

opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]

with opener.open('https://papermc.io/repo/repository/maven-public/com/destroystokyo/paper/paper-api/maven-metadata.xml') as data:
    root = ElementTree.parse(data)
    latest = root.find("./versioning/latest")
    snip.rv = latest.text