def __init__(self, country, schedule_time): try: super().__init__(country) self.schedule_time = schedule_time except (AttributeError, TypeError) as e: logger.error(e) logger.error(log_exception())
def send_message(self): """ schedules the message :return: None """ try: delay = self.country_delay.get_delay() s.enter(delay, 1, self.message_sender) except (AttributeError, ValueError) as e: logger.error(e) logger.error(log_exception())
def get_country_code(self): """ Returns the country code :param self: Instance of Country :return: Country code :rtype: str """ try: return pycountry.countries.get(name=self.country).alpha_2 except (KeyError, AttributeError) as e: logger.error(e) logger.error(log_exception())
def __init__(self, to, body, country, schedule_time): """ Initialize MessageScheduler :param to: Receiver phone number :param body: Body text :param country: Country name :param schedule_time: Schedule Time """ try: super().__init__(to, body) self.country_delay = CountryDelay(country, schedule_time) except (AttributeError, TypeError) as e: logger.error(e) logger.error(log_exception())
def get_current_time(self): """ Returns the current time in specific country :param self: Instance of Country :return: current_time :rtype: datetime.datetime """ try: tz = self.get_time_zone() now = datetime.now(timezone(tz)) except (AttributeError, UnknownTimeZoneError) as e: logger.error(e) logger.error(log_exception()) return now
def get_delay(self): """ Returns the time in delivery of message, based on country and schedule time :param self: Instance of CountryDelay :return: time remaining in sending message (seconds) :rtype: int """ try: time_now = self.get_current_time().timestamp() schedule_time = datetime.strptime(self.schedule_time, "%Y-%m-%d %I:%M %p").timestamp() return int(schedule_time - time_now) except ValueError as e: logger.error(e) logger.error(log_exception())
def message_sender(self): """ Send the message :return: response from sender API """ url = os.getenv('url') headers = { 'apiKey': os.getenv('apiKey') } payload = f"mobile_number={self.to}&sms_text={self.body}&label=labelmarket&sender_id=80019950" try: response = requests.request("GET", url, headers=headers, params=payload) logger.info(response.content) except Exception as e: logger.error('Exception while sending message ', e) logger.error(log_exception())
def get_time_zone(self): """ Returns the timezone of the country :param self: Instance of Country :return: timezone :rtype: str """ try: timezone_country = {} country_code = self.get_country_code() for countrycode in country_timezones: timezones = country_timezones[countrycode] for tz in timezones: timezone_country[countrycode] = tz return timezone_country[country_code] except (KeyError, ValueError, AttributeError, TypeError) as e: logger.error(e) logger.error(log_exception())