]> git.somenet.org - pub/jan/neo-infcoin-utils.git/blob - mattermost_awards/main.py
Mattermost awards
[pub/jan/neo-infcoin-utils.git] / mattermost_awards / main.py
1 #!/usr/bin/env python3
2 # Someone's FSInf-coin utils.
3 #   Copyright (c) 2021 by Someone <someone@somenet.org> (aka. Jan Vales <jan@jvales.net>)
4 #   published under MIT-License
5 #
6 # Run as cronjob:
7 #   40 12 * * * (cd ~/neo-infcoin-utils/mattermost_awards; ./main.py) &> /tmp/infcoin_awards.log
8 #
9 # awards infcoins to mm-posts.
10 #
11
12 import json
13 import os
14 import psycopg2
15 import psycopg2.extras
16 import requests
17 from inspect import cleandoc
18
19 import config
20
21
22
23 def award_infcoins(user_id, amount, reason_part):
24     infcoin_wallet = json.loads(_load_wallet(config.infcoin_wallet_uid_mappingsdir+"wallet-"+user_id+".json"))
25     if infcoin_wallet["wallet"] != "":
26         r = requests.get(config.infcoin_req_url, {"auth_name":config.infcoin_req_user, "auth_secret":config.infcoin_req_pw, "addr":infcoin_wallet["wallet"], "amount":amount, "reason_uniq":"mm-"+reason_part+", uid="+user_id})
27         return True
28     return False
29
30
31
32 def _load_wallet(path, default='{"wallet":""}'):
33     ret = default
34     if os.path.isfile(path):
35         with open(path, "r") as f:
36             ret = f.read()
37     return ret
38
39
40
41 def main():
42     dbconn = psycopg2.connect(config.dbconnstring)
43     dbconn.set_session(autocommit=False)
44
45     # TODO: grant coins
46
47     # process awarded coins - TODO.
48     cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
49     cur.execute(""" SELECT id AS postid, userid FROM posts WHERE deleteat = 0 AND props::jsonb->>'infcoin' = '1' """)
50
51     for post in cur.fetchall():
52         print("awarding: "+post["userid"]+"for post: "+post["postid"])
53         award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["postid"])
54
55
56
57     # TODO: remove old tags?
58
59
60 main()