Python script to display stats in MacOS menu bar app

Hey guys,

I just wanted to share this script I use, in combination with Platypus to display my Pihole info on my MacBook Pro's menu bar.

I used Python and based my code on a BitBar plugin by the user matryer.

I used Platypus to wrap my code into a MacOs menu bar app.

WARNING: I am very new to coding.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

import webbrowser
import sys
import json
import lxml
import requests
import pihole as ph
from bs4 import BeautifulSoup

def getDivider():
    print('----')

pihole = ph.PiHole('<Your Pi-hole IP>')
pihole.authenticate('<Your Pi-hole Web Admin Password>')
adminLoginUrl = '<Your Pi-hole IP>/admin/index.php?login'
adminRequestUrl = ' <Your Pi-hole IP>/admin/'
adminPass = {'pass': '<Your Pi-hole Web Admin Password>'}
adminHash = '<Your Pi-hole Web Admin Hash>'


if len(sys.argv) == 1:
    # Log into Pi-Hole Admin page
    with requests.Session() as session:
        post = session.post(adminLoginUrl, data=adminPass)

        # Collect and parse admin page Status menu
        request = session.get(adminRequestUrl)
        soup = BeautifulSoup(request.text, 'html.parser')

        # Remove <i> tags
        iTags = soup.find(class_='fa fa-circle text-green-light')
        iTags.decompose()

        # Pull all text from the pull-left info div
        piholeStatusList = soup.find(id='temperature')

        # Pull all text from all instances of <span> tag within pull-left info div
        piholeStatusListData = piholeStatusList.find_all('span')

        hostName = soup.find('code').get_text()
        status = soup.find(id='status').get_text()
        temp = soup.find(id='rawtemp').get_text()
        load = soup.find(title='Detected 4 cores').get_text()
        for memStatus in piholeStatusListData:
            mem = memStatus.contents

        print('Status')
        getDivider()
        if status == ' Active':
            print('🟢' + status)
        else:
            print('🔴' + status)
        if (float(temp) < 60.0):
            print('🟢 CPU temp: ' + str(int(float(temp)) * 9 / 5 + 32) + ' °F')
        elif (float(temp) >= 60.0 & float(temp) <= 85.0):
            print('🟠 CPU temp: ' + str(int(float(temp)) * 9 / 5 + 32) + ' °F')
        else:
            print('🔴 CPU temp: ' + str(int(float(temp)) * 9 / 5 + 32) + ' °F')
        print('🟢 Load:' + load[7:])
        if (float(mem[1][15:19]) <= 75.0):
            print('🟢 Memory usage:' + mem[1][15:19] + " %")
        else:
            print('🔴 Memory usage:' + mem[1][15:19] + " %")
        getDivider()
        print('System')
        getDivider()
        print('Pi-Hole Core: ' + pihole.getVersion()['core_current'], end='')
        if pihole.getVersion()['core_update'] == False:
            print(' (up to date)')
        else:
            print(' ❗(out of date)')
        print('Web Interface: ' + pihole.getVersion()['web_current'], end='')
        if pihole.getVersion()['web_update'] == False:
            print(' (up to date)')
        else:
            print(' ❗(out of date)')
        print('FTL: ' + pihole.getVersion()['FTL_current'], end=''),
        if pihole.getVersion()['FTL_update'] == False:
            print(' (up to date)')
        else:
            print(' ❗(out of date)')
        print('Gravity last updated: ' + str(pihole.gravity_last_updated['relative']['days']) + ' days ago')
        getDivider()
        print('Data')
        getDivider()
        print('Domains on Blocklist: ' + pihole.domain_count)
        print('Total queries: ' + pihole.queries)
        print('Queries blocked: ' + pihole.blocked)
        print('Percent blocked: ' + pihole.ads_percentage + ' %')
        print('Database size: ' + str(int(pihole.getDBfilesize() / 1048576)) + ' MB')
        print('Recently blocked: ' + str(requests.get('<Your Pi-hole IP>/admin/api.php?recentBlocked').text))
        getDivider()
        print('Admin')
        getDivider()
        print('Hostname: ' + hostName)
        if status == ' Active':
            print('Disable')
        else:
            print('❗Enable')
        print('Go to admin page…')
elif sys.argv[1] == 'Go to admin page…':
        webbrowser.open(adminLoginUrl)
elif sys.argv[1] == 'Disable':
        webbrowser.open(adminRequestUrl + 'api.php?disable&auth=' + adminHash)
elif sys.argv[1] == '❗Enable':
        webbrowser.open(adminRequestUrl + 'api.php?enable&auth=' + adminHash)
else:
    quit()

Please feel free to use however you like and let me know what you think! Thank you Pi-hole and your amazing work and all the people here that support and help us!

1 Like