diff --git a/PiholeAPI.py b/PiholeAPI.py index 745efab..bcccfb4 100644 --- a/PiholeAPI.py +++ b/PiholeAPI.py @@ -72,6 +72,7 @@ class PiholeAPI: # comment: default fallback print() print(response.__dict__) + print(response.raw) return False # ## Auth based methods @@ -135,7 +136,7 @@ class PiholeAPI: data = self.__query('auth/sessions') if data: return data['sessions'] - return False + return None def delete_session(self, session_id: int = None) -> bool: """Delete session by ID @@ -157,6 +158,14 @@ class PiholeAPI: logging.info(f'successfull delete session with {session_id=}') return True + def clear_sessions(self): + data = self.get_sessions() + for session in data: + if not session['current_session']: + if not self.delete_session(session['id']): + return False + return True + def get_app_password(self) -> dict | None: """Create new application password @@ -170,10 +179,106 @@ class PiholeAPI: data = self.__query('auth/app') return data['app'] + # + ## metrics based methods + # + def get_history(self) -> list | None: + """Get activity graph data + + Returns: + list | None: if successfull: list of history query data + """ + data = self.__query('history') + if not data: + return None + return data['history'] + + def get_history_timerange(self, starttime: int | datetime, endtime: int | datetime) -> list | None: + """Get activity graph data (long-term data) + + # TODO: BUG - always response with You need to specify both \\"from\\" and \\"until\\" in the request. + + Args: + starttime (int | datetime): start time of data collection (int as timestamp) + endtime (int | datetime): end time of data collection (int as timestamp) + + Returns: + list | None: if successfull: list of history query data in given time range + """ + payload = dict() + if isinstance(starttime, int) and isinstance(endtime, int): + payload['from'] = starttime + payload['until'] = endtime + if isinstance(starttime, datetime) and isinstance(endtime, datetime): + payload['from'] = int(starttime.timestamp()) + payload['until'] = int(endtime.timestamp()) + + if len(payload.keys()) > 0: + data = self.__query('history/database', payload=payload) + if not data: + return None + return data + + + def get_client_history(self) -> list | None: + """Get per-client activity graph data + + Returns: + list | None: if successfull: list of history query data by client + """ + data = self.__query('history/clients') + if not data: + return None + return data['history'] + + def get_client_history_timerange(self, starttime: int | datetime, endtime: int | datetime) -> list | None: + """Get activity graph data (long-term data) + + # TODO: BUG - always response with You need to specify both \\"from\\" and \\"until\\" in the request. + + Args: + starttime (int | datetime): start time of data collection (int as timestamp) + endtime (int | datetime): end time of data collection (int as timestamp) + + Returns: + list | None: if successfull: list of history query data by client in given time range + """ + payload = dict() + if isinstance(starttime, int) and isinstance(endtime, int): + payload['from'] = starttime + payload['until'] = endtime + if isinstance(starttime, datetime) and isinstance(endtime, datetime): + payload['from'] = int(starttime.timestamp()) + payload['until'] = int(endtime.timestamp()) + + if len(payload.keys()) > 0: + data = self.__query('history/database', payload=payload) + if not data: + return None + return data + + def get_queries(self) -> list | None: + """Request query details. Query parameters may be used to limit the number of results. + + # TODO: add arguments + + Returns: + list | None: By default, this API callback returns the most recent 100 queries. + """ + data = self.__query('queries') + if not data: + return None + print(data['queries'][0]) + return data['queries'] + + def get_upstrams(self) -> list | None: + data = self.__query('stats/upstreams') + print(data) def main(): pi = PiholeAPI() - pi.get_sessions() + pi.clear_sessions() + pi.get_upstrams() if __name__ == '__main__':