phantasma/app/video/management/commands/update_geoip.py

30 lines
806 B
Python
Raw Normal View History

2021-10-27 15:19:55 +00:00
import os
import re
import ox
from django.core.management.base import BaseCommand
from django.conf import settings
class Command(BaseCommand):
"""
"""
help = 'update geoip database'
args = ''
def handle(self, **options):
force = False
path = settings.GEOIP_PATH / 'GeoLite2-City.mmdb'
index = ox.net.read_url('https://db-ip.com/db/download/ip-to-city-lite').decode()
match = re.compile('href=[\'"](http.*.mmdb.gz)').findall(index)
if match:
url = match[0]
print('download', url)
ox.net.save_url(url, "%s.gz" % path)
if os.path.exists(path):
os.unlink(path)
os.system('gunzip "%s.gz"' % path)
else:
print('failed to download GeoLite2-City.mmdb')