mirror of
https://github.com/miurahr/aqtinstall.git
synced 2025-12-17 20:54:38 +03:00
Use settings module for blacklisting
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
42
aqt/helper.py
Normal file
42
aqt/helper.py
Normal file
@@ -0,0 +1,42 @@
|
||||
import xml.etree.ElementTree as ElementTree
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def altlink(url, priority=None, blacklist=None):
|
||||
'''Download .meta4 metalink version4 xml file and parse it.'''
|
||||
|
||||
mirrors = {}
|
||||
url = url
|
||||
try:
|
||||
m = requests.get(url + '.meta4')
|
||||
except requests.exceptions.ConnectionError:
|
||||
return
|
||||
else:
|
||||
mirror_xml = ElementTree.fromstring(m.text)
|
||||
for f in mirror_xml.iter("{urn:ietf:params:xml:ns:metalink}file"):
|
||||
for u in f.iter("{urn:ietf:params:xml:ns:metalink}url"):
|
||||
pri = u.attrib['priority']
|
||||
mirrors[pri] = u.text
|
||||
|
||||
if len(mirrors) == 0:
|
||||
# no alternative
|
||||
return url
|
||||
if priority is None:
|
||||
if blacklist is not None:
|
||||
for ind in range(len(mirrors)):
|
||||
mirror = mirrors[str(ind + 1)]
|
||||
black = False
|
||||
for b in blacklist:
|
||||
if mirror.startswith(b):
|
||||
black = True
|
||||
continue
|
||||
if black:
|
||||
continue
|
||||
return mirror
|
||||
else:
|
||||
for ind in range(len(mirrors)):
|
||||
mirror = mirrors[str(ind + 1)]
|
||||
return mirror
|
||||
else:
|
||||
return mirrors[str(priority)]
|
||||
Reference in New Issue
Block a user