Ejemplo n.º 1
0
def _createNewVisitor(ip, browsers, visits, dynamo_client):
    '''Adds new Visitor data from a visitor-specific DataFrame to the table.

  Parameters
  ----------
  ip : str
    The IP address of the visitor.
  v_df : pd.DataFrame
    The visitor-specific DataFrame that holds the session's data.
  dynamo_client : DynamoClient
    The DynamoDB client used to access the table

  Returns
  -------
  result : dict
    The result of adding the new visitor and their data to the table. This
    could be new visitor, location, browser, session, and visits added or the
    error that occurred.
  '''
    result = dynamo_client.addNewVisitor(
        Visitor(ip, 1),  # Visitor
        requestToLocation(
            json.loads(
                http.request(
                    'GET',
                    f'''https://geo.ipify.org/api/v1?apiKey={ os.environ.get('IPIFY_KEY')
        }&ipAddress={ ip }''').data.decode('utf8'))),  # Location
        browsers,  # Browsers
        visits  # Visits
    )
    if 'error' in result.keys():
        print('ERROR _createNewSession ' + result['error'])
    return result
Ejemplo n.º 2
0
def test_postal_code_requestToLocation():
    location = requestToLocation(
        {
            'ip': '0.0.0.0',
            'location': {
                'country': 'US',
                'region': 'California',
                'city': 'Westlake Village',
                'lat': 34.14584,
                'lng': -118.80565,
                'postalCode': '',
                'timezone': '-08:00',
                'geonameId': 5408395
            },
            'domains': ['cpe-75-82-84-171.socal.res.rr.com'],
            'as': {
                'asn': 20001,
                'name': 'Charter Communications (20001)',
                'route': '75.82.0.0/15',
                'domain': 'https://www.spectrum.com',
                'type': 'Cable/DSL/ISP'
            },
            'isp': 'Charter Communications',
            'proxy': {
                'proxy': False,
                'vpn': False,
                'tor': False
            }
        }, '171a0329-f8b2-499c-867d-1942384ddd5f')
    assert location.id == '171a0329-f8b2-499c-867d-1942384ddd5f'
    assert location.ip == '0.0.0.0'
    assert location.country == 'US'
    assert location.region == 'California'
    assert location.city == 'Westlake Village'
    assert location.latitude == 34.14584
    assert location.longitude == -118.80565
    assert location.postalCode is None
    assert location.timeZone == '-08:00'
    assert location.domains == ['cpe-75-82-84-171.socal.res.rr.com']
    assert location.autonomousSystem == {
        'asn': 20001,
        'name': 'Charter Communications (20001)',
        'route': '75.82.0.0/15',
        'domain': 'https://www.spectrum.com',
        'type': 'Cable/DSL/ISP'
    }
    assert location.isp == 'Charter Communications'
    assert not location.proxy
    assert not location.vpn
    assert not location.tor
Ejemplo n.º 3
0
def test_exception_requestToLocation():
    with pytest.raises(Exception) as e:
        assert requestToLocation({}, '171a0329-f8b2-499c-867d-1942384ddd5f')
    assert str(e.value) == 'Could not parse location'
Ejemplo n.º 4
0
def test_exception_requestToLocation():
  with pytest.raises( Exception ) as e:
    assert requestToLocation( {} )
  assert str( e.value ) == 'Could not parse location'