Exemplo n.º 1
0
        return users

    json_users = users_from(search_term)

    if not len(json_users):
        return

    for user in json_users:
        logger.info("FOUND -- %s -- %s" %
                    (user.get('username'), user.get('location')))
        user.update({'country': country_stub, 'city': city})
        all_users.append(user)


for country_code, country in countries.items():
    logger.info("COUNTRY: %s" % country.get('name'))

    country_stub = copy.copy(country)
    country_stub.update({'code': country_code})
    del (country_stub['patterns'])

    for city in country.get('patterns', []):
        logging.info("SEARCHING for city -- %s" % city.get('name'))
        for search_name in city.get('patterns', [city.get('name')]):
            add_to(search_name, all_users, country_stub, city)

    for name in country.get('names', []):
        logging.info("SEARCHING for country -- %s" % name)
        add_to(name, all_users, country_stub, None)
def country_city_by_location(location):
    ll = location.lower()
    country_matches = []
    city_matches = []

    for ccode, cc in countries.items():
        for ct in cc.get('patterns'):
            for ct_name in ct.get('patterns', [ct.get('name')]):
                if ct_name.lower() in location.lower():
                    city_matches.append((ccode, ct.get('name')))
        for cc_name in cc.get('names', [ct.get('name')]):
            if cc_name.lower() in location.lower():
                country_matches.append(ccode)

    city_matches = list(set(city_matches))
    country_matches = list(set(country_matches))

    err = (None, None)

    # HACKS
    if "benin" in ll and "city" in ll:
        return 'NG', "Benin City"

    if "cape town" in ll:
        if not "Capte Town" in [x[1] for x in city_matches]:
            city_matches.append(('ZA', "Cape Town"))

    if 'africa' in ll and not 'south' in ll:
        while True:
            try:
                country_matches.remove('ZA')
            except ValueError:
                break

    if ('addis' in ll and 'ababa' in ll
        ) or 'rabat' in ll:
        while True:
            try:
                city_matches.remove(('NG', 'Aba'))
            except ValueError:
                break

    if 'kampala' in ll or 'uganda' in ll:
        while True:
            try:
                city_matches.remove(('TD', 'Pala'))
            except ValueError:
                break

    if 'nigeria' in ll:
        while True:
            try:
                country_matches.remove('NE')
            except ValueError:
                break

    # Saint Louis in Senegal and Missouri/USA
    if 'saint' in ll and 'louis' in ll \
        and (not 'sn' in ll or not 'senegal' in ll or not 'sénégal' in ll):
        return err

    # Man vs Isle of Man
    if 'isle of man' in ll:
        return err

    if 'juba' in ll:
        while True:
            try:
                country_matches.remove('SD')
            except ValueError:
                break

    # Joar Sahara in Bangladesh
    if 'sahara' in ll and 'joar' in ll:
        return err

    if 'verde' in ll and (not 'cape' in ll and not 'cabo' in ll):
        return err

    # Praia Grande, Brazil
    if 'praia' in ll and 'grande' in ll:
        return err

    # Tripoli in Lebanon
    if 'tripoli' in ll and 'lebanon' in ll:
        return err

    # Lagos, Chile
    if 'lagos' in ll and 'chile' in ll:
        return err

    if 'lagos' in ll and 'juan' in ll:
        return err

    if 'lagos' in ll and ('pt' in ll or 'portugal' in ll or 'island' in ll):
        return err

    if 'drc' in ll and 'madrid' in ll:
        return err

    # Monrovia, CA
    if 'monrovia' in ll and ('ca' in ll or 'california' in ll or 'us' in ll):
        return err

    # Saint Maurice
    if 'maurice' in ll and 'saint' in ll:
        return err

    # more than ile maurice
    if 'maurice' in ll and len(ll) > 13:
        return err

    # Mali / Somalia
    if 'somali' in ll:
       while True:
            try:
                country_matches.remove('ML')
            except ValueError:
                break

    # Cape Elisabeth in Maine
    if 'elizabeth' in ll and ('cape' in ll or 'maine' in ll \
        or 'nj' in ll or 'nc' in ll or 'co' in ll):
        return err

    # Alexandria, Virginia
    if 'alexandria' in ll and ('virginia' in ll or 'va' in ll):
        return err

    # DR Congo and RD Congo
    if 'congo' in ll and ('rd' in ll or 'dr' in ll):
        while True:
            try:
                country_matches.remove('CG')
            except ValueError:
                break
        country_matches.append('CD')

    # basically, we found a single match on city
    if len(city_matches) == 1:
        if len(country_matches) == 0 \
            or (len(country_matches) == 1
                and country_matches[-1] == city_matches[-1][0]):
            return city_matches[-1]
        elif city_matches[-1][0] in country_matches:
            return city_matches[-1]

    # single match on country
    elif len(city_matches) == 0 and len(country_matches) == 1:
        return country_matches[-1], None

    # multiple locations
    elif len(city_matches) > 1 or len(country_matches) > 1:
        # pick first city that matches a country
        if len(city_matches):
            for cc, ct in city_matches:
                if cc in country_matches:
                    return cc, ct
        # pick first country
        if len(country_matches):
            return country_matches[0], None
        # pick what's left: a city
        return city_matches[0]

    if DEBUG:
        print(len(city_matches))
        print(len(country_matches))

        from pprint import pprint as pp ; pp(city_matches)
        from pprint import pprint as pp ; pp(country_matches)

    country_code = None
    city_name = None

    return country_code, city_name
def country_city_by_location(location):
    ll = location.lower()
    country_matches = []
    city_matches = []

    for ccode, cc in countries.items():
        for ct in cc.get('patterns'):
            for ct_name in ct.get('patterns', [ct.get('name')]):
                if ct_name.lower() in location.lower():
                    city_matches.append((ccode, ct.get('name')))
        for cc_name in cc.get('names', [ct.get('name')]):
            if cc_name.lower() in location.lower():
                country_matches.append(ccode)

    city_matches = list(set(city_matches))
    country_matches = list(set(country_matches))

    err = (None, None)

    # HACKS
    if "benin" in ll and "city" in ll:
        return 'NG', "Benin City"

    if "cape town" in ll:
        if not "Capte Town" in [x[1] for x in city_matches]:
            city_matches.append(('ZA', "Cape Town"))

    if 'africa' in ll and not 'south' in ll:
        while True:
            try:
                country_matches.remove('ZA')
            except ValueError:
                break

    if ('addis' in ll and 'ababa' in ll) or 'rabat' in ll:
        while True:
            try:
                city_matches.remove(('NG', 'Aba'))
            except ValueError:
                break

    if 'kampala' in ll or 'uganda' in ll:
        while True:
            try:
                city_matches.remove(('TD', 'Pala'))
            except ValueError:
                break

    if 'nigeria' in ll:
        while True:
            try:
                country_matches.remove('NE')
            except ValueError:
                break

    # Saint Louis in Senegal and Missouri/USA
    if 'saint' in ll and 'louis' in ll \
        and (not 'sn' in ll or not 'senegal' in ll or not 'sénégal' in ll):
        return err

    # Man vs Isle of Man
    if 'isle of man' in ll:
        return err

    if 'juba' in ll:
        while True:
            try:
                country_matches.remove('SD')
            except ValueError:
                break

    # Joar Sahara in Bangladesh
    if 'sahara' in ll and 'joar' in ll:
        return err

    if 'verde' in ll and (not 'cape' in ll and not 'cabo' in ll):
        return err

    # Praia Grande, Brazil
    if 'praia' in ll and 'grande' in ll:
        return err

    # Tripoli in Lebanon
    if 'tripoli' in ll and 'lebanon' in ll:
        return err

    # Lagos, Chile
    if 'lagos' in ll and 'chile' in ll:
        return err

    if 'lagos' in ll and 'juan' in ll:
        return err

    if 'lagos' in ll and ('pt' in ll or 'portugal' in ll or 'island' in ll):
        return err

    if 'drc' in ll and 'madrid' in ll:
        return err

    # Monrovia, CA
    if 'monrovia' in ll and ('ca' in ll or 'california' in ll or 'us' in ll):
        return err

    # Saint Maurice
    if 'maurice' in ll and 'saint' in ll:
        return err

    # more than ile maurice
    if 'maurice' in ll and len(ll) > 13:
        return err

    # Mali / Somalia
    if 'somali' in ll:
        while True:
            try:
                country_matches.remove('ML')
            except ValueError:
                break

    # Cape Elisabeth in Maine
    if 'elizabeth' in ll and ('cape' in ll or 'maine' in ll \
        or 'nj' in ll or 'nc' in ll or 'co' in ll):
        return err

    # Alexandria, Virginia
    if 'alexandria' in ll and ('virginia' in ll or 'va' in ll):
        return err

    # DR Congo and RD Congo
    if 'congo' in ll and ('rd' in ll or 'dr' in ll):
        while True:
            try:
                country_matches.remove('CG')
            except ValueError:
                break
        country_matches.append('CD')

    # basically, we found a single match on city
    if len(city_matches) == 1:
        if len(country_matches) == 0 \
            or (len(country_matches) == 1
                and country_matches[-1] == city_matches[-1][0]):
            return city_matches[-1]
        elif city_matches[-1][0] in country_matches:
            return city_matches[-1]

    # single match on country
    elif len(city_matches) == 0 and len(country_matches) == 1:
        return country_matches[-1], None

    # multiple locations
    elif len(city_matches) > 1 or len(country_matches) > 1:
        # pick first city that matches a country
        if len(city_matches):
            for cc, ct in city_matches:
                if cc in country_matches:
                    return cc, ct
        # pick first country
        if len(country_matches):
            return country_matches[0], None
        # pick what's left: a city
        return city_matches[0]

    if DEBUG:
        print(len(city_matches))
        print(len(country_matches))

        from pprint import pprint as pp
        pp(city_matches)
        from pprint import pprint as pp
        pp(country_matches)

    country_code = None
    city_name = None

    return country_code, city_name
        return users

    json_users = users_from(search_term)

    if not len(json_users):
        return

    for user in json_users:
        logger.info("FOUND -- %s -- %s" % (user.get('username'),
                                           user.get('location')))
        user.update({'country': country_stub,
                     'city': city})
        all_users.append(user)

for country_code, country in countries.items():
    logger.info("COUNTRY: %s" % country.get('name'))

    country_stub = copy.copy(country)
    country_stub.update({'code': country_code})
    del(country_stub['patterns'])

    for city in country.get('patterns', []):
        logging.info("SEARCHING for city -- %s" % city.get('name'))
        for search_name in city.get('patterns', [city.get('name')]):
            add_to(search_name, all_users, country_stub, city)

    for name in country.get('names', []):
        logging.info("SEARCHING for country -- %s" % name)
        add_to(name, all_users, country_stub, None)