summaryrefslogtreecommitdiffstats
path: root/classes/uptime.py
diff options
context:
space:
mode:
authornico <nico@magicbroccoli.de>2018-11-11 03:12:11 +0100
committernico <nico@magicbroccoli.de>2018-11-11 03:13:33 +0100
commit69951bba37a85cf7527d08861ab1f2715576df49 (patch)
tree3798538b14cc67e7deed4adc44957b4cc4607019 /classes/uptime.py
parent6bb9f1d5b87537ed6bccf0dd6efb7b80c6a81395 (diff)
parent1b13bdfd926e394cab2a2edd15ecabf0afcc4cf2 (diff)
Merge branch 'dev'
+ added more comments to xep requests + added opt_arg to version, xep and contact + implemented data dictionary to hold all data in main bot + added message_ids * updated gitignore file * partly reworked servercontact implementation * complete rework of uptime, version * part rework of xep requests * complete rework of validate function * updated HandleError function * part rework of StaticStrings function * complete rework of queue building and deduplication * logging parameter fix
Diffstat (limited to 'classes/uptime.py')
-rw-r--r--classes/uptime.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/classes/uptime.py b/classes/uptime.py
new file mode 100644
index 0000000..6eb15dd
--- /dev/null
+++ b/classes/uptime.py
@@ -0,0 +1,50 @@
+# -*- coding: utf-8 -*-
+
+
+# XEP-0012: Last Activity
+class LastActivity:
+ """
+ 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
+
+ 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
+ ('days', 86400), # 60 * 60 * 24
+ ('hours', 3600), # 60 * 60
+ ('minutes', 60),
+ ('seconds', 1)
+ )
+
+ # for every element in possible time section process the seconds
+ 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])
+
+ # 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