11"""Example for the usage of the hole module."""
2+
23import asyncio
34import json
45from datetime import datetime
1314PORT = 443
1415VERIFY_TLS = False
1516
17+
1618async def main ():
1719 """Get the data from a Pi-hole instance."""
1820 async with aiohttp .ClientSession () as session :
@@ -22,14 +24,20 @@ async def main():
2224 password = PASSWORD ,
2325 protocol = PROTOCOL ,
2426 port = PORT ,
25- verify_tls = VERIFY_TLS
27+ verify_tls = VERIFY_TLS ,
2628 ) as pihole :
2729 await pihole .get_data ()
2830
2931 print ("\n === Version Information ===" )
30- print (f"Core Version: { pihole .core_current } (Latest: { pihole .core_latest } , Update Available: { pihole .core_update } )" )
31- print (f"Web Version: { pihole .web_current } (Latest: { pihole .web_latest } , Update Available: { pihole .web_update } )" )
32- print (f"FTL Version: { pihole .ftl_current } (Latest: { pihole .ftl_latest } , Update Available: { pihole .ftl_update } )" )
32+ print (
33+ f"Core Version: { pihole .core_current } (Latest: { pihole .core_latest } , Update Available: { pihole .core_update } )"
34+ )
35+ print (
36+ f"Web Version: { pihole .web_current } (Latest: { pihole .web_latest } , Update Available: { pihole .web_update } )"
37+ )
38+ print (
39+ f"FTL Version: { pihole .ftl_current } (Latest: { pihole .ftl_latest } , Update Available: { pihole .ftl_update } )"
40+ )
3341
3442 print ("\n === Basic Statistics ===" )
3543 print (f"Status: { pihole .status } " )
@@ -57,22 +65,31 @@ async def main():
5765
5866 print ("\n === Forward Destinations ===" )
5967 for upstream in pihole .forward_destinations :
60- print (f"Name: { upstream ['name' ]} , IP: { upstream ['ip' ]} , Count: { upstream ['count' ]} " )
68+ print (
69+ f"Name: { upstream ['name' ]} , IP: { upstream ['ip' ]} , Count: { upstream ['count' ]} "
70+ )
6171
6272 print ("\n === Reply Types ===" )
6373 for reply_type , count in pihole .reply_types .items ():
6474 print (f"{ reply_type } : { count } " )
6575
6676 print ("\n === Raw Data ===" )
67- print (json .dumps ({
68- "data" : pihole .data ,
69- "blocked_domains" : pihole .blocked_domains ,
70- "permitted_domains" : pihole .permitted_domains ,
71- "clients" : pihole .clients ,
72- "upstreams" : pihole .upstreams ,
73- "blocking_status" : pihole .blocking_status ,
74- "versions" : pihole .versions
75- }, indent = 4 , sort_keys = True ))
77+ print (
78+ json .dumps (
79+ {
80+ "data" : pihole .data ,
81+ "blocked_domains" : pihole .blocked_domains ,
82+ "permitted_domains" : pihole .permitted_domains ,
83+ "clients" : pihole .clients ,
84+ "upstreams" : pihole .upstreams ,
85+ "blocking_status" : pihole .blocking_status ,
86+ "versions" : pihole .versions ,
87+ },
88+ indent = 4 ,
89+ sort_keys = True ,
90+ )
91+ )
92+
7693
7794async def toggle_blocking ():
7895 """Example of enabling and disabling Pi-hole blocking."""
@@ -83,18 +100,20 @@ async def toggle_blocking():
83100 password = PASSWORD ,
84101 protocol = PROTOCOL ,
85102 port = PORT ,
86- verify_tls = VERIFY_TLS
103+ verify_tls = VERIFY_TLS ,
87104 ) as pihole :
88105 await pihole .get_data ()
89106 initial_status = pihole .status
90107 print (f"\n Initial Pi-hole status: { initial_status } " )
91108
92109 print ("\n Disabling Pi-hole blocking for 60 seconds..." )
93110 disable_result = await pihole .disable (duration = 60 )
94-
111+
95112 await pihole .get_data ()
96113 if pihole .status != "disabled" :
97- print (f"ERROR: Failed to disable Pi-hole! Status is still: { pihole .status } " )
114+ print (
115+ f"ERROR: Failed to disable Pi-hole! Status is still: { pihole .status } "
116+ )
98117 return
99118 print (f"Successfully disabled Pi-hole. Status: { pihole .status } " )
100119 print (f"Disable operation response: { disable_result } " )
@@ -104,20 +123,27 @@ async def toggle_blocking():
104123
105124 print ("\n Enabling Pi-hole blocking..." )
106125 enable_result = await pihole .enable ()
107-
126+
108127 await pihole .get_data ()
109128 if pihole .status != "enabled" :
110- print (f"ERROR: Failed to enable Pi-hole! Status is still: { pihole .status } " )
129+ print (
130+ f"ERROR: Failed to enable Pi-hole! Status is still: { pihole .status } "
131+ )
111132 return
112133 print (f"Successfully enabled Pi-hole. Status: { pihole .status } " )
113134 print (f"Enable operation response: { enable_result } " )
114135
115136 if pihole .status == initial_status :
116- print ("\n Toggle test completed successfully! Pi-hole returned to initial state." )
137+ print (
138+ "\n Toggle test completed successfully! Pi-hole returned to initial state."
139+ )
117140 else :
118- print (f"\n WARNING: Final status ({ pihole .status } ) differs from initial status ({ initial_status } )" )
141+ print (
142+ f"\n WARNING: Final status ({ pihole .status } ) differs from initial status ({ initial_status } )"
143+ )
144+
119145
120146if __name__ == "__main__" :
121147 print (f"=== Pi-hole Statistics as of { datetime .now ()} ===" )
122148 asyncio .run (main ())
123- asyncio .run (toggle_blocking ())
149+ asyncio .run (toggle_blocking ())
0 commit comments