Exemplo n.º 1
0
def run(task,
        sources=None,
        notif_agents=None,
        notify=True,
        force_tasks=False,
        force_agents=False,
        recent_ads=0,
        save_ads=True,
        ignore_old_ads=False):

    from lib.core.state import State

    if sources is None:
        sources = State.get_sources()

    if notif_agents is None:
        notif_agents = State.get_notif_agents()

    exclude_words = task.exclude

    log.info_print(f"Task: {task.name}")

    if task.enabled == False:
        if force_tasks == False:
            log.info_print("Task disabled. Skipping...")
            print()
            return
        else:
            log.info_print("Task disabled but forcing task to run...")

    task_notif_agents = notif_agent.get_notif_agents_by_ids(
        task.notif_agent_ids)

    if notify == True and force_agents == False:
        notif_agent.notif_agents_enabled_check(task_notif_agents)

    source_results = {}

    for source_id in task.source_ids:
        source_results[source_id] = source.scrape(
            sources[source_id],
            task_notif_agents,
            include=task.include,
            exclude=task.exclude,
            colour_flag=task.colour_flag,
            notify=notify,
            force_tasks=force_tasks,
            force_agents=force_agents,
            recent_ads=recent_ads,
            save_ads=save_ads,
            ignore_old_ads=ignore_old_ads)

    if save_ads:
        ad.save()

    result = RunResult(source_results=source_results)
    return result
Exemplo n.º 2
0
def get_notif_agents_by_ids(ids):
    from lib.core.state import State

    notif_agents = State.get_notif_agents()

    result = []
    for id in ids:
        result.append(notif_agents[id])

    return result
Exemplo n.º 3
0
def edit_notif_agent():
    creator.print_title("Edit Notification Agent")
    notif_agent = creator.prompt_complex_dict("Choose a notification agent",
                                              State.get_notif_agents(),
                                              "name",
                                              extra_options=["d"],
                                              extra_options_desc=["done"])
    if notif_agent == "d":
        return
    else:
        notif_agent_creator(notif_agent)
Exemplo n.º 4
0
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
Exemplo n.º 5
0
    def __repr__(self):
        from lib.core.state import State
        source_names = []
        sources = State.get_sources()
        for id in self.source_ids:
            source_names.append(f"'{sources[id].name}'")

        notif_agent_names = []
        notif_agents = State.get_notif_agents()
        for id in self.notif_agent_ids:
            notif_agent_names.append(f"'{notif_agents[id].name}'")

        return f"""
Exemplo n.º 6
0
def list_notif_agents(pause_after=0):
    from lib.core.state import State

    notif_agents = State.get_notif_agents()

    i = 0
    for id in notif_agents:
        print(notif_agents[id])
        i = i + 1
        if pause_after > 0 and i == pause_after:
            i = 0
            if input(":") == "q":
                break
Exemplo n.º 7
0
def test_notif_agent(notif_agent):
    from lib.core.state import State

    notif_agents = State.get_notif_agents()
    modules = State.get_notif_agent_modules()

    log.debug_print(f"Testing notification agent: '{notif_agent.name}'")
    module = modules[notif_agent.module]
    module.send(title=f"Testing '{notif_agent.name}'",
                message="This is a test message",
                **notif_agent.module_properties)

    print("Done!")
Exemplo n.º 8
0
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]
Exemplo n.º 9
0
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]
Exemplo n.º 10
0
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
Exemplo n.º 11
0
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
Exemplo n.º 12
0
def notif_agent_creator(notif_agent):
    from lib.core.state import State

    n = notif_agent

    cur_notif_agents = State.get_notif_agents()
    modules = State.get_notif_agent_modules()

    while True:
        n.name = creator.prompt_string("Name", default=n.name)
        n.module = create_notif_agent_choose_module(n.module)

        module = modules[n.module]
        props = module.get_properties()

        print("Module Properties")

        for p in props:
            default = None
            if n.module_properties is not None:
                default = n.module_properties.get(p, None)
            else:
                n.module_properties = {}

            while True:
                n.module_properties[p] = creator.prompt_string(f"{p}",
                                                               default=default)
                is_valid, error_msg = module.is_property_valid(
                    p, n.module_properties[p])

                if is_valid:
                    break
                else:
                    print(error_msg)
                    default = None
        print()
        print(f"Id: {n.id}")
        print(f"Name: {n.name}")
        print(f"Module: {n.module}")
        print("-----------------------------")
        for p in props:
            print(f"{p}: {n.module_properties[p]}")
        print("-----------------------------")

        while True:
            confirm = creator.prompt_options("Choose an option",
                                             ["save", "edit", "test", "quit"])
            if confirm == "test":
                test_notif_agent(notif_agent)
                continue
            else:
                break

        if confirm == "save":
            break
        elif confirm == "edit":
            continue
        elif confirm == "quit":
            return

    cur_notif_agents[n.id] = notif_agent

    State.save_notif_agents()
Exemplo n.º 13
0
def test_notif_agent():
    option = simple_cmd("Choose a Notification Agent to Test",
                        "Test Notification Agent", State.get_notif_agents(),
                        core.NotifAgent)
    if option is not None:
        notif_agent.test_notif_agent(option)