API parsing via Python (bitbar)


#21

been trying different variation for a while, still no luck

synntax is my enemy


#22

try defining urllib.request.urlopen at url_enable = :wink:

https://docs.python.org/3.0/howto/urllib2.html


#23

url_enable = urllib.request.urlopen("%s/api.php?enable&auth=%s" % (base_url, password))

i get NameError: name urllib is not defined


#24

Try
url_enable = urlopen("%s/api.php?enable&auth=%s" % (base_url, password))

since you already imported urlopen directly.


#25

that does work to disable but breaks the status check so i never get the option to enable, also the pihole is now disabled everytime the script runs


#26

That shouldn’t happen. Unless you altered the get_status() function.


#27

only changed what you posted


#28

It’s because of this.

Every time the scrip is executed, it checks for the status and because Pi-hole is enabled it auto executes the disable function (because of the new urlopen parameter and thus disabling Pi-hole).

You’ll have to adapt the code to execute onl when needed.

Now that you know how to silently send the parameters, go ahead and explore and tinker with the code.

You could implement an added condition to make it more strict …


#29

add:
disabled = status == 'disabled'

and:
print(‘Status: %s’ % status) + “| color=black”
if enabled:
print(‘Disable Pi-hole | color=red href=%s’ % url_disable)
if disabled:
print(‘Enable Pi-hole | color=green href=%s’ % url_enable)


#30

That won’t help, it does nothing at all.

the url_disable gets executed when the function checks for status and sees the status is enabled.
It was designed and implemented as a click that opens the page when clicked (executes url_disable at click - original behavior).

Since we altered the disable code and added urlopen to it, the url_disable when invoked by the IF condition (automatically) disables it (doesn’t wait for click).

You’ll have to write that whole condition differently somehow.


#31

still can’t seem to figure this one out