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 delete_notif_agent(): notif_agents_dict = State.get_notif_agents() creator.print_title("Delete Notification Agent") changes_made = False while True: notif_agents_list = [] for n in notif_agents_dict: notif_agents_list.append(notif_agents_dict[n]) for i in range(len(notif_agents_list)): print(f"{i} - {notif_agents_list[i].name}") print("s - save and quit") print("q - quit without saving") tnum_str = creator.prompt_string("Delete notif_agent") if tnum_str == "s": State.save_notif_agents() State.save_tasks() break 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(notif_agents_list): if creator.yes_no(f"Delete {notif_agents_list[tnum].name}", "y") == "y": used_by = get_tasks_using_notif_agent( notif_agents_dict[notif_agents_list[tnum].id]) if used_by is not None and len(used_by) > 0: task_names = [] for u in used_by: task_names.append(f"'{u.name}'") print( f"Cannot delete notification agent '{notif_agents_list[tnum].name}'. It is being used by: {','.join(task_names)}" ) print( "Delete tasks using this notification agent or remove this notification agent from those tasks first before deleting." ) else: do_delete_notif_agent(notif_agents_list[tnum].id) changes_made = True
def test_source(source): from lib.core.state import State modules = State.get_source_modules() include = [] exclude = [] if creator.yes_no("Would you like to add inlcudes/excludes", "n") == "y": include = creator.prompt_string("Include", allow_empty=True) exclude = creator.prompt_string("Exclude", allow_empty=True) module = modules[source.module] return scrape(source, None, include=include, exclude=exclude, notify=False, save_ads=False)
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 create_task_add_notif_agents(default=None): from lib.core.state import State notif_agents_dict = State.get_notif_agents() default_str = "" if default is None and len(notif_agents_dict) == 1: default = [list(notif_agents_dict)[0]] if default is not None: first = True for s in default: if not s in notif_agents_dict: continue if first: default_str = f"[{notif_agents_dict[s].name}" first = False else: default_str = f"{default_str}, {notif_agents_dict[s].name}" default_str = f" {default_str}]" add_notif_agents = [] if len(notif_agents_dict) == 0: log.error_print(f"No notif_agents found. Please add a notif_agent ") return notif_agents_list = list(notif_agents_dict.values()) remaining_notif_agents = notif_agents_list.copy() while len(remaining_notif_agents) > 0: i = 0 for s in remaining_notif_agents: print(f"{i} - {s.name}") i = i + 1 choices = "0" if len(remaining_notif_agents) > 1: choices = f"0-{len(remaining_notif_agents) - 1}" if len(add_notif_agents) > 0: print("r - reset") print("d - done") if default is None or len(add_notif_agents) > 0: notif_agent_index_str = input(f"notif_agent [{choices}]: ") else: notif_agent_index_str = input( f"notif_agent [{choices}]:{default_str} ") if default is not None and notif_agent_index_str == "" and len( add_notif_agents) == 0: return default if len(remaining_notif_agents) == 0 and notif_agent_index_str == "": add_notif_agents.append(remaining_notif_agents[notif_agent_index]) break if len(add_notif_agents): if notif_agent_index_str == "d": break elif notif_agent_index_str == "r": print("Resetting...") remaining_notif_agents = notif_agents_list.copy() add_notif_agents = [] if re.match("[0-9]+$", notif_agent_index_str): notif_agent_index = int(notif_agent_index_str) if notif_agent_index >= 0 and notif_agent_index < len( notif_agents_list): add_notif_agents.append( remaining_notif_agents[notif_agent_index]) del (remaining_notif_agents[notif_agent_index]) if len(remaining_notif_agents) == 0: break confirm = creator.yes_no("Add another?", "y") if confirm == "n": break result = [] for s in add_notif_agents: result.append(s.id) return result
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