From 505da62f2ad7f6839c07f22c5cc413d6ecd49600 Mon Sep 17 00:00:00 2001 From: nico Date: Fri, 8 Nov 2019 14:55:49 +0100 Subject: slimmed down methods (#4) * slimmed down config class + add start stop arguments to config class init * update requirements.txt * correctly apply custom timestamp to report functions too * init report methods earlier --- main.py | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) (limited to 'main.py') diff --git a/main.py b/main.py index 3cec813..60e65f5 100755 --- a/main.py +++ b/main.py @@ -11,7 +11,6 @@ import sys import tabulate from defusedxml import ElementTree -from config import Config from report import ReportDomain @@ -25,9 +24,9 @@ class AbuseReport: self.start = arguments.start self.stop = arguments.stop self.path = os.path.dirname(__file__) - self.config = Config() self.conn = sqlite3.connect("/".join([self.path, "spam.db"])) + self.Report = ReportDomain(self.conn) self.jid_pattern = re.compile("^(?:([^\"&'/:<>@]{1,1023})@)?([^/@]{1,1023})(?:/(.{1,1023}))?$") self.message_pattern = re.compile(r'', re.DOTALL) @@ -79,6 +78,9 @@ class AbuseReport: # set stop value to now self.stop = dt.datetime.strftime(dt.datetime.now(), '%Y-%m-%dT%H:%M:%S') + # add validated timestamps to report class + self.Report.addtime(self.start, self.stop) + # if one or more domains are specified return only their info if self.domain is not None: @@ -86,11 +88,10 @@ class AbuseReport: for domain in self.domain: # build and execute - sql = '''SELECT COUNT(*) AS messages, COUNT(DISTINCT user) AS bots, domain, MIN(ts) AS first, MAX(ts) AS last \ - FROM spam \ - WHERE domain = :domain \ - AND ts > :start \ - AND ts < :stop;''' + sql = '''SELECT COUNT(*) AS messages, COUNT(DISTINCT user) AS bots, domain, MIN(ts) AS first, MAX(ts) AS last + FROM spam + WHERE domain = :domain + AND ts > :start AND ts < :stop;''' parameter = { "domain": domain, "start": self.start, @@ -114,15 +115,14 @@ class AbuseReport: else: # build and execute - sql = '''SELECT COUNT(*) AS messages, COUNT(DISTINCT user) AS bots, domain AS domain from spam \ - WHERE ts > :start \ - AND ts < :stop \ + sql = '''SELECT COUNT(*) AS messages, COUNT(DISTINCT user) AS bots, domain AS domain from spam + WHERE ts > :start AND ts < :stop GROUP BY domain ORDER BY 1 DESC LIMIT 10;''' result = self.conn.execute(sql, {"start": self.start, "stop": self.stop}).fetchall() # tabelize data - spam_table = tabulate.tabulate(result, headers=["messages", "bots", "domain", "first seen", "last seen"], - tablefmt="github") + spam_table = tabulate.tabulate(result, tablefmt="psql", headers=["messages", "bots", "domain","first seen", + "last seen"]) # output to stdout output = "\n\n".join([spam_table]) @@ -203,9 +203,6 @@ class AbuseReport: :param domain: string containing a domain name :param query: list of tuples containing the query results for the specified domain/s """ - # init report class - report = ReportDomain(self.config, self.conn) - try: # open abuse report template file with open("/".join([self.path, "template/abuse-template.txt"]), "r", encoding="utf-8") as template: @@ -225,15 +222,15 @@ class AbuseReport: # write report files with open("/".join([self.path, "report", report_filename]), "w", encoding="utf-8") as report_out: - content = report.template(report_template, domain, query) + content = self.Report.template(report_template, domain, query) report_out.write(content) with open("/".join([self.path, "report", jids_filename]), "w", encoding="utf-8") as report_out: - content = report.jids(domain) + content = self.Report.jids(domain) report_out.write(content) with open("/".join([self.path, "report", logs_filename]), "w", encoding="utf-8") as report_out: - content = report.logs(domain) + content = self.Report.logs(domain) report_out.write(content) -- cgit v1.2.3-18-g5258