Skip to content

Commit fb6ca09

Browse files
authored
Merge pull request #5 from s0d3s/master
Implement determining location of proxies
2 parents b929cb4 + d58726f commit fb6ca09

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

models.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from proxy_py import settings
22
import peewee
33
import peewee_async
4+
import os.path
5+
import geoip2.database
6+
import logging
7+
8+
log = logging.getLogger("proxy_py/main")
49

510
raw_db = peewee_async.PooledPostgresqlDatabase(
611
*settings.DATABASE_CONNECTION_ARGS,
@@ -52,15 +57,31 @@ class Meta:
5257
# TODO: consider storing as binary
5358
_white_ipv4 = peewee.CharField(16, null=True)
5459
_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
5960

6061
def get_raw_protocol(self):
6162
return self.raw_protocol
62-
63+
6364
@property
65+
def location(self):
66+
if os.path.isfile(settings.GEOLITE2_CITY_FILE_LOCATION):
67+
reader = geoip2.database.Reader(settings.GEOLITE2_CITY_FILE_LOCATION)
68+
response = reader.city(self.domain)
69+
70+
return {
71+
'latitude': response.location.latitude,
72+
'longitude': response.location.longitude,
73+
'country_code': response.country.iso_code,
74+
'country': response.country.name,
75+
'city': response.city.name,
76+
}
77+
else:
78+
'''
79+
DB doesn`t exists
80+
'''
81+
log.warning("Public IP Database has been absent")
82+
return None
83+
84+
@property
6485
def address(self):
6586
return self.to_url()
6687

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'

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ pytest
1414
pytest-asyncio
1515
termcolor
1616
uvloop
17+
geoip2

0 commit comments

Comments
 (0)