From 081c42aeed67c00d80083e06521f98c2212b661c Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Thu, 27 Jun 2024 22:37:45 +0200 Subject: [PATCH 1/2] Hotspot voucher api interface Signed-off-by: martin f. krafft --- README.md | 11 +++++++++++ omada/omada.py | 22 ++++++++++++++++++++++ vouchers.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100755 vouchers.py diff --git a/README.md b/README.md index e790772..845adb6 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,17 @@ USERNAME IP ADDRESS STATUS Make sure you have your [Settings](#Settings) file configured correctly for these to work. +### [vouchers.py](vouchers.py) + +A simple script to fetch the next ten unused vouchers, optionally limited to certain voucher groups: + +``` +$ python vouchers.py kids +kids: 112768, 426812, 004408, 551961, 090222, 023504, 657011, 125990, 591588, 650649 +``` + +If no arguments are provided, then all voucher groups are iterated. Else, only voucher groups that have any of the provided argument strings in their names are listed. + ## Settings You can store your controller settings in a configuration file to avoid hard-coding them in your scripts. diff --git a/omada/omada.py b/omada/omada.py index fad4ca0..a292615 100644 --- a/omada/omada.py +++ b/omada/omada.py @@ -590,3 +590,25 @@ def getWirelessGroups(self, site=None): ## def getWirelessNetworks(self, group, site=None): return self.__get( f'/sites/{self.__findKey(site)}/setting/wlans/{group}/ssids' ) + + ## + ## Returns the list of voucher groups for the given site. + ## + def getVoucherGroups(self, site=None): + return self.__geterator( f'/hotspot/sites/{self.__findKey(site)}/voucherGroups' ) + + ## + ## Returns the details for the given voucher group and site. + ## + def getVoucherGroupDetails(self, id, site=None): + return self.__get( f'/hotspot/sites/{self.__findKey(site)}/voucherGroups/{id}' ) + + ## + ## Returns unused vouchers for the given voucher group and site, up to an + ## optional maximum number + ## + def getUnusedVouchers(self, id, maxnr=None, site=None): + res = self.__get( f'/hotspot/sites/{self.__findKey(site)}/voucherGroups/{id}/printUnused' ) + data = res.get('data', []) + return data if maxnr is None else data[:maxnr] + diff --git a/vouchers.py b/vouchers.py new file mode 100755 index 0000000..dade11e --- /dev/null +++ b/vouchers.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 + +import sys +from omada import Omada + +def main(): + + omada = Omada(verbose=True) + omada.login() + + try: + for vg in omada.getVoucherGroups(): + if len(sys.argv) > 1 and not any(arg in vg['name'] for arg in sys.argv[1:]): + # if arguments are provided, limit the voucher groups searched + # to those whose names match one of the command line + # arguments. I.e. if there are arguments and none ("not any") + # match, then skip this group: + continue + unused = omada.getUnusedVouchers(vg['id'], maxnr=10) + codes = [v['code'] for v in unused] + print(f"{vg['name']}: {', '.join(codes)}") + + finally: + omada.logout() + +if __name__ == '__main__': + import warnings + with warnings.catch_warnings(action="ignore"): + main() From d21f5d049df8da233c4fa0437e2553e5cc3ab45a Mon Sep 17 00:00:00 2001 From: "martin f. krafft" Date: Thu, 19 Sep 2024 16:17:38 +0200 Subject: [PATCH 2/2] Skip empty voucher groups Signed-off-by: martin f. krafft --- vouchers.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/vouchers.py b/vouchers.py index dade11e..ced4f8c 100755 --- a/vouchers.py +++ b/vouchers.py @@ -17,6 +17,9 @@ def main(): # match, then skip this group: continue unused = omada.getUnusedVouchers(vg['id'], maxnr=10) + if not unused: + # no vouchers left in this group, skip it + continue codes = [v['code'] for v in unused] print(f"{vg['name']}: {', '.join(codes)}")