Exemplo n.º 1
0
    def regions_for_mcc(self, mcc, metadata=False):
        """
        Return a list of region codes matching the passed in
        mobile country code.

        If the metadata argument is set to True, returns a list of
        region instances containing additional metadata instead.

        The return list is filtered by the set of recognized
        region codes present in the GENC dataset.
        """
        codes = [region.alpha2 for region in mobile_codes.mcc(str(mcc))]
        # map mcc region codes to genc region codes
        codes = [MCC_TO_GENC_MAP.get(code, code) for code in codes]
        valid_codes = set(codes).intersection(self._valid_regions)
        if not metadata:
            return list(valid_codes)

        result = []
        for code in valid_codes:
            region = genc.region_by_alpha2(code)
            if region is not None:
                result.append(Region(
                    code=region.alpha2,
                    name=region.name,
                    radius=self.region_max_radius(code)))
        return result
Exemplo n.º 2
0
    def regions_for_mcc(self, mcc, metadata=False):
        """
        Return a list of region codes matching the passed in
        mobile country code.

        If the metadata argument is set to True, returns a list of
        dictionaries containing additional metadata instead.

        The return list is filtered by the set of recognized
        region codes present in the GENC dataset.
        """
        codes = [region.alpha2 for region in mobile_codes.mcc(str(mcc))]
        # map mcc region codes to genc region codes
        codes = [MCC_GENC_SHAPEFILE_MAP.get(code, code) for code in codes]
        valid_codes = set(codes).intersection(self._valid_regions)
        if not metadata:
            return list(valid_codes)

        result = []
        for code in valid_codes:
            region = genc.region_by_alpha2(code)
            if region is not None:
                result.append(
                    Region(code=region.alpha2,
                           name=region.name,
                           radius=self.region_max_radius(code)))
        return result
Exemplo n.º 3
0
def regions(session):
    columns = RegionStat.__table__.c
    rows = session.execute(
        select(
            [
                columns.region,
                columns.gsm,
                columns.wcdma,
                columns.lte,
                columns.blue,
                columns.wifi,
            ]
        )
    ).fetchall()

    regions = {}
    for row in rows:
        code = row.region
        name = genc.region_by_alpha2(code).name
        gsm = int(row.gsm or 0)
        wcdma = int(row.wcdma or 0)
        lte = int(row.lte or 0)
        cell = sum((gsm, wcdma, lte))
        regions[code] = {
            "code": code,
            "name": name,
            "order": transliterate(name[:10]).lower(),
            "gsm": gsm,
            "wcdma": wcdma,
            "lte": lte,
            "cell": cell,
            "blue": int(row.blue or 0),
            "wifi": int(row.wifi or 0),
        }
    return sorted(regions.values(), key=itemgetter("cell"), reverse=True)
def regions(session):
    columns = RegionStat.__table__.c
    rows = session.execute(
        select([columns.region, columns.gsm, columns.wcdma, columns.lte,
                columns.blue, columns.wifi])
    ).fetchall()

    regions = {}
    for row in rows:
        code = row.region
        name = genc.region_by_alpha2(code).name
        gsm = int(row.gsm or 0)
        wcdma = int(row.wcdma or 0)
        lte = int(row.lte or 0)
        cell = sum((gsm, wcdma, lte))
        regions[code] = {
            'code': code,
            'name': name,
            'order': transliterate(name[:10]).lower(),
            'gsm': gsm,
            'wcdma': wcdma,
            'lte': lte,
            'cell': cell,
            'blue': int(row.blue or 0),
            'wifi': int(row.wifi or 0),
        }
    return sorted(regions.values(), key=itemgetter('cell'), reverse=True)
Exemplo n.º 5
0
    def region_for_code(self, code):
        """
        Return a region instance with metadata for the code or None.

        The return list is filtered by the set of recognized
        region codes present in the GENC dataset.
        """
        if code in self._valid_regions:
            region = genc.region_by_alpha2(code)
            return Region(code=region.alpha2,
                          name=region.name,
                          radius=self.region_max_radius(code))
        return None
Exemplo n.º 6
0
    def region_for_code(self, code):
        """
        Return a region instance with metadata for the code or None.

        The return list is filtered by the set of recognized
        region codes present in the GENC dataset.
        """
        if code in self._valid_regions:
            region = genc.region_by_alpha2(code)
            return Region(
                code=region.alpha2,
                name=region.name,
                radius=self.region_max_radius(code))
        return None
    def lookup(self, addr):
        """
        Look up information for the given IP address.

        :param addr: IP address (e.g. '203.0.113.30')
        :type addr: str

        :returns: A dictionary with city, region data and location data.
        :rtype: dict
        """
        try:
            record = self.city(addr)
        except self.lookup_exceptions:
            # The GeoIP database has no data for this IP or is broken.
            record = None

        if not record:
            return None

        region = record.country
        city = record.city.geoname_id if record.city else None
        subs = []
        if record.subdivisions:
            for sub in record.subdivisions:
                subs.append(sub.iso_code)
        location = record.location
        if not (location.latitude and location.longitude
                and region.iso_code):  # pragma: no cover
            return None

        code = GEOIP_GENC_MAP.get(region.iso_code, region.iso_code).upper()
        radius, region_radius = self.radius(code,
                                            location,
                                            subs=subs,
                                            city=city)

        score = 0.9
        if city:
            score = REGION_SCORE.get(code, 0.3)
        return {
            # Round lat/lon to a standard maximum precision
            'latitude': round(location.latitude, DEGREE_DECIMAL_PLACES),
            'longitude': round(location.longitude, DEGREE_DECIMAL_PLACES),
            'region_code': code,
            'region_name': genc.region_by_alpha2(code).name,
            'city': bool(city),
            'radius': radius,
            'region_radius': region_radius,
            'score': score,
        }
Exemplo n.º 8
0
    def lookup(self, addr):
        """
        Look up information for the given IP address.

        :param addr: IP address (e.g. '203.0.113.30')
        :type addr: str

        :returns: A dictionary with city, region data and location data.
        :rtype: dict
        """
        try:
            record = self.city(addr)
        except self.lookup_exceptions:
            # The GeoIP database has no data for this IP or is broken.
            record = None

        if not record:
            return None

        region = record.country
        city = record.city.geoname_id if record.city else None
        subs = []
        if record.subdivisions:
            for sub in record.subdivisions:
                subs.append(sub.iso_code)
        location = record.location
        if not (location.latitude and
                location.longitude and
                region.iso_code):  # pragma: no cover
            return None

        code = GEOIP_GENC_MAP.get(region.iso_code, region.iso_code).upper()
        radius, region_radius = self.radius(
            code, location, subs=subs, city=city)

        score = 0.9
        if city:
            score = REGION_SCORE.get(code, 0.3)
        return {
            # Round lat/lon to a standard maximum precision
            'latitude': round(location.latitude, DEGREE_DECIMAL_PLACES),
            'longitude': round(location.longitude, DEGREE_DECIMAL_PLACES),
            'region_code': code,
            'region_name': genc.region_by_alpha2(code).name,
            'city': bool(city),
            'radius': radius,
            'region_radius': region_radius,
            'score': score,
        }
Exemplo n.º 9
0
def regions(session):
    rows = session.query(RegionStat).all()
    regions = {}
    for row in rows:
        code = row.region
        name = genc.region_by_alpha2(code).name
        gsm = int(row.gsm or 0)
        wcdma = int(row.wcdma or 0)
        lte = int(row.lte or 0)
        cell = sum((gsm, wcdma, lte))
        regions[code] = {
            'code': code,
            'name': name,
            'order': transliterate(name[:10]).lower(),
            'gsm': gsm,
            'wcdma': wcdma,
            'lte': lte,
            'cell': cell,
            'blue': int(row.blue or 0),
            'wifi': int(row.wifi or 0),
        }
    return sorted(regions.values(), key=itemgetter('cell'), reverse=True)
Exemplo n.º 10
0
def regions(session):
    rows = session.query(RegionStat).all()
    regions = {}
    for row in rows:
        code = row.region
        name = genc.region_by_alpha2(code).name
        gsm = int(row.gsm or 0)
        wcdma = int(row.wcdma or 0)
        lte = int(row.lte or 0)
        cell = sum((gsm, wcdma, lte))
        wifi = int(row.wifi or 0)
        regions[code] = {
            'code': code,
            'name': name,
            'order': transliterate(name[:10]).lower(),
            'gsm': gsm,
            'wcdma': wcdma,
            'lte': lte,
            'cell': cell,
            'wifi': wifi,
        }
    return sorted(regions.values(), key=itemgetter('cell'), reverse=True)
Exemplo n.º 11
0
def test_api_alpha2():
    assert genc.region_by_alpha2('DE').name == 'Germany'
    assert genc.region_by_alpha2('de').name == 'Germany'
    assert genc.region_by_alpha2('None') is None
    assert genc.region_by_alpha2('None', 1) == 1
Exemplo n.º 12
0
Arquivo: tests.py Projeto: alow/genc
 def test_alpha2(self):
     self.assertEqual(genc.region_by_alpha2('DE').name, 'Germany')
     self.assertEqual(genc.region_by_alpha2('de').name, 'Germany')
     self.assertEqual(genc.region_by_alpha2('None'), None)
     self.assertEqual(genc.region_by_alpha2('None', 1), 1)
Exemplo n.º 13
0
Arquivo: tests.py Projeto: alow/genc
 def test_alpha2(self):
     self.assertEqual(genc.region_by_alpha2('DE').name, 'Germany')
     self.assertEqual(genc.region_by_alpha2('de').name, 'Germany')
     self.assertEqual(genc.region_by_alpha2('None'), None)
     self.assertEqual(genc.region_by_alpha2('None', 1), 1)