diff --git a/IPDB_Updater/ipdb-downloader.py b/IPDB_Updater/ipdb-downloader.py new file mode 100644 index 0000000..c2a71b1 --- /dev/null +++ b/IPDB_Updater/ipdb-downloader.py @@ -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)) diff --git a/IPDB_Updater/ipdb_update.service b/IPDB_Updater/ipdb_update.service new file mode 100644 index 0000000..5344a26 --- /dev/null +++ b/IPDB_Updater/ipdb_update.service @@ -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 diff --git a/IPDB_Updater/ipdb_update.timer b/IPDB_Updater/ipdb_update.timer new file mode 100644 index 0000000..06d95a5 --- /dev/null +++ b/IPDB_Updater/ipdb_update.timer @@ -0,0 +1,11 @@ +[Unit] +Description=Update ip database every month + +[Timer] +OnCalendar=monthly +Persistent=true + +[Install] +WantedBy=timers.target + +