def __init__(self, id=None, name="New Task", enabled=True, frequency=15, frequency_unit="minutes", source_ids=None, notif_agent_ids=None, include=None, exclude=None, colour_flag=""): if id is None: from lib.core.state import State id = creator.create_simple_id(State.get_tasks()) self.id = id self.name = name self.enabled = enabled self.frequency = frequency self.frequency_unit = frequency_unit self.source_ids = source_ids self.notif_agent_ids = notif_agent_ids self.include = include self.exclude = exclude self.colour_flag = colour_flag
def run(cron_time, cron_unit, notify=True, force_tasks=False, force_agents=False, recent_ads=3): tasks = State.get_tasks() log.add_handler("CRON_HANDLER") log.info_print(f"Running cronjob for schedule: {cron_time} {cron_unit}") # Scrape each url given in tasks file for id in tasks: t = tasks[id] freq = t.frequency freq_unit = t.frequency_unit # skip tasks that dont correspond with the cron schedule if int(freq) != int(cron_time) or freq_unit[:1] != cron_unit[:1]: continue task.run(t, notify=notify, force_tasks=force_tasks, force_agents=force_agents, recent_ads=recent_ads)
def do_delete_task(id): from lib.core.state import State import lib.core.hooks as hooks tasks = State.get_tasks() hooks.delete_task_model(tasks[id]) del tasks[id]
def get_tasks_using_notif_agent(notif_agent): result = [] tasks = State.get_tasks() for id in tasks: if notif_agent.id in tasks[id].notif_agent_ids: result.append(tasks[id]) return result
def delete_source(): from lib.core.state import State sources_dict = State.get_sources() tasks_dict = State.get_tasks() creator.print_title("Delete Source") changes_made = False while True: sources_list = [] for s in sources_dict: sources_list.append(sources_dict[s]) for i in range(len(sources_list)): print(f"{i} - {sources_list[i].name}") print("s - save and quit") print("q - quit without saving") tnum_str = creator.prompt_string("Delete source") if tnum_str == "s": save(sources_dict, sources_file) core.task.save(tasks_dict, tasks_file) return elif tnum_str == "q": if changes_made and creator.yes_no("Quit without saving", "n") == "y": return elif not changes_made: return if re.match("[0-9]+$", tnum_str): tnum = int(tnum_str) if tnum >= 0 and tnum < len(sources_list): if creator.yes_no(f"Delete {sources_list[tnum].name}", "y") == "y": used_by = get_tasks_using_source( sources_dict[sources_list[tnum].id], tasks_dict) if len(used_by) > 0: task_names = [] for u in used_by: task_names.append(f"'{u.name}'") print( f"Cannot delete source. It is being used by task(s): {','.join(task_names)}" ) print( "Delete those tasks or remove this notification agent from them first before deleting." ) else: do_delete_source(sources_list[tnum].id) changes_made = True
def prime_all(tasks=None, notify=True, recent_ads=3): if tasks is None: from lib.core.state import State tasks = State.get_tasks() results = [] for id in tasks: results.append(prime(tasks[id], notify=notify, recent_ads=recent_ads)) return results
def list_tasks(pause_after=0): from lib.core.state import State tasks = State.get_tasks() i = 0 for t in tasks: print(tasks[t]) i = i + 1 if pause_after > 0 and i == pause_after: i = 0 if input(":") == "q": break
def edit_task(): from lib.core.state import State creator.print_title("Edit Task") task = creator.prompt_complex_dict("Choose a task", State.get_tasks(), "name", extra_options=["d"], extra_options_desc=["done"]) if task == "d": return else: task_creator(task)
def get_tasks_using_source(source, tasks=None): if tasks is None: from lib.core.state import State tasks = State.get_tasks() used_by = [] for id in tasks: if source.id in tasks[id].source_ids: used_by.append(tasks[id]) return used_by
def do_delete_notif_agent_yaml(id): from lib.core.state import State tasks_dict = State.get_tasks() notif_agents_dict = State.get_notif_agents() for task_id in tasks_dict: t = tasks_dict[task_id] if id in t.notif_agent_ids: t.notif_agent_ids.remove(id) del notif_agents_dict[id]
def __init__(self, id=None, name="New Notification Agent", module=None, module_properties=None): if id is None: from lib.core.state import State id = creator.create_simple_id(State.get_tasks()), self.id = id self.name = name self.module = module self.module_properties = module_properties
def do_delete_source(id): import lib.core.hooks as hooks from lib.core.state import State tasks = State.get_tasks() sources = State.get_sources() for task_id in tasks: task = tasks[task_id] if id in task.source_ids: task.source_ids.remove(id) hooks.delete_source_model(sources[id]) del sources[id]
def do_delete_notif_agent(id): from lib.core.state import State import lib.core.hooks as hooks tasks_dict = State.get_tasks() notif_agents_dict = State.get_notif_agents() for task_id in tasks_dict: t = tasks_dict[task_id] if id in t.notif_agent_ids: t.notif_agent_ids.remove(id) hooks.delete_notif_agent_model(notif_agents_dict[id]) del notif_agents_dict[id]
def refresh_cron(tasks=None): if tasks is None: from lib.core.state import State tasks = State.get_tasks() cron.clear() schdules = [] for id in tasks: task = tasks[id] sched = (task.frequency, task.frequency_unit) if not sched in schedules: schedules.append(sched) for s in schedules: cron.add(s[0], s[1])
def choose_task(msg, title, options_dict=None, confirm_msg=None): from lib.core.state import State tasks = State.get_tasks() creator.print_title(title) while True: tasks_list = list(tasks.values()) for i in range(len(tasks_list)): print(f"{i} - {tasks_list[i].name}") if options_dict is not None: for o in options_dict: print(f"{o} - {options_dict[o]}") tnum_str = creator.prompt_string(msg) if options_dict is not None: for o in options_dict: if re.match(tnum_str, o): return o if not re.match("[0-9]+$", tnum_str): continue tnum = int(tnum_str) if tnum < 0 and tnum >= len(tasks): continue task = tasks_list[tnum] print(task) if confirm_msg is not None: if creator.yes_no(format_task_string(confirm_msg, task)) == "n": continue print("****") print(tasks_list[tnum]) return tasks_list[tnum]
def test_task(): option = simple_cmd("Choose a Task to Test", "Test Task", State.get_tasks(), task.Task) if option is not None: task.test(option)
def task_creator(task): from lib.core.state import State cur_tasks = State.get_tasks() sources = State.get_sources() notif_agents = State.get_notif_agents() t = task while True: while True: print(f"Id: {t.id}") t.name = creator.prompt_string("Name", default=t.name) t.frequency = creator.prompt_num("Frequency", default=t.frequency) t.frequency_unit = creator.prompt_options("Frequency Unit", ["minutes", "hours"], default=t.frequency_unit) t.source_ids = create_task_add_sources(default=t.source_ids) if t.source_ids == None: return t.include = creator.prompt_string( "Include [list seperated by commas]", allow_empty=True, default=t.include) t.exclude = creator.prompt_string( "Exclude [list seperated by commas]", allow_empty=True, default=t.exclude) t.notif_agent_ids = create_task_add_notif_agents( default=t.notif_agent_ids) if t.notif_agent_ids == None: return print() print(f"Name: {t.name}") print(f"Frequency: {t.frequency} {t.frequency_unit}") print(f"Sources") print(f"----------------------------") for source_id in t.source_ids: print(f"{sources[source_id].name}") print("-----------------------------") print(f"Include: {t.include}") print(f"Exclude: {t.exclude}") while True: confirm = creator.prompt_options( "Choose an option", ["save", "edit", "dryrun", "quit"]) if confirm == "quit": if creator.yes_no("Quit without saving?", "n") == "y": return else: continue elif confirm == "dryrun": if creator.yes_no("Execute dry run?", "y"): log.debug_print("Executing dry run...") test(task) continue else: break if confirm == "save": break elif confirm == "edit": continue cur_tasks[task.id] = task save() if creator.yes_no("Prime this task?", "y") == "y": recent = int( creator.prompt_num( "How many of the latest ads do you want notified?", default="3")) if recent == 0: notify = False else: notify = True prime(task, notify=notify, recent_ads=recent) if not cron.exists(task.frequency, task.frequency_unit): if creator.yes_no( f"Add cronjob for '{task.frequency} {task.frequency_unit}'", "y") == "y": refresh_cron() else: print( f"Cronjob already exists for '{task.frequency} {task.frequency_unit}'... skipping" ) print("Done!") return
def prime_task(): option = simple_cmd("Choose a Task to Prime", "Prime Task", State.get_tasks(), task.Task) if option is not None: task.prime(option)