|
1 #!/usr/bin/env python3 |
|
2 |
|
3 import urllib.request |
|
4 import argparse, sys |
|
5 from xml.dom.minidom import parse, getDOMImplementation |
|
6 |
|
7 |
|
8 def getJidsFromUrl(url): |
|
9 f = urllib.request.urlopen(url) |
|
10 indoc = parse(f) |
|
11 jids = [] |
|
12 query = indoc.documentElement |
|
13 if query.localName == "query" : |
|
14 for item in query.getElementsByTagName("item"): |
|
15 jid = item.getAttribute("jid") |
|
16 jids.append(jid) |
|
17 return jids |
|
18 |
|
19 def createDocument(): |
|
20 impl = getDOMImplementation() |
|
21 return impl.createDocument(None, "resources", None) |
|
22 |
|
23 |
|
24 def purge(elems): |
|
25 res = [] |
|
26 for i in elems: |
|
27 if i not in res: |
|
28 res.append(i) |
|
29 return res |
|
30 |
|
31 def appendStringElem(doc, node, jids): |
|
32 for i in jids: |
|
33 item = doc.createElement("item") |
|
34 text = doc.createTextNode(i) |
|
35 item.appendChild(text) |
|
36 node.appendChild(item) |
|
37 |
|
38 |
|
39 parser = argparse.ArgumentParser(description='Collect some free xmpp services') |
|
40 parser.add_argument('url', metavar="url", |
|
41 default=['http://xmpp.net/services.xml', "https://list.jabber.at/api/?format=services.xml"] , nargs='*', |
|
42 help='url to get the services') |
|
43 |
|
44 parser.add_argument('-o', metavar="FILE", type=argparse.FileType('bw'), |
|
45 default = sys.stdout.buffer, |
|
46 help='send output to FILE') |
|
47 |
|
48 args = parser.parse_args() |
|
49 |
|
50 # collect the servers jid |
|
51 jids = [] |
|
52 for url in args.url: |
|
53 jids += getJidsFromUrl(url) |
|
54 |
|
55 jids = purge(jids) |
|
56 |
|
57 # create the xml output document |
|
58 outdoc = createDocument() |
|
59 res_element = outdoc.documentElement |
|
60 string_array_elem = outdoc.createElement('string-array') |
|
61 res_element.appendChild(string_array_elem) |
|
62 string_array_elem.setAttribute("name", "xmpp_server_list") |
|
63 appendStringElem(outdoc, string_array_elem, jids) |
|
64 |
|
65 # print result |
|
66 f = args.o |
|
67 f.write(outdoc.toprettyxml(encoding="utf-8")) |
|
68 |