MCPcopy
hub / github.com/HelloZeroNet/ZeroNet / downloadGeoLiteDb

Method downloadGeoLiteDb

plugins/Sidebar/SidebarPlugin.py:552–602  ·  view source on GitHub ↗
(self, db_path)

Source from the content-addressed store, hash-verified

550 self.response(to, "".join(body))
551
552 def downloadGeoLiteDb(self, db_path):
553 import gzip
554 import shutil
555 from util import helper
556
557 if config.offline:
558 return False
559
560 self.log.info("Downloading GeoLite2 City database...")
561 self.cmd("progress", ["geolite-info", _["Downloading GeoLite2 City database (one time only, ~20MB)..."], 0])
562 db_urls = [
563 "https://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz",
564 "https://raw.githubusercontent.com/texnikru/GeoLite2-Database/master/GeoLite2-City.mmdb.gz"
565 ]
566 for db_url in db_urls:
567 downloadl_err = None
568 try:
569 # Download
570 response = helper.httpRequest(db_url)
571 data_size = response.getheader('content-length')
572 data_recv = 0
573 data = io.BytesIO()
574 while True:
575 buff = response.read(1024 * 512)
576 if not buff:
577 break
578 data.write(buff)
579 data_recv += 1024 * 512
580 if data_size:
581 progress = int(float(data_recv) / int(data_size) * 100)
582 self.cmd("progress", ["geolite-info", _["Downloading GeoLite2 City database (one time only, ~20MB)..."], progress])
583 self.log.info("GeoLite2 City database downloaded (%s bytes), unpacking..." % data.tell())
584 data.seek(0)
585
586 # Unpack
587 with gzip.GzipFile(fileobj=data) as gzip_file:
588 shutil.copyfileobj(gzip_file, open(db_path, "wb"))
589
590 self.cmd("progress", ["geolite-info", _["GeoLite2 City database downloaded!"], 100])
591 time.sleep(2) # Wait for notify animation
592 self.log.info("GeoLite2 City database is ready at: %s" % db_path)
593 return True
594 except Exception as err:
595 download_err = err
596 self.log.error("Error downloading %s: %s" % (db_url, err))
597 pass
598 self.cmd("progress", [
599 "geolite-info",
600 _["GeoLite2 City database download error: {}!<br>Please download manually and unpack to data dir:<br>{}"].format(download_err, db_urls[0]),
601 -100
602 ])
603
604 def getLoc(self, geodb, ip):
605 global loc_cache

Callers 1

getGeoipDbMethod · 0.95

Calls 8

httpRequestMethod · 0.80
errorMethod · 0.80
formatMethod · 0.80
cmdMethod · 0.45
readMethod · 0.45
writeMethod · 0.45
tellMethod · 0.45
seekMethod · 0.45

Tested by

no test coverage detected