]> git.somenet.org - pub/jan/neo-infcoin-utils.git/blob - vowi_awards/infcoin_process_changes.py
mattermost_awards/main.py
[pub/jan/neo-infcoin-utils.git] / vowi_awards / infcoin_process_changes.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 * * * * (cd ~/gitstuff/neo-infcoin-utils/vowi_awards; ./infcoin_process_changes.py) &> /tmp/infcoin_process_changes.py.log
8 #
9 # awards infcoins to vowi-contributions.
10 #
11
12
13 import json
14 import os
15 import pprint
16 import signal
17 import requests
18
19 import mwclient
20
21
22 import config
23
24
25 def process_recent_mw_changes(mws):
26     try:
27         with open('data/infcoin_process_changes-'+mws.host+'.json') as file_handle:
28             changes = json.load(file_handle)
29     except:
30         changes = dict()
31     if 'delme' in changes:
32         del changes['delme']
33
34     if 'revid' not in changes:
35         changes['revid'] = 138500
36
37     if 'infcoin_addr' not in changes:
38         changes['infcoin_addr'] = dict()
39
40     max_revid = mws.recentchanges().next()['revid']
41     print("revid:"+str(changes['revid'])+", max_revid:"+str(max_revid))
42
43     # Iterate over all revisions since the last run and find wallet addresses.
44     for i in range(changes['revid'], max_revid):
45         revision = mws.revisions([i])
46         if not revision:
47             continue
48         revision = revision[0]
49         changes['revid'] = revision['revid']
50
51         if len(revision['comment'].strip()) < 34 or "minor" in revision or "bot" in revision:
52             continue
53
54         infcoin_addr = revision['comment'].strip().split()
55         infcoin_addr = infcoin_addr[len(infcoin_addr)-1]
56         if not (len(infcoin_addr) == 34 and infcoin_addr.isalnum() and infcoin_addr.isascii()):
57             continue
58
59         if infcoin_addr in changes['infcoin_addr']:
60             changes['infcoin_addr'][infcoin_addr].add(revision['revid'])
61         else:
62             changes['infcoin_addr'][infcoin_addr] = set([revision['revid']])
63
64
65     # request coins for all changes
66     for addr,revs in changes['infcoin_addr'].copy().items():
67         print("processing:"+addr)
68         for rev in revs:
69             requests.get(config.infcoin_req_url, {"auth_name":config.infcoin_req_user, "auth_secret":config.infcoin_req_pw, "addr":addr, "amount":config.infcoin_req_amount, "reason_uniq":"wiki-"+mws.host+", revid="+str(rev)})
70
71         del changes['infcoin_addr'][addr]
72         with open('data/infcoin_process_changes-'+mws.host+'.json', 'w') as file_handle:
73             json.dump(changes, file_handle)
74
75
76     # warn if not all were processed.
77     if len(changes['infcoin_addr']) != 0:
78         print('Some requests failed!')
79         pprint.pprint(changes)
80
81
82
83 if __name__ == '__main__':
84     def signal_handler(signal, frame):
85         print('SIG received. exitting!')
86         os._exit(0)
87     signal.signal(signal.SIGINT, signal_handler)
88
89     mws = mwclient.Site(config.mw_name, path='/', retry_timeout=120)
90     mws.login(config.mw_user, config.mw_user_pw)
91
92     process_recent_mw_changes(mws)