summaryrefslogtreecommitdiff
path: root/utils.py
blob: 5786e8b7302f0e599a481f3c2e90cae8fedb193b (plain) (blame)
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
import json

default_state = {
        'source': 'off',
        'source_time': 0,
        'title': None,
        'welcome': None,
        }

def parse_streamers_file(filename):
    streamers = {}

    with open(filename) as f:
        for line in f:
            psk, username = line.strip().split(":")
            streamers[psk] = username

    return streamers

def load_state(filename):
    state = dict(default_state)

    try:
        with open(filename) as f:
            state.update(json.load(f))
    except FileNotFoundError:
        pass
    except json.JSONDecodeError:
        pass

    return state

def save_state(state, filename):
    with open(filename, "w") as f:
        json.dump(state, f)

def is_banned(ip, filename):
    try:
        with open(filename) as f:
            for line in f:
                if line.strip() == ip:
                    return True
    except FileNotFoundError:
        pass

    return False

def add_ban(ip, filename):
    with open(filename, 'a') as f:
        f.write(ip + "\n")