89 lines
2.6 KiB
Python
Executable File
89 lines
2.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import csv
|
|
import io
|
|
import re
|
|
##
|
|
import iso3166
|
|
##
|
|
import classes
|
|
|
|
|
|
_proto_re = re.compile(r'^(?P<proto>https?)(?P<uri>.*)')
|
|
|
|
|
|
class Ranker(classes.Ranker):
|
|
# https://lists.centos.org/pipermail/centos-mirror/2017-March/010312.html
|
|
mirrorlist_url = 'https://www.centos.org/download/full-mirrorlist.csv'
|
|
distro_name = 'centos'
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.get_mirrors()
|
|
|
|
def extract_mirrors(self):
|
|
# They removed FTP support.
|
|
c = csv.DictReader(io.StringIO(self.raw_html), )
|
|
my_country = iso3166.countries.get(self.my_info['country'])
|
|
countrynames = iso3166.countries_by_name.keys()
|
|
for row in c:
|
|
if not row.get('Region') or row['Region'].strip() == '':
|
|
row['Location'] = row['Region']
|
|
# They changed things. Again.
|
|
country = row['Region'].strip()
|
|
continent = row['Location'].strip()
|
|
cu = country.upper()
|
|
if continent in ('US', 'Canada'):
|
|
country = continent
|
|
try:
|
|
country = iso3166.countries.get(country)
|
|
except KeyError:
|
|
country = iso3166.countries_by_name.get(cu)
|
|
# Gorram it.
|
|
if not country:
|
|
for cs in countrynames:
|
|
if cs.startswith(cu):
|
|
country = iso3166.countries_by_name[cs]
|
|
break
|
|
if country != my_country:
|
|
continue
|
|
for k, v in row.items():
|
|
if v.strip() == '':
|
|
row[k] = None
|
|
pref_url = row.get('Rsync link')
|
|
pref_url = str(pref_url).strip()
|
|
if pref_url not in ('', None, 'None'):
|
|
url = _proto_re.sub(r'\g<uri>', pref_url)
|
|
else:
|
|
continue
|
|
self.raw_mirrors.append(row)
|
|
self.mirror_candidates.append(url)
|
|
return(None)
|
|
|
|
|
|
def parseArgs():
|
|
args = argparse.ArgumentParser(description = 'Generate a list of suitable CentOS upstream mirrors in order of '
|
|
'speed')
|
|
args.add_argument('-x', '--xml',
|
|
dest = 'xml',
|
|
action = 'store_true',
|
|
help = ('If specified, generate a config stub instead of a printed list of URLs'))
|
|
return(args)
|
|
|
|
|
|
def main():
|
|
args = parseArgs().parse_args()
|
|
r = Ranker()
|
|
r.extract_mirrors()
|
|
r.speedcheck()
|
|
if args.xml:
|
|
print(r.gen_xml())
|
|
else:
|
|
r.print()
|
|
return(None)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
main()
|