def __get_cached_balance_info(self, short=False):
        balance_file = self.config["workDir"] + "/LAST_BALANCE"
        if os.path.isfile(balance_file):
            ussd_fetcher = UssdFetcher(self.config["gammuConfigFile"], self.config["gammuConfigSection"])
            with open(balance_file, "r") as f:
                file_content = f.read()
                reply_unicode = ussd_fetcher.convert_reply_raw_to_unicode(file_content)
                balance_regex_raw = self.config["balanceInfoRegex"]
                if not balance_regex_raw:
                    return reply_unicode
                balance_regex = re.compile(balance_regex_raw, flags=re.UNICODE | re.MULTILINE)
                match = re.search(balance_regex, reply_unicode)

                if match:
                    if short and match.groups():
                        return match.group(1)
                    else:
                        return match.group(0)

        return None
Example #2
0
    def __get_cached_balance_info(self, short=False):
        balance_file = self.config['workDir'] + '/LAST_BALANCE'
        if os.path.isfile(balance_file):
            ussd_fetcher = UssdFetcher(self.config['gammuConfigFile'], self.config['gammuConfigSection'])
            with open(balance_file, 'r') as f:
                file_content = f.read()
                reply_unicode = ussd_fetcher.convert_reply_raw_to_unicode(file_content)
                balance_regex_raw = self.config['balanceInfoRegex']
                if not balance_regex_raw:
                    return reply_unicode
                balance_regex = re.compile(balance_regex_raw, flags=re.UNICODE|re.MULTILINE)
                match = re.search(balance_regex, reply_unicode)

                if match:
                    if short and match.groups():
                        return match.group(1)
                    else:
                        return match.group(0)
 
        return None
Example #3
0
    def __update_balance_if_necessary(self, force=False):
        balance_file = self.config['workDir'] + '/LAST_BALANCE'
        ussd = self.config['ussdCheckBalance']
        balance_fetch_interval = self.config['balanceFetchInterval']
        can_do_check = True and ussd
        do_check = force or not os.path.exists(balance_file)
        if os.path.exists(balance_file):
            last_updated_time = datetime.fromtimestamp(os.path.getctime(balance_file))
            do_check = do_check or (balance_fetch_interval > 0 and (last_updated_time < datetime.now() - timedelta(days=balance_fetch_interval)))
            if (not can_do_check) or do_check:
                os.remove(balance_file)

        if can_do_check and do_check:
            debug("Updating cached balance (force={}) using USSD code '{}' ...".format(force, ussd))
            ussd_fetcher = UssdFetcher(self.config['gammuConfigFile'], self.config['gammuConfigSection'])
            reply_raw = ussd_fetcher.fetch_ussd_reply_raw(ussd)
            with open(balance_file, 'w') as f:
                if reply_raw is not None:
                    f.write(reply_raw)
            debug("done, USSD reply was '{}'.".format(reply_raw))
    def __update_balance_if_necessary(self, force=False):
        balance_file = self.config["workDir"] + "/LAST_BALANCE"
        ussd = self.config["ussdCheckBalance"]
        balance_fetch_interval = self.config["balanceFetchInterval"]
        can_do_check = True and ussd
        do_check = force or not os.path.exists(balance_file)
        if os.path.exists(balance_file):
            last_updated_time = datetime.fromtimestamp(os.path.getctime(balance_file))
            do_check = do_check or (
                balance_fetch_interval > 0
                and (last_updated_time < datetime.now() - timedelta(days=balance_fetch_interval))
            )
            if (not can_do_check) or do_check:
                os.remove(balance_file)

        if can_do_check and do_check:
            debug("Updating cached balance (force={}) using USSD code '{}' ...".format(force, ussd))
            ussd_fetcher = UssdFetcher(self.config["gammuConfigFile"], self.config["gammuConfigSection"])
            reply_raw = ussd_fetcher.fetch_ussd_reply_raw(ussd)
            with open(balance_file, "w") as f:
                if reply_raw is not None:
                    f.write(reply_raw)
            debug("done, USSD reply was '{}'.".format(reply_raw))