mirror of
https://github.com/miurahr/aqtinstall.git
synced 2025-12-18 13:14:37 +03:00
Refactoring metalink class
Signed-off-by: Hiroshi Miura <miurahr@linux.com>
This commit is contained in:
@@ -27,7 +27,7 @@ import platform
|
|||||||
import requests
|
import requests
|
||||||
import sys
|
import sys
|
||||||
import traceback
|
import traceback
|
||||||
import aqt.metalink
|
import xml.etree.ElementTree as ElementTree
|
||||||
from six import StringIO
|
from six import StringIO
|
||||||
from multiprocessing.dummy import Pool
|
from multiprocessing.dummy import Pool
|
||||||
from operator import and_
|
from operator import and_
|
||||||
@@ -50,16 +50,10 @@ def _get(url, stream=False, mirror=True):
|
|||||||
blacklist = ['http://mirrors.ustc.edu.cn',
|
blacklist = ['http://mirrors.ustc.edu.cn',
|
||||||
'http://mirrors.tuna.tsinghua.edu.cn',
|
'http://mirrors.tuna.tsinghua.edu.cn',
|
||||||
'http://mirrors.geekpie.club']
|
'http://mirrors.geekpie.club']
|
||||||
for b in blacklist:
|
mml = Metalink(url)
|
||||||
if newurl.startswith(b):
|
newurl = mml.altlink(blacklist=blacklist)
|
||||||
mml = aqt.metalink.Metalink(url)
|
print('Redirected to new URL: {}'.format(newurl))
|
||||||
newurl = mml.altlink(blacklist=blacklist)
|
r = requests.get(newurl, stream=stream)
|
||||||
break
|
|
||||||
try:
|
|
||||||
r = requests.get(newurl, stream=stream)
|
|
||||||
except requests.exceptions.ConnectionError as e:
|
|
||||||
print('Redirected to new URL: {}'.format(newurl))
|
|
||||||
raise e
|
|
||||||
else:
|
else:
|
||||||
r = requests.get(url, stream=True)
|
r = requests.get(url, stream=True)
|
||||||
return r
|
return r
|
||||||
@@ -147,3 +141,47 @@ class QtInstaller:
|
|||||||
raise e
|
raise e
|
||||||
else:
|
else:
|
||||||
exit(1)
|
exit(1)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Metalink:
|
||||||
|
'''Download .meta4 metalink version4 xml file and parse it.'''
|
||||||
|
|
||||||
|
def __init__(self, url):
|
||||||
|
self.mirrors = {}
|
||||||
|
self.url = url
|
||||||
|
try:
|
||||||
|
m = requests.get(url + '.meta4')
|
||||||
|
except:
|
||||||
|
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']
|
||||||
|
self.mirrors[pri] = u.text
|
||||||
|
|
||||||
|
def altlink(self, priority=None, blacklist=None):
|
||||||
|
if len(self.mirrors) == 0:
|
||||||
|
# no alternative
|
||||||
|
return self.url
|
||||||
|
if priority is None:
|
||||||
|
if blacklist is not None:
|
||||||
|
for ind in range(len(self.mirrors)):
|
||||||
|
mirror = self.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(self.mirrors)):
|
||||||
|
mirror = self.mirrors[str(ind + 1)]
|
||||||
|
if mirror == self.candidate:
|
||||||
|
continue
|
||||||
|
return mirror
|
||||||
|
else:
|
||||||
|
return self.mirrors[str(priority)]
|
||||||
@@ -1,67 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
#
|
|
||||||
# Copyright (C) 2018 Linus Jahn <lnj@kaidan.im>
|
|
||||||
# Copyright (C) 2019 Hiroshi Miura <miurahr@linux.com>
|
|
||||||
#
|
|
||||||
# Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
||||||
# this software and associated documentation files (the "Software"), to deal in
|
|
||||||
# the Software without restriction, including without limitation the rights to
|
|
||||||
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
||||||
# the Software, and to permit persons to whom the Software is furnished to do so,
|
|
||||||
# subject to the following conditions:
|
|
||||||
#
|
|
||||||
# The above copyright notice and this permission notice shall be included in all
|
|
||||||
# copies or substantial portions of the Software.
|
|
||||||
#
|
|
||||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
||||||
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
||||||
# COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
||||||
# IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
|
|
||||||
import requests
|
|
||||||
import xml.etree.ElementTree as ElementTree
|
|
||||||
|
|
||||||
|
|
||||||
class Metalink:
|
|
||||||
'''Download .meta4 metalink version4 xml file and parse it.'''
|
|
||||||
|
|
||||||
def __init__(self, url, candidate=None):
|
|
||||||
self.mirrors = {}
|
|
||||||
self.url = url
|
|
||||||
self.candidate = candidate
|
|
||||||
m = requests.get(url + '.meta4')
|
|
||||||
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']
|
|
||||||
self.mirrors[pri] = u.text
|
|
||||||
|
|
||||||
def altlink(self, priority=None, blacklist=None):
|
|
||||||
if len(self.mirrors) == 0:
|
|
||||||
# no alternative
|
|
||||||
if self.candidate is not None:
|
|
||||||
return self.candidate
|
|
||||||
else:
|
|
||||||
return self.url
|
|
||||||
if priority is None:
|
|
||||||
if blacklist is not None:
|
|
||||||
for ind in range(len(self.mirrors)):
|
|
||||||
mirror = self.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(self.mirrors)):
|
|
||||||
mirror = self.mirrors[str(ind + 1)]
|
|
||||||
if mirror == self.candidate:
|
|
||||||
continue
|
|
||||||
return mirror
|
|
||||||
else:
|
|
||||||
return self.mirrors[str(priority)]
|
|
||||||
Reference in New Issue
Block a user