about summary refs log tree commit diff
path: root/src/petthecord/main.py
blob: a1bc1a6f107c99e697ebc06bc13ca38b039b8a28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from argparse import ArgumentParser
from os import getenv
from sys import argv, stderr

from .bot import Bot


def main() -> None:
    parser = ArgumentParser(
        prog="petthecord",
        description="Web API for petting any user you want"
    )
    parser.add_argument(
        "-p", "--port",
        default=8000,
        type=int,
        metavar="PORT",
        help="Bind port"
    )
    parser.add_argument(
        "-i", "--host",
        default="127.0.0.1",
        metavar="HOST",
        help="Bind IP"
    )
    parser.add_argument(
        "-o", "--origin",
        default="https://ptc.pwn3t.ru",
        metavar="PATH",
        help="Root of the bot"
    )
    parser.add_argument(
        "-d", "--cache-dir",
        default="/var/cache/petthecord",
        metavar="PATH",
        help="Directory for cache storing"
    )
    parser.add_argument(
        "-n", "--no-cache",
        action="store_true",
        default=False,
        help="Turn off the cache"
    )
    parser.add_argument(
        "-l", "--cache-lifetime",
        default=86400,
        type=int,
        metavar="TIME",
        help="Lifetime of cached avatar in seconds"
    )
    parser.add_argument(
        "-s", "--cache-gc-delay",
        default=14400,
        type=int,
        metavar="TIME",
        help="Delay between cache's garbage collector runs in seconds"
    )
    args = parser.parse_args()

    bot = Bot(
        args.host,
        args.port,
        args.origin,
        not args.no_cache,
        args.cache_dir,
        args.cache_lifetime,
        args.cache_gc_delay
    )
    if (token := getenv("PETTHECORD_TOKEN")) is not None:
        bot.run(token)
    elif (token_path := getenv("PETTHECORD_TOKEN_FILE")) is not None:
        with open(token_path) as f:
            token = f.read()
        bot.run(token)
    else:
        print(f"{argv[0]}: Neither PETTHECORD_TOKEN nor PETTHECORD_TOKEN_FILE are set", file=stderr)