-
Notifications
You must be signed in to change notification settings - Fork 0
/
patron_payout.py
76 lines (67 loc) · 3.28 KB
/
patron_payout.py
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 configuration import read_config
#
# players = read_config("players")
#
# for email, player in players.items():
# steamid = player['steamid']
# patron = player.get("patron", None)
# if patron:
# print (steamid, patron)
from ark_manager import *
from gamechat_commands import parse_chat
from dbo import *
import functools
from datetime import datetime
import patreon
from debug import get_logger
log = get_logger("patreon")
def patreon_payout():
# Only perform on day 1 of a month
now = datetime.now()
payout_data = read_config("patreon")
log.info("Checking payout information...")
payout_emails = []
for email, info in payout_data.items():
if now.month > info['last_payout_month'] and now > info['last_payout_date']:
log.info(f"User {email} added for payout check.")
payout_emails.append(email)
log.info(f"Payout emails {payout_emails}")
if len(payout_emails) > 0:
log.info(f"Gathering patreon api info...")
api_client = patreon.API(read_config("config")["patreon_token"])
pledges = {}
users = {}
all_data = api_client.fetch_campaign_and_patrons()._all_resource_json_data()
for data in all_data:
if 'type' in data:
if data['type'] == 'pledge':
pledges[data['relationships']['patron']['data']['id']] = data['attributes']
elif data['type'] == 'user':
users[data['id']] = data['attributes']
user_info = read_config("players")
for user_id, pledge in pledges.items():
active = pledge['declined_since'] == None
if users[user_id]['email'] not in payout_emails:
log.warning(f"User {users[user_id]['email']} already received payout this month. Skip.")
continue
if user_info[users[user_id]['email']] == None:
log.warning(f"Tried to payout user with email {users[user_id]['email']} but it does not have SteamID set.")
broadcast("There was a problem during payout to one of the patrons, SteamID not set, please contact administrator.", True)
continue
if not active:
log.warning(f"User {users[user_id]['email']} canceled subscription. Not paying.")
continue
log.info(f"{users[user_id]['email']} is paying {pledge['amount_cents'] / 100}$, active is {active}")
if users[user_id]['email'].strip() in payout_emails:
log.info(f"Paying out user {users[user_id]['email'].strip()}")
rcon_command(f"ScriptCommand TCsAR AddArcTotal {user_info[users[user_id]['email']]} {pledge['amount_cents'] / 100}")
log.debug(f"Setting user payout date for {users[user_id]['email']}...")
payout_data[users[user_id]['email']]['last_payout_month'] = datetime.now().month
payout_data[users[user_id]['email']]['last_payout_date'] = datetime.now()
else:
log.info (f"NOT paying out user {users[user_id]['email'].strip()} because it was not found in payout_emails - probably already payed.")
write_config('patreon', payout_data)
else:
log.info("Looks like everyone received their payment, exit.")
if __name__ == "__main__":
patreon_payout()