Skip to content

Commit 81efb97

Browse files
author
Stewerio De Santo
committed
add GeoLite2 support
1 parent b929cb4 commit 81efb97

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

models.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from proxy_py import settings
22
import peewee
33
import peewee_async
4+
import os.path
5+
import geoip2.database
46

57
raw_db = peewee_async.PooledPostgresqlDatabase(
68
*settings.DATABASE_CONNECTION_ARGS,
@@ -52,15 +54,31 @@ class Meta:
5254
# TODO: consider storing as binary
5355
_white_ipv4 = peewee.CharField(16, null=True)
5456
_white_ipv6 = peewee.CharField(45, null=True)
55-
city = peewee.TextField(null=True)
56-
region = peewee.TextField(null=True)
57-
country_code = peewee.CharField(3, null=True)
58-
# TODO: add location
5957

6058
def get_raw_protocol(self):
6159
return self.raw_protocol
62-
60+
6361
@property
62+
def location(self):
63+
if os.path.exists(settings.GEOLITE2_CITY_FILE_LOCATION) and os.path.isfile(settings.GEOLITE2_CITY_FILE_LOCATION):
64+
reader = geoip2.database.Reader(settings.GEOLITE2_CITY_FILE_LOCATION)
65+
response = reader.city(domain)
66+
67+
return {
68+
'latitude': response.location.latitude,
69+
'longitude': response.location.longitude,
70+
'country_code': response.country.iso_code,
71+
'country': response.country.name,
72+
'city': response.city.name,
73+
}
74+
else:
75+
print('ff')
76+
'''
77+
DB doesn`t exists
78+
'''
79+
return {}
80+
81+
@property
6482
def address(self):
6583
return self.to_url()
6684

processor.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -358,14 +358,6 @@ async def create_or_update_proxy(
358358
if additional_info.ipv4 is not None:
359359
proxy.white_ipv4 = additional_info.ipv4
360360

361-
if additional_info.city is not None:
362-
proxy.city = additional_info.city
363-
364-
if additional_info.region is not None:
365-
proxy.region = additional_info.region
366-
367-
if additional_info.country_code is not None:
368-
proxy.country_code = additional_info.country_code.strip().lower()
369361

370362
checking_time = int(end_checking_time - start_checking_time)
371363
if checking_time > settings.PROXY_CHECKING_TIMEOUT:

proxy_py/_settings.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
Database settings (do not try to change after creation of the database)
99
"""
10+
GEOLITE2_CITY_FILE_LOCATION = '/tmp/proxy_py_9910549a_7d41_4102_9e9d_15d39418a5cb/GeoLite2-City.mmdb'
1011

1112
DATABASE_CONNECTION_ARGS = ()
1213
DATABASE_CONNECTION_KWARGS = {
@@ -82,7 +83,7 @@
8283
PROXY_PROVIDER_SERVER_API_CONFIG_FETCH_CONFIG = {
8384
'fields': [
8485
'address', 'protocol', 'auth_data', 'domain', 'port', 'last_check_time', 'next_check_time', 'number_of_bad_checks',
85-
'bad_proxy', 'uptime', 'response_time', 'white_ipv4', 'white_ipv6', 'city', 'country_code', 'region'
86+
'bad_proxy', 'uptime', 'response_time', 'white_ipv4', 'white_ipv6', 'location'
8687
],
8788
'filter_fields': [
8889
'last_check_time', 'protocol', 'number_of_bad_checks', 'bad_proxy', 'uptime', 'response_time'

0 commit comments

Comments
 (0)