From 559ab280ca705bca200823a0493308b10aba1dd4 Mon Sep 17 00:00:00 2001 From: nico Date: Thu, 11 Oct 2018 19:28:18 +0200 Subject: * new uptime.py file - removed asyncio from import * fixed bug with muc jid parser * outsorced validate and dedup to misc.py --- classes/uptime.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 classes/uptime.py (limited to 'classes/uptime.py') diff --git a/classes/uptime.py b/classes/uptime.py new file mode 100644 index 0000000..96c8685 --- /dev/null +++ b/classes/uptime.py @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- + + +# XEP-0012: Last Activity +class LastActivity: + """ query the server uptime of the specified domain, defined by XEP-0012 """ + def __init__(self, session, msg, target): + self.session = session + self.nick = msg['mucnick'] + self.message_type = msg['type'] + self.target = target + + async def query(self): + last_activity = await self.session['xep_0012'].get_last_activity(jid=self.target) + seconds = await last_activity['last_activity']['seconds'] + + return seconds + + async def format_values(self, granularity=4): + seconds = await self.query() + #seconds = last_activity['last_activity']['seconds'] + uptime = [] + intervals = ( + ('years', 31536000), # 60 * 60 * 24 * 365 + ('weeks', 604800), # 60 * 60 * 24 * 7 + ('days', 86400), # 60 * 60 * 24 + ('hours', 3600), # 60 * 60 + ('minutes', 60), + ('seconds', 1) + ) + for name, count in intervals: + value = seconds // count + if value: + seconds -= value * count + if value == 1: + name = name.rstrip('s') + uptime.append("{} {}".format(value, name)) + result = ' '.join(uptime[:granularity]) + + if self.message_type == "groupchat": + text = "%s: %s is running since %s" % (self.nick, self.target, result) + else: + text = "%s is running since %s" % (self.target, result) + + return text -- cgit v1.2.3-18-g5258 From 0c313565f2b649366f7382dc1b3f28a3e80f4ffc Mon Sep 17 00:00:00 2001 From: nico Date: Tue, 6 Nov 2018 23:43:11 +0100 Subject: simplification and major rework * updated gitignore file * partly reworked servercontact implementation * complete rework of uptime, version * part rework of xep requests + added more comments to xep requests + added opt_arg to version, xep and contact * complete rework of validate function * updated HandleError function * part rework of StaticStrings function + implemented data dictionary to hold all data in main bot + added message_ids * complete rework of queue building and deduplication --- classes/uptime.py | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) (limited to 'classes/uptime.py') diff --git a/classes/uptime.py b/classes/uptime.py index 96c8685..6eb15dd 100644 --- a/classes/uptime.py +++ b/classes/uptime.py @@ -3,23 +3,19 @@ # XEP-0012: Last Activity class LastActivity: - """ query the server uptime of the specified domain, defined by XEP-0012 """ - def __init__(self, session, msg, target): - self.session = session - self.nick = msg['mucnick'] - self.message_type = msg['type'] - self.target = target - - async def query(self): - last_activity = await self.session['xep_0012'].get_last_activity(jid=self.target) - seconds = await last_activity['last_activity']['seconds'] - - return seconds + """ + query the server uptime of the specified domain, defined by XEP-0012 + """ + def __init__(self): + # init all necessary variables + self.last_activity = None + self.target, self.opt_arg = None, None - async def format_values(self, granularity=4): - seconds = await self.query() - #seconds = last_activity['last_activity']['seconds'] + def process(self, granularity=4): + seconds = self.last_activity['last_activity']['seconds'] uptime = [] + + # touple with displayable time sections intervals = ( ('years', 31536000), # 60 * 60 * 24 * 365 ('weeks', 604800), # 60 * 60 * 24 * 7 @@ -28,6 +24,8 @@ class LastActivity: ('minutes', 60), ('seconds', 1) ) + + # for every element in possible time section process the seconds for name, count in intervals: value = seconds // count if value: @@ -37,9 +35,16 @@ class LastActivity: uptime.append("{} {}".format(value, name)) result = ' '.join(uptime[:granularity]) - if self.message_type == "groupchat": - text = "%s: %s is running since %s" % (self.nick, self.target, result) - else: - text = "%s is running since %s" % (self.target, result) + # insert values into result string + text = "%s is running since %s" % (self.target, result) return text + + def format(self, query, target, opt_arg): + self.last_activity = query + + self.target = target + self.opt_arg = opt_arg + + reply = self.process() + return reply -- cgit v1.2.3-18-g5258