def source_creator(source):
    from lib.core.state import State

    s = source
    sources = State.get_sources()
    modules = State.get_source_modules()

    while True:
        name = s.name
        s.name = creator.prompt_string("Name", default=name)

        s.module = create_source_choose_module(s.module)

        module = modules[s.module]
        props = module.get_properties()
        print("Module Properties")
        for p in props:
            default = None
            if s.module_properties is not None:
                default = s.module_properties.get(p, None)

            if s.module_properties is None:
                s.module_properties = {}

            s.module_properties[p] = creator.prompt_string(f"{p}",
                                                           default=default)

        print()
        print(f"Id: {s.id}")
        print(f"Name: {s.name}")
        print(f"Module: {s.module}")
        print("-----------------------------")
        for p in s.module_properties:
            print(f"{p}: {s.module_properties[p]}")
        print("-----------------------------")

        while True:
            confirm = creator.prompt_options("Choose an option",
                                             ["save", "edit", "test", "quit"])
            if confirm == "test":
                test_source(source, modules)
                continue

            else:
                break

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

    sources[source.id] = source

    save()
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 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)
Beispiel #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
Beispiel #5
0
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]
Beispiel #6
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
Beispiel #7
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()