()
return mirrors
Discover available Linux Mint mirrors.

    :returns: A set of :class:`.CandidateMirror` objects that have their
              :attr:`~.CandidateMirror.mirror_url` property set and may have
              the :attr:`~.CandidateMirror.last_updated` property set.
    :raises: If no mirrors are discovered an exception is raised.

    This queries :data:`MIRRORS_URL`to discover available Linux Mint mirrors.
    Here's an example run:

    >>> from apt_smart.backends.linuxmint import discover_mirrors
    >>> from pprint import pprint
    >>> pprint(discover_mirrors())
    set([CandidateMirror(mirror_url='http://mirrors.cqu.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.hust.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.shu.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='https://mirrors.tuna.tsinghua.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.ustc.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.zju.edu.cn/linuxmint/'),
         ...])
Discover available Linux Mint mirrors.
Discover available Linux Mint mirrors .
def discover_mirrors():
    """
    Discover available Linux Mint mirrors.

    :returns: A set of :class:`.CandidateMirror` objects that have their
              :attr:`~.CandidateMirror.mirror_url` property set and may have
              the :attr:`~.CandidateMirror.last_updated` property set.
    :raises: If no mirrors are discovered an exception is raised.

    This queries :data:`MIRRORS_URL`to discover available Linux Mint mirrors.
    Here's an example run:

    >>> from apt_smart.backends.linuxmint import discover_mirrors
    >>> from pprint import pprint
    >>> pprint(discover_mirrors())
    set([CandidateMirror(mirror_url='http://mirrors.cqu.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.hust.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.shu.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='https://mirrors.tuna.tsinghua.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.ustc.edu.cn/linuxmint/'),
         CandidateMirror(mirror_url='http://mirrors.zju.edu.cn/linuxmint/'),
         ...])
    """
    timer = Timer()
    mirrors = set()
    logger.info("Discovering Linux Mint mirrors at %s ..", MIRRORS_URL)
    # Find which country the user is in to get mirrors in that country
    try:
        url = 'https://ipapi.co/json'
        response = fetch_url(url, timeout=2)
        # On py3 response is bytes and json.loads throws TypeError in py3.4 and 3.5,
        # so decode it to str
        if isinstance(response, six.binary_type):
            response = response.decode('utf-8')
        data = json.loads(response)
        country = data['country_name']
        logger.info("Found your location: %s by %s", country, url)
    except Exception:
        url = 'http://ip-api.com/json'
        response = fetch_url(url, timeout=5)
        if isinstance(response, six.binary_type):
            response = response.decode('utf-8')
        data = json.loads(response)
        country = data['country']
        logger.info("Found your location: %s by %s", country, url)
    if country == 'United States':
        country = 'USA'
    try:
        data = fetch_url(MIRRORS_URL, timeout=15)
    except Exception:
        logger.warning("Time out, try again")
        data = fetch_url(MIRRORS_URL, timeout=70, retry=True)
    soup = BeautifulSoup(data, 'html.parser')
    tables = soup.findAll('table')
    if not tables:
        raise Exception("Failed to locate <table> element in Linux Mint mirror page! (%s)" % MIRRORS_URL)
    else:
        while len(mirrors) < 3:
            for row in tables[2].findAll("tr"):
                if country in row.get_text():
                    for data in row.findAll("td"):
                        data_text = data.get_text()
                        # Check if the link looks like a mirror URL.
                        if data_text.startswith(('http://', 'https://')):
                            mirrors.add(CandidateMirror(mirror_url=data_text))
            if country == 'Worldwide':
                break  # break while loop when already set to 'Worldwide'
            if len(mirrors) < 3:
                logging.info("Too few mirrors found in your country, get more Worldwide mirrors.")
                country = 'Worldwide'

    if not mirrors:
        raise Exception("Failed to discover any Linux Mint mirrors! (using %s)" % MIRRORS_URL)
    logger.info("Discovered %s in %s.", pluralize(len(mirrors), "Linux Mint mirror"), timer)
    return mirrors
def discover_mirrors ( ) : timer = Timer ( ) mirrors = set ( ) logger . info ( "Discovering Linux Mint mirrors at %s .." , MIRRORS_URL ) # Find which country the user is in to get mirrors in that country try : url = 'https://ipapi.co/json' response = fetch_url ( url , timeout = 2 ) # On py3 response is bytes and json.loads throws TypeError in py3.4 and 3.5, # so decode it to str if isinstance ( response , six . binary_type ) : response = response . decode ( 'utf-8' ) data = json . loads ( response ) country = data [ 'country_name' ] logger . info ( "Found your location: %s by %s" , country , url ) except Exception : url = 'http://ip-api.com/json' response = fetch_url ( url , timeout = 5 ) if isinstance ( response , six . binary_type ) : response = response . decode ( 'utf-8' ) data = json . loads ( response ) country = data [ 'country' ] logger . info ( "Found your location: %s by %s" , country , url ) if country == 'United States' : country = 'USA' try : data = fetch_url ( MIRRORS_URL , timeout = 15 ) except Exception : logger . warning ( "Time out, try again" ) data = fetch_url ( MIRRORS_URL , timeout = 70 , retry = True ) soup = BeautifulSoup ( data , 'html.parser' ) tables = soup . findAll ( 'table' ) if not tables : raise Exception ( "Failed to locate <table> element in Linux Mint mirror page! (%s)" % MIRRORS_URL ) else : while len ( mirrors ) < 3 : for row in tables [ 2 ] . findAll ( "tr" ) : if country in row . get_text ( ) : for data in row . findAll ( "td" ) : data_text = data . get_text ( ) # Check if the link looks like a mirror URL. if data_text . startswith ( ( 'http://' , 'https://' ) ) : mirrors . add ( CandidateMirror ( mirror_url = data_text ) ) if country == 'Worldwide' : break # break while loop when already set to 'Worldwide' if len ( mirrors ) < 3 : logging . info ( "Too few mirrors found in your country, get more Worldwide mirrors." ) country = 'Worldwide' if not mirrors : raise Exception ( "Failed to discover any Linux Mint mirrors! (using %s)" % MIRRORS_URL ) logger . info ( "Discovered %s in %s." , pluralize ( len ( mirrors ) , "Linux Mint mirror" ) , timer ) return mirrors