Commit b572ffec authored by Romain Courteaud's avatar Romain Courteaud

Add configuration file

parent 1c4c8adf
......@@ -8,14 +8,20 @@ def runUrlChecker():
@runUrlChecker.command("bot", short_help="Runs url checker bot.")
@click.argument("url")
@click.argument("sqlite_path")
def runWebBot(url, sqlite_path):
from urlchecker_http import WebBot
@click.option("--url", "-u", help="The url to check.")
@click.option("--sqlite", "-s", help="The path of the sqlite DB.")
@click.argument("configuration")
def runWebBot(url, sqlite, configuration):
from urlchecker_http import create_bot
click.echo("Running url checker bot")
bot = WebBot()
return bot.run(url, sqlite_path)
mapping = {}
if url:
mapping["URL"] = url
if sqlite:
mapping["SQLITE"] = sqlite
bot = create_bot(cfgfile=configuration, mapping=mapping)
return bot.run()
if __name__ == "__main__":
......
......@@ -9,11 +9,14 @@ import dns.resolver
import miniupnpc
import platform
from urlchecker_db import LogDB
import configparser
import os
__version__ = "0.0.3"
PREFERRED_TYPE = "text/html"
TIMEOUT = 2
CONFIG_SECTION = "URLCHECKER"
class BotError(Exception):
......@@ -21,6 +24,10 @@ class BotError(Exception):
class WebBot:
def __init__(self):
self.config = configparser.ConfigParser(empty_lines_in_values=False)
self.config[CONFIG_SECTION] = {}
def initDB(self, sqlite_path):
self._db = LogDB(sqlite_path)
self._db.createTables()
......@@ -110,9 +117,9 @@ class WebBot:
print(ip, hostname, response.status_code)
def run(self, url, sqlite_path):
def run(self):
print(time.strftime("%Y-%m-%d %H:%M:%S"))
self.initDB(sqlite_path)
self.initDB(self.config[CONFIG_SECTION]["SQLITE"])
self._db.storeEntry(platform=platform.platform())
print("Platform", platform.platform())
......@@ -138,10 +145,29 @@ class WebBot:
except Exception:
pass
try:
self.check(url)
except KeyboardInterrupt:
self.stop()
except:
print("Oups, error")
raise
for url in self.config[CONFIG_SECTION]["URL"].split():
try:
self.check(url)
except KeyboardInterrupt:
self.stop()
except:
print("Oups, error")
raise
def create_bot(envvar="URLCHECKER_SETTINGS", cfgfile=None, mapping=None):
bot = WebBot()
if (envvar is not None) and (envvar in os.environ):
bot.config.read([os.environ.get(envvar)])
if cfgfile is not None:
print(cfgfile)
bot.config.read([cfgfile])
if mapping is not None:
bot.config.read_dict({CONFIG_SECTION: mapping})
for parameter in ["URL", "SQLITE"]:
if parameter not in bot.config[CONFIG_SECTION]:
raise AttributeError("Config %s not defined" % parameter)
return bot
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment