class Bells(App): APP_ID = APP.BELLS RULE_NAME = 'Divine Bells' def __init__(self): super().__init__() self.rule = Rule(Bells.RULE_NAME, alarmable=True) self.rule.description = 'Scanning for value(s) to see if they are breached every hour' self.rule.app_id = self.APP_ID self.overseer.add_rule(self.rule) def execute(self, command, **kwargs): if command == 'get-alerts': rule = self.overseer.get_rule(Bells.RULE_NAME) return {'rule': rule} elif command == 'create-alert': symbol = kwargs['symbol'] below = kwargs['below'] above = kwargs['above'] days_to_cancel = kwargs['daysToCancel'] alert = QuoteAlert(symbol, below, above, days_to_cancel) alert.set_rule(self.rule) if create(self.rule, alert): return {Toast.IDENTIFIER: Toast('Created alert', duration=2, variant=Variant.SUCCESS)} else: return {Toast.IDENTIFIER: Toast('Failed to create alert', variant=Variant.ERROR)} elif command == 'remove-alert': subrule_name = kwargs['subruleName'] self.rule.remove_subrule(subrule_name)
def __init__(self): cache = Cache(60) super().__init__(app_type=APPTYPE.STREAMING, cache=cache) auth = tweepy.AppAuthHandler(self.configuration['TWITTER_APP_KEY'], self.configuration['TWITTER_APP_SECRET']) self.api = tweepy.API(auth) self.users = self.configuration['TWITTER_USERS_TO_FETCH_SCHEDULE'] rule = Rule('Schedule Finder', alarmable=False) rule.description = 'Try to find a set of schedules' rule.app_id = self.APP_ID interval = Interval(hour=1, minute=0) for user in self.users: rule.create_subrule(subrule_name=user + ':sch', func=self.get_schedule, args=user, triggers={ 'day_of_week': 'mon,tue,wed,thu,fri,sat,sun', 'hour': str(interval.hour), 'minute': str(interval.minute) }) interval.increment_15_mins() self.overseer.add_rule(rule) self.start()
def create(rule: Rule, alert: QuoteAlert): return rule.create_subrule(subrule_name=alert.symbol, func=scan, args=alert, metadata={ 'symbol': alert.symbol, 'below': alert.below, 'above': alert.above }, triggers={ 'end_date': alert.end_time, 'day_of_week': 'mon,tue,wed,thu,fri', 'hour': '10-15', })
def add_rule(self, rule: Rule): rule.id = identify(rule.name) # Rule id's should be unique if rule.id in self.rules_map: panic( 'overseer', 'Rule id clash. Rule id: [{0}] name: [{1}]'.format( rule.id, rule.name)) return self.rules_map[rule.id] = rule subrules = rule.scheduler.get_str_jobs() log( 'overseer', 'Running rule id: [{0}] name: [{1}] with [{2}] subrules. Subrules: {3}' .format(rule.id, rule.name, len(subrules), subrules))
def create_subreddit_feeds(self): subs = self.configuration['SUBREDDIT_FEED_SITES'] rule = Rule('Subreddit feeds', alarmable=False) rule.description = 'Store a set of Subreddit feeds' rule.app_id = self.APP_ID for sub_name in subs: rule.create_subrule(subrule_name=sub_name + '_feed', func=self.get_submissions, args=sub_name, triggers={ 'hour': '5', }) self.overseer.add_rule(rule)
def create_rss_feeds(self): urls = self.configuration['RSS_FEED_SITES'] rule = Rule('RSS feeds', alarmable=False) rule.description = 'Store a set of RSS feeds' rule.app_id = self.APP_ID for url in urls: rule.create_subrule(subrule_name=extract_domain(url) + '_feed', func=get_feed, args=url, triggers={ 'day_of_week': 'sat', }) self.overseer.add_rule(rule)
def __init__(self): super().__init__() self.rule = Rule(Bells.RULE_NAME, alarmable=True) self.rule.description = 'Scanning for value(s) to see if they are breached every hour' self.rule.app_id = self.APP_ID self.overseer.add_rule(self.rule)
from common.abstract_classes.rule import Rule rule = Rule('Sample Rule') rule.description = 'This is a sample template on creating a rule' def run(): rule.create_subrule(subrule_name='subrule1', func=dummy, args='param', triggers={ 'day_of_week': 'mon,tue,wed,thu,fri', 'hour': '1' }) rule.create_subrule(subrule_name='subrule2', func=dummy, triggers={ 'day_of_week': 'tue,thu', 'hour': '5', 'minute': '30' }) def dummy(arg=None): pass
from common.abstract_classes.rule import Rule rule = Rule('Sample Rule 2') rule.description = 'Sample description 2' def run(): rule.create_subrule(subrule_name='subrule1', func=some_func, args=('param1', 'param2'), triggers={ 'day_of_week': 'fri', 'hour': '*/1' }) def some_func(param1, param2): pass