back to python works with 2 and 3

This commit is contained in:
Tommy Huff 2016-10-24 20:52:08 -04:00
parent 33285d8d8c
commit 40fdaea099

View file

@ -14,19 +14,13 @@
#Functions############################################################################################################## #Functions##############################################################################################################
#move to pihole #move to pihole
if [ ! -f "/opt/pihole/jq" ] ; then
curl -s http://stedolan.github.io/jq/download/linux64/jq -o /opt/pihole/jq
chmod 755 /opt/pihole/jq
fi
statsUpdateJSON() { statsUpdateJSON() {
if [[ -z "${AdminLink}" ]] ; then if [[ -z "${AdminLink}" ]] ; then
AdminLink="http://127.0.0.1/admin" AdminLink="http://127.0.0.1/admin"
fi fi
local x=$(curl -s ${AdminLink}/api.php?summaryRaw) local x=$(curl -s ${AdminLink}/api.php?summaryRaw)
#check if json is valid #check if json is valid
if echo "${x}" | /opt/pihole/jq "." > /dev/null ; then if python -c "import sys, json;json.loads('${x}')" ; then
echo "${x}" echo "${x}"
else else
echo "Error" echo "Error"
@ -39,8 +33,7 @@ statsBlockedDomains() {
json=$(statsUpdateJSON) json=$(statsUpdateJSON)
fi fi
if [[ "${json}" != "Error" ]] ; then if [[ "${json}" != "Error" ]] ; then
# local x=$(echo "${json}" | python -c "import sys, json; print json.load(sys.stdin)['domains_being_blocked']") local x=$(python -c "import sys, json;print(json.loads('${json}')['domains_being_blocked'])")
local x=$(echo "${json}" | /opt/pihole/jq ".domains_being_blocked" | tr -d '"')
echo ${x} echo ${x}
else else
echo "Error" echo "Error"
@ -53,7 +46,7 @@ statsQueriesToday() {
json=$(statsUpdateJSON) json=$(statsUpdateJSON)
fi fi
if [[ "${json}" != "Error" ]] ; then if [[ "${json}" != "Error" ]] ; then
local x=$(echo "${json}" | /opt/pihole/jq ".dns_queries_today" | tr -d '"') local x=$(python -c "import sys, json;print(json.loads('${json}')['dns_queries_today'])")
echo ${x} echo ${x}
else else
echo "Error" echo "Error"
@ -66,7 +59,7 @@ statsBlockedToday() {
json=$(statsUpdateJSON) json=$(statsUpdateJSON)
fi fi
if [[ "${json}" != "Error" ]] ; then if [[ "${json}" != "Error" ]] ; then
local x=$(echo "${json}" | /opt/pihole/jq ".ads_blocked_today" | tr -d '"') local x=$(python -c "import sys, json;print(json.loads('${json}')['ads_blocked_today'])")
echo ${x} echo ${x}
else else
echo "Error" echo "Error"
@ -79,7 +72,7 @@ statsPercentBlockedToday() {
json=$(statsUpdateJSON) json=$(statsUpdateJSON)
fi fi
if [[ "${json}" != "Error" ]] ; then if [[ "${json}" != "Error" ]] ; then
local x=$(echo "${json}" | /opt/pihole/jq ".ads_percentage_today" | tr -d '"' | xargs printf "%.*f\n" 2) local x=$(python -c "import sys, json;print(round(float(json.loads('${json}')['ads_percentage_today']), 2))")
echo ${x} echo ${x}
else else
echo "Error" echo "Error"