-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingler.py
executable file
·221 lines (200 loc) · 7.45 KB
/
pingler.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import inspect
import requests
import traceback
from prettytable import PrettyTable
from datetime import date
# ensure utf-8 encoding
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def pingler():
# ensure CAcert bundle is available
if 'REQUESTS_CA_BUNDLE' not in os.environ:
os.environ['REQUESTS_CA_BUNDLE'] = '/etc/ssl/certs/cacert.org.pem'
# getting all of the member information
try:
# using ~/.netrc for authentication
r = requests.get('https://vorstand.c-base.org/cteward-api/legacy/member/*')
if r.status_code != 200:
print "Server request failed, code %d\n" % r.status_code
print r.text
sys.exit(1)
members_arr = r.json()['results']
except KeyboardInterrupt:
print "Shutdown requested...exiting"
except requests.exceptions.SSLError as e:
print "SSL certificate error.\n"
print e
sys.exit(1)
except Exception as e:
traceback.print_exc(file=sys.stdout)
sys.exit(1);
members = {}
for member in sorted(members_arr):
# FIXME: we're assuming this is always set & unique (it should be)
members[member['Crewname']] = member
# getting all of the member information
try:
# using ~/.netrc for authentication
r = requests.get('https://vorstand.c-base.org/cteward-api/legacy/member/*/raw')
if r.status_code != 200:
print "Server request failed, code %d\n" % r.status_code
print r.text
sys.exit(1)
members_raw_arr = r.json()
except KeyboardInterrupt:
print "Shutdown requested...exiting"
except requests.exceptions.SSLError as e:
print "SSL certificate error.\n"
print e
sys.exit(1)
except Exception as e:
traceback.print_exc(file=sys.stdout)
sys.exit(1);
members_raw = {}
for member_raw in sorted(members_raw_arr):
# FIXME: we're assuming this is always set & unique (it should be)
members_raw[member_raw['Kurzname']] = member_raw
contributions = {}
for member in sorted(members):
# getting all of the contribution information
try:
# using ~/.netrc for authentication
r = requests.get('https://vorstand.c-base.org/cteward-api/legacy/member/%s/contributions' % member)
if r.status_code != 200:
print "Server request failed, code %d\n" % r.status_code
print r.text
sys.exit(1)
contributions[member] = r.json()
except KeyboardInterrupt:
print "Shutdown requested...exiting"
except requests.exceptions.SSLError as e:
print "SSL certificate error.\n"
print e
sys.exit(1)
except Exception as e:
traceback.print_exc(file=sys.stdout)
sys.exit(1);
if len(contributions) > 0:
x = PrettyTable([
"Crewname",
"Status",
"T",
"Err",
"Vorher",
str(date.today().year-3),
str(date.today().year-2),
str(date.today().year-1),
str(date.today().year),
"Gesamt",
"M",
"Kontakt",
"N"
])
x.align["Crewname"] = "l"
x.align["Status"] = "l"
x.align["T"] = "l"
x.align["Err"] = "r"
x.align["Vorher"] = "r"
x.align[str(date.today().year-3)] = "r"
x.align[str(date.today().year-2)] = "r"
x.align[str(date.today().year-1)] = "r"
x.align[str(date.today().year) ] = "r"
x.align["Gesamt"] = "r"
x.align["M"] = "r"
x.align["Kontakt"] = "r"
x.align["N"] = "r"
maxlen_name = 0
maxlen_stat = 0
error_total = 0
yearb_total = 0 # Years before
yearx_total = 0 # Year-3
yeary_total = 0 # Year-2
yearz_total = 0 # Year-1
yearn_total = 0 # Year (now)
total_total = 0
for contributor in sorted(contributions):
if contributions[contributor]['total']['unpaid'] == 0:
continue
if len(contributor) > maxlen_name:
maxlen_name = len(contributor)
if len(members[contributor]['Status']) > maxlen_stat:
maxlen_stat = len(members[contributor]['Status'])
yearb_sum = 0 # Sum for years before
yearb = "" # Years before
yearx = "" # Year-3
yeary = "" # Year-2
yearz = "" # Year-1
yearn = "" # Year (now)
for year in contributions[contributor]['years']:
if year == str(date.today().year):
yearn = u"%.02f€" % contributions[contributor]['years'][year]['unpaid']
yearn_total += contributions[contributor]['years'][year]['unpaid']
elif year == str(date.today().year-1):
yearz = u"%.02f€" % contributions[contributor]['years'][year]['unpaid']
yearz_total += contributions[contributor]['years'][year]['unpaid']
elif year == str(date.today().year-2):
yeary = u"%.02f€" % contributions[contributor]['years'][year]['unpaid']
yeary_total += contributions[contributor]['years'][year]['unpaid']
elif year == str(date.today().year-3):
yearx = u"%.02f€" % contributions[contributor]['years'][year]['unpaid']
yearx_total += contributions[contributor]['years'][year]['unpaid']
else:
yearb_sum += contributions[contributor]['years'][year]['unpaid']
yearb = u"%.02f€" % yearb_sum
yearb_total += contributions[contributor]['years'][year]['unpaid']
x.add_row([
contributor,
members[contributor]['Status'],
members_raw[contributor]['Zahlungsart'],
"???",
yearb,
yearx,
yeary,
yearz,
yearn,
u"%.02f€" % contributions[contributor]['total']['unpaid'],
"?",
"--.--.----",
"?"
])
total_total += contributions[contributor]['total']['unpaid']
x.add_row([
'=' * maxlen_name,
'=' * maxlen_stat,
"=",
"===",
"=" * len(u"%.02f€" % yearb_total),
"=" * len(u"%.02f€" % yearx_total),
"=" * len(u"%.02f€" % yeary_total),
"=" * len(u"%.02f€" % yearz_total),
"=" * len(u"%.02f€" % yearn_total),
"=" * len(u"%.02f€" % total_total),
"=",
"==========",
"="
])
x.add_row([
"",
"Total",
"",
"???",
u"%.02f€" % yearb_total,
u"%.02f€" % yearx_total,
u"%.02f€" % yeary_total,
u"%.02f€" % yearz_total,
u"%.02f€" % yearn_total,
u"%.02f€" % total_total,
"",
"",
""
])
print x
sys.exit(1)
else:
sys.exit(0)
if __name__ == '__main__':
pingler()