Added IP Database auto-updater

This commit is contained in:
marios8543 2019-11-30 17:18:47 +02:00
parent 993482612a
commit 1e81ebc381
3 changed files with 68 additions and 0 deletions

View File

@ -0,0 +1,42 @@
from requests import get
from pymysql import connect
import os
from zipfile import ZipFile
from io import BytesIO
res = get("https://www.ip2location.com/download?token={}&file=DB3LITE".format(os.getenv("token")))
conn = connect(host=getenv("db_host"),user=os.getenv("db_user"),password=os.getenv("db_pass"),db=getenv("db_db"),local_infile=True)
db = conn.cursor()
file = BytesIO()
db.execute("""
CREATE TABLE IF NOT EXISTS `ip2location_db3`(
`ip_from` INT(10) UNSIGNED,
`ip_to` INT(10) UNSIGNED,
`country_code` CHAR(2),
`country_name` VARCHAR(64),
`region_name` VARCHAR(128),
`city_name` VARCHAR(128),
INDEX `idx_ip_from` (`ip_from`),
INDEX `idx_ip_to` (`ip_to`),
INDEX `idx_ip_from_to` (`ip_from`, `ip_to`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
""")
if res.ok:
file.write(res.content)
with ZipFile(file) as zip:
zip.extract("IP2LOCATION-LITE-DB3.CSV")
db.execute("TRUNCATE TABLE `ip2location_db3`")
db.execute("""
LOAD DATA LOCAL
INFILE '{}/IP2LOCATION-LITE-DB3.CSV'
INTO TABLE
`ip2location_db3`
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\r\n'
IGNORE 0 LINES;
""".format(os.path.dirname(os.path.realpath(__file__))))
else:
print("Could not download. ({}) ({})".format(res.status_code,res.content))

View File

@ -0,0 +1,15 @@
[Unit]
Description=StoryBear with intergrated web server
After=network.target mysql.service
[Service]
User=marios
ExecStart=/usr/bin/python3 /home/ipdb-downloader.py #Replace this according to where you've saved the script
Environment=token=IP2LOCATION_TOKEN #Your IP2LOCATION token. You can get this Register on: https://www.ip2location.com/
Environment=db_host=127.0.0.1 #The address of your mysql server
Environment=db_user=root #Username for mysql
Environment=db_pass=MYSQL_PASSWORD #Password for mysql
Environment=db_db=ip_logging #Database for mysql
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,11 @@
[Unit]
Description=Update ip database every month
[Timer]
OnCalendar=monthly
Persistent=true
[Install]
WantedBy=timers.target