Exemple #1
0
class Miamala:

    ctx = user.quick_login("admin", "admin")
    meseji = sms.get_sms(ctx, 1, 1, qty=10, unread_preferred=True)
    data = Connection()

    def save_miamala(self):
        """miamala ilio ingia kwenye account"""
        for ujumbe in self.meseji['response']['Messages']['Message']:
            if "HaloPesa" in ujumbe["Phone"]:
                muamala = ujumbe['Content'].split(' ')
                if "Transaction" and "received" in muamala:
                    try:
                        self.Transaction_id = muamala[2]
                        amount = muamala[muamala.index("received") + 1]
                        phone = muamala[muamala.index('number') + 1]
                        sender = muamala[muamala.index('from') +
                                         1] + " " + muamala[
                                             muamala.index('from') + 2]
                        time = muamala[muamala.index('time') + 2]
                        date = muamala[muamala.index('time') + 1]
                        query = f"""INSERT INTO watu.miamla (transaction_id,amout,phone,sender,time,date)
                               values ({self.Transaction_id},{amount},{phone},{sender},{time},{date}) """
                    except Exception as e:
                        logging.INFO("Error in muamala")
                        pass
                    finally:
                        self.data.run_query(query)

    def watu_details(self):
        """get information of people"""
        query = "SELECT * FROM watu.people"
        watu = self.data.run_query(query)
        return jsonify(watu)
Exemple #2
0
    def run(self):

        try:

            # If we have detected an error, wait a bit before running again
            if self._error_countdown > 0:
                self._error_countdown -= 1
                return

            if self._ctx is None:
                # Log in required - This is all hard-coded
                logger.debug(
                    "Attempting to login with {} / {} / {}".format(
                        settings.USERNAME, settings.PASSWORD, settings.MODEM_HOST
                    )
                )
                self._ctx = quick_login(
                    settings.USERNAME, settings.PASSWORD, settings.MODEM_HOST
                )

            # Begin the scrape...
            for end_point in END_POINTS:
                result = get_from_url_raw(
                    url="http://{host}{end_point}".format(
                        host=settings.MODEM_HOST, end_point=end_point
                    ),
                    ctx=self._ctx,
                )
                if result.status_code == 200:
                    text = result.text
                    xmldoc = parse_xml_string(text)
                    err = check_error(xmldoc.documentElement)

                    if err:
                        self._ctx = None
                        self._error_countdown = settings.ERROR_COUNTDOWN
                        logger.error("request error: {}".format(err))
                        break

                    # Everything is OK - store the result
                    self._modem_data.set_data(key=end_point, value=text)

        except Exception:
            # Don't blow up or it will break APScheduler
            logger.error("Error in scraper:\n{}".format(traceback.format_exc()))
            self._ctx = None
            self._error_countdown = settings.ERROR_COUNTDOWN
        finally:
            self._modem_data.log_contents()
Exemple #3
0
def get_session():
    return user.quick_login(USER, PASSWORD)