From 1b5ff8d6fb97f370f68c9adab026e19d19a98e66 Mon Sep 17 00:00:00 2001 From: Someone Date: Fri, 5 Mar 2021 17:27:51 +0100 Subject: [PATCH] Mattermost awards --- mattermost_awards/.gitignore | 1 + mattermost_awards/config.py.example | 17 ++++++++ mattermost_awards/main.py | 60 +++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 mattermost_awards/.gitignore create mode 100644 mattermost_awards/config.py.example create mode 100755 mattermost_awards/main.py diff --git a/mattermost_awards/.gitignore b/mattermost_awards/.gitignore new file mode 100644 index 0000000..4acd06b --- /dev/null +++ b/mattermost_awards/.gitignore @@ -0,0 +1 @@ +config.py diff --git a/mattermost_awards/config.py.example b/mattermost_awards/config.py.example new file mode 100644 index 0000000..b625e45 --- /dev/null +++ b/mattermost_awards/config.py.example @@ -0,0 +1,17 @@ +# +# Someone's FSInf-coin utils. +# Copyright (c) 2021 by Someone (aka. Jan Vales ) +# published under MIT-License +# +# Example config file. +# mv to config.py.examle config.py + edit. +# + +dbconnstring = "host=... dbname=... user=... password=..." + +infcoin_wallet_uid_mappingsdir = "..." + +infcoin_req_url = "..." +infcoin_req_user = "..." +infcoin_req_pw = "..." +infcoin_req_amount = 100 diff --git a/mattermost_awards/main.py b/mattermost_awards/main.py new file mode 100755 index 0000000..f95cfae --- /dev/null +++ b/mattermost_awards/main.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python3 +# Someone's FSInf-coin utils. +# Copyright (c) 2021 by Someone (aka. Jan Vales ) +# published under MIT-License +# +# Run as cronjob: +# 40 12 * * * (cd ~/neo-infcoin-utils/mattermost_awards; ./main.py) &> /tmp/infcoin_awards.log +# +# awards infcoins to mm-posts. +# + +import json +import os +import psycopg2 +import psycopg2.extras +import requests +from inspect import cleandoc + +import config + + + +def award_infcoins(user_id, amount, reason_part): + infcoin_wallet = json.loads(_load_wallet(config.infcoin_wallet_uid_mappingsdir+"wallet-"+user_id+".json")) + if infcoin_wallet["wallet"] != "": + 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}) + return True + return False + + + +def _load_wallet(path, default='{"wallet":""}'): + ret = default + if os.path.isfile(path): + with open(path, "r") as f: + ret = f.read() + return ret + + + +def main(): + dbconn = psycopg2.connect(config.dbconnstring) + dbconn.set_session(autocommit=False) + + # TODO: grant coins + + # process awarded coins - TODO. + cur = dbconn.cursor(cursor_factory=psycopg2.extras.DictCursor) + cur.execute(""" SELECT id AS postid, userid FROM posts WHERE deleteat = 0 AND props::jsonb->>'infcoin' = '1' """) + + for post in cur.fetchall(): + print("awarding: "+post["userid"]+"for post: "+post["postid"]) + award_infcoins(post["userid"], config.infcoin_req_amount, "post, pid="+post["postid"]) + + + + # TODO: remove old tags? + + +main() -- 2.43.0