Skip to content

notion_recurring_tasks.main

The main module of notion-recurring-tasks.

check_notion_key

check_notion_key(key: str | None) -> bool

Check the validity of the given Notion key.

Parameters:

Name Type Description Default
key str | None

Notion key

required

Raises:

Type Description
EnvironmentError

If the given key is not valid.

Returns:

Type Description
bool

True if key is valid. False otherwise.

Source code in src/notion_recurring_tasks/main.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
def check_notion_key(key: str | None) -> bool:
    """Check the validity of the given Notion key.

    Args:
        key (str | None): Notion key

    Raises:
        EnvironmentError: If the given key is not valid.

    Returns:
        bool: True if key is valid. False otherwise.
    """
    try:
        if not key:
            raise EnvironmentError("Missing NOTION_KEY environment variable.")
        elif not key.startswith("secret_"):
            raise EnvironmentError("NOTION_KEY environment variable not correct.")
    except EnvironmentError as e:
        print(f"WARNING: {e}")
        return False

    return True

get_notion_key

get_notion_key() -> None

Gets the Notion key from the user. If valid, sets the environment variable.

Source code in src/notion_recurring_tasks/main.py
45
46
47
48
49
50
51
52
def get_notion_key() -> None:
    """Gets the Notion key from the user. If valid, sets the environment variable.
    """
    while True:
        key = input("Input your Notion key: ")
        if check_notion_key(key):
            set_notion_key(key)
            break

main

main() -> None

The main function where the program starts.

Source code in src/notion_recurring_tasks/main.py
74
75
76
77
78
79
80
def main() -> None:
    """The main function where the program starts.
    """
    if not check_notion_key(os.getenv("NOTION_KEY")):
        get_notion_key()

    script()

script

script() -> None

The notion-recurring-tasks script.

Source code in src/notion_recurring_tasks/main.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def script() -> None:
    """The notion-recurring-tasks script.
    """
    db_id: str = notion_api.get_db_id()

    tasks: list = notion_api.find_recurrable_tasks(db_id)

    num_updated_tasks: int = 0
    for task in tasks:
        new_due_date = dataprocessing.calc_new_due_date(task)

        try:
            notion_api.update_task_properties(task["id"], new_due_date)
        except HTTPError:
            print(f"Failed to update {task["properties"]["Name"]["title"][0]["plain_text"]}.")
        else:
            num_updated_tasks += 1

    print(f"Updated {num_updated_tasks} tasks.")

set_notion_key

set_notion_key(key: str) -> None

Sets the NOTION_KEY environment variable.

Parameters:

Name Type Description Default
key str

Notion key

required
Source code in src/notion_recurring_tasks/main.py
35
36
37
38
39
40
41
42
43
def set_notion_key(key: str) -> None:
    """Sets the NOTION_KEY environment variable.

    Args:
        key (str): Notion key
    """
    with open('.env', 'w') as f:
        f.write("# Notion Recurring Tasks environment variables\n")
        f.write(f"NOTION_KEY = '{key}'")