<commit_before>"""
This product includes GeoLite2 data created by MaxMind, available from
https://www.maxmind.com
(attribution requirement)
"""

import geoip2.database
from os.path import dirname, join, abspath
import logging


logger = logging.getLogger(__name__)

_app_path = dirname(dirname(abspath(__file__)))
_geo_file = "GeoLite2-City.mmdb"
geoip2_reader = geoip2.database.Reader(join(_app_path, 'data', 'geo',
                                            'GeoLite2-City_latest', _geo_file))


def get_geoip2_data(ip_address):
    out = {}
    try:
        loc_data = geoip2_reader.city(ip_address)
        out['city'] = loc_data.city.name
        out['country'] = loc_data.country.name
        out['country_code'] = loc_data.country.iso_code
        out['ip_lat'] = str(loc_data.location.latitude)
        out['ip_long'] = str(loc_data.location.longitude)
        out['postal_code'] = loc_data.postal.code
        out['subdivision'] = loc_data.subdivisions.most_specific.name
    except:
        logger.error('Problem getting geoip data for {}'.format(ip_address))

    return out<commit_msg>Bypass geo location if not installed or file not found<commit_after>"""
This product includes GeoLite2 data created by MaxMind, available from
https://www.maxmind.com
(attribution requirement)
"""


from os.path import dirname, join, abspath
import logging

logger = logging.getLogger(__name__)


_app_path = dirname(dirname(abspath(__file__)))
_geo_file = "GeoLite2-City.mmdb"
geo_file_path = join(_app_path, 'data', 'geo',
                                'GeoLite2-City_latest', _geo_file)
geoip2_reader = None

try:
    import geoip2.database
    geoip2_reader = geoip2.database.Reader(geo_file_path)
except ImportError as err:
    logger.error(f'Cannot import geoip2 module. To use API access logging, you need '
                 f'to install it manually using `pip install geoip2`')
except FileNotFoundError as err:
    logger.error(f'Geoip cites file cannot be read from {geo_file_path}.\n'
                f'Make sure to manually download the file from: \n'
                f'https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.tar.gz\n'
                f'Then, set the geo_file_path in qcfractal_config.yaml in your base_folder '
                f'(default base_folder is ~/.qca/qcfractal/qcfractal_config.yaml).')


def get_geoip2_data(ip_address):
    out = {}

    if not geoip2_reader:
        return out

    try:
        loc_data = geoip2_reader.city(ip_address)
        out['city'] = loc_data.city.name
        out['country'] = loc_data.country.name
        out['country_code'] = loc_data.country.iso_code
        out['ip_lat'] = str(loc_data.location.latitude)
        out['ip_long'] = str(loc_data.location.longitude)
        out['postal_code'] = loc_data.postal.code
        out['subdivision'] = loc_data.subdivisions.most_specific.name
    except:
        logger.error('Problem getting geoip data for {}'.format(ip_address))

    return out
{'fraction_non_alphanumeric': 0.10838101649276338, 'fraction_numerical': 0.007068327162571525, 'mean_word_length': 3.221590909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '362497', 'n_tokens_mistral': 990, 'n_tokens_neox': 947, 'n_words': 207}