Closed bobvandevijver closed 7 years ago
If this is fixed, the note in the Readme introduced in #12 should be reverted.
What language do you use on your Fritzbox? Can you post what it says on the "energy" page? That's where I extract the data.
I use English:
This is the diff for it to work:
diff --git a/fritzbox_uptime.py b/fritzbox_uptime.py
index d6e2878..b60ca84 100755
--- a/fritzbox_uptime.py
+++ b/fritzbox_uptime.py
@@ -22,7 +22,7 @@ import sys
import fritzbox_helper as fh
PAGE = '/system/energy.lua'
-pattern = re.compile("(\d+)\s(Tag|Stunden|Minuten)")
+pattern = re.compile("(\d+)\s(days|hours|minutes)")
def get_uptime():
@@ -37,11 +37,11 @@ def get_uptime():
if matches:
hours = 0.0
for m in matches:
- if m.group(2) == 'Tag':
+ if m.group(2) == 'days':
hours += 24 * int(m.group(1))
- if m.group(2) == "Stunden":
+ if m.group(2) == "hours":
hours += int(m.group(1))
- if m.group(2) == "Minuten":
+ if m.group(2) == "minutes":
hours += int(m.group(1)) / 60.0
uptime = hours / 24
print "uptime.value %.2f" % uptime
My approach would have been language-agnostic:
diff --git a/fritzbox_uptime.py b/fritzbox_uptime.py
index d6e2878..68153d8 100755
--- a/fritzbox_uptime.py
+++ b/fritzbox_uptime.py
@@ -22,7 +22,7 @@ import sys
import fritzbox_helper as fh
PAGE = '/system/energy.lua'
-pattern = re.compile("(\d+)\s(Tag|Stunden|Minuten)")
+pattern = re.compile("\s(\d+)\s\w+")
def get_uptime():
@@ -36,12 +36,12 @@ def get_uptime():
matches = re.finditer(pattern, data)
if matches:
hours = 0.0
- for m in matches:
- if m.group(2) == 'Tag':
+ for i, m in enumerate(matches):
+ if i == 0:
hours += 24 * int(m.group(1))
- if m.group(2) == "Stunden":
+ if i == 1:
hours += int(m.group(1))
- if m.group(2) == "Minuten":
+ if i == 2:
hours += int(m.group(1)) / 60.0
uptime = hours / 24
print "uptime.value %.2f" % uptime
But it's also less robust.
Thinking about it, I actually prefer the explicit way:
diff --git a/fritzbox_uptime.py b/fritzbox_uptime.py
index d6e2878..634493e 100755
--- a/fritzbox_uptime.py
+++ b/fritzbox_uptime.py
@@ -16,13 +16,13 @@
#%# capabilities=autoconf
"""
-import os
import re
import sys
+
import fritzbox_helper as fh
PAGE = '/system/energy.lua'
-pattern = re.compile("(\d+)\s(Tag|Stunden|Minuten)")
+pattern = re.compile("(\d+)\s(Tag|Stunden|Minuten|days|hours|minutes)")
def get_uptime():
@@ -37,11 +37,11 @@ def get_uptime():
if matches:
hours = 0.0
for m in matches:
- if m.group(2) == 'Tag':
+ if m.group(2) in ("Tag", "day"):
hours += 24 * int(m.group(1))
- if m.group(2) == "Stunden":
+ if m.group(2) in ("Stunden", "hours"):
hours += int(m.group(1))
- if m.group(2) == "Minuten":
+ if m.group(2) in ("Minuten", "minutes"):
hours += int(m.group(1)) / 60.0
uptime = hours / 24
print "uptime.value %.2f" % uptime
This won't scale to more languages but maybe we should cross that bridge once we get there.
It works, but I've got a better solution in the works currently I think. Basically, it looks like this (PR on the way):
diff --git a/fritzbox_uptime.py b/fritzbox_uptime.py
index d6e2878..60cb226 100755
--- a/fritzbox_uptime.py
+++ b/fritzbox_uptime.py
@@ -21,8 +21,15 @@ import re
import sys
import fritzbox_helper as fh
+locale = "de" if not os.environ.get('locale') else os.environ['locale']
+patternLoc = {"de":"(\d+)\s(Tag|Stunden|Minuten)", \
+ "en":"(\d+)\s(days|hours|minutes)"}
+dayLoc = {"de":"Tag", "en":"days"}
+hourLoc = {"de":"Stunden", "en":"hours"}
+minutesLoc = {"de":"Minuten", "en":"minutes"}
+
PAGE = '/system/energy.lua'
-pattern = re.compile("(\d+)\s(Tag|Stunden|Minuten)")
+pattern = re.compile(patternLoc[locale])
def get_uptime():
@@ -37,11 +44,11 @@ def get_uptime():
if matches:
hours = 0.0
for m in matches:
- if m.group(2) == 'Tag':
+ if m.group(2) == dayLoc[locale]:
hours += 24 * int(m.group(1))
- if m.group(2) == "Stunden":
+ if m.group(2) == hourLoc[locale]:
hours += int(m.group(1))
- if m.group(2) == "Minuten":
+ if m.group(2) == minutesLoc[locale]:
hours += int(m.group(1)) / 60.0
uptime = hours / 24
print "uptime.value %.2f" % uptime
Instead of
locale = "de" if not os.environ.get('locale') else os.environ['locale']
you could do
locale = os.environ.get('locale', 'de')
Yes, you're right. Adjusted in the PR!
(Python ain't my preferred language 😄)
Certain scripts require German, while with a simple localization database this should be easily resolved.