forked from d101tm/tmstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetperformancefiles.py
More file actions
executable file
·203 lines (158 loc) · 7.83 KB
/
getperformancefiles.py
File metadata and controls
executable file
·203 lines (158 loc) · 7.83 KB
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
#!/usr/bin/env python3
""" Get performance information from Toastmasters and write them to files in the work directory.
Unless invoked with --startdate, only gets the latest available information,
including club information (unless --skip-clubs is specified).
If invoked with --startdate, gets information and writes files for that date
(and, if --enddate is specified, for all available dates through --enddate).
Does not get club information because it's not available for past dates.
"""
from datetime import datetime, timedelta, date
import requests
import tmglobals
import tmparms
from tmutil import cleandate, gotoworkdir
myglobals = tmglobals.tmglobals()
# Map filenames to report names from Toastmasters
reportnames = {'clubperf':'clubperformance',
'areaperf':'divisionperformance',
'distperf':'districtperformance'}
def getmonthend(m, y):
lasts = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
if (m == 2) and (0 == y % 4):
eom = (2, 29, y)
else:
eom = (m, lasts[m-1], y)
return '%d/%d/%d' % eom
def makeurl(report, district, tmyearpiece="", monthend="", asof=""):
url = "http://dashboards.toastmasters.org/"
url += "export.aspx?type=CSV&report=" + reportnames[report] + "~" + district
try:
asof = asof.strftime('%m/%d/%Y')
except AttributeError:
pass
return url + "~" + monthend + "~" + asof + "~" + tmyearpiece
def getresponse(url):
headers = {'user-agent': 'curl/7.54.0'}
try:
clubinfo = requests.get(url, headers=headers).text.replace('\r','').split('\n')
except requests.exceptions.SSLError:
clubinfo = requests.get(url, headers=headers, verify=False).text.replace('\r','').split('\n')
# Suppress any leading empty lines
while not clubinfo[0]:
del clubinfo[0]
if len(clubinfo) < 10:
# We didn't get anything of value
print("Nothing fetched for", url)
clubinfo = False
elif clubinfo[0][0] in ['{', '<']:
# This isn't a naked CSV
print("Not a CSV at", url)
clubinfo = False
return clubinfo
def getreportfromWHQ(report, district, tmyearpiece, month, thedate):
url = makeurl(report, district, tmyearpiece, getmonthend(month[0],month[1]), datetime.strftime(thedate, '%m/%d/%Y'))
resp = getresponse(url)
if not resp:
print("No valid response received for %s" % url)
return resp
def gettmyearfordate(d):
if d.month >= 7:
tmyearpiece = "%d-%d" % (d.year, d.year+1)
else:
tmyearpiece = "%d-%d" % (d.year-1, d.year)
return tmyearpiece
def writereportfile(data, report, reportdate, monthend, tmyearpiece):
if data:
with open(makefilename(report, reportdate), 'w') as f:
f.write('\n'.join(data))
print('Wrote %s for %s (month: %s, year: %s)' % (report, reportdate, monthend, tmyearpiece))
else:
print('No data for %s for %s (month %s, year: %s)' % (report, reportdate, monthend, tmyearpiece))
def makefilename(reportname, thedate):
return '%s.%s.csv' % (reportname, thedate.strftime('%Y-%m-%d'))
def getreport(report, district, monthend, tmyearpiece, asof=""):
""" Returns (data, reportdate, reportmonthend) tuple from report """
reportdate = None
reportmonthend = None
url = makeurl(report, district, monthend=monthend, tmyearpiece=tmyearpiece, asof=asof)
data = getresponse(url)
if data:
while not data[-1].strip():
data = data[:-1]
dateline = data[-1].replace(',','')
reportdate = datetime.strptime(cleandate(dateline.split()[-1]), '%Y-%m-%d').date() # "Month of Jun, as of 07/02/2015" => '2015-07-02'
# Figure out the last day of the month for which the report applies
reportmonth = datetime.strptime(dateline.split()[2], "%b").month # Extract the month of the report
if reportmonth == reportdate.month:
reportmonthend = getmonthend(reportmonth, reportdate.year)
else:
reportmonthend = getmonthend(reportmonth, reportdate.year if reportmonth != 12 else reportdate.year-1)
return (data, reportdate, reportmonthend)
def doreportsfor(district, asof):
""" Get and write files for the specified date (if available) """
# Try for the month in question
tmyearpiece = gettmyearfordate(asof)
monthend = getmonthend(asof.month, asof.year)
(data, reportdate, reportmonthend) = getreport('clubperf', district, monthend=monthend, tmyearpiece=tmyearpiece, asof=asof)
if not data:
# Need to try the previous month
if asof.month == 1:
monthend = getmonthend(12, asof.year - 1)
else:
monthend = getmonthend(asof.month -1, asof.year)
(data, reportdate, reportmonthend) = getreport('clubperf', district, monthend=monthend, tmyearpiece=tmyearpiece, asof=asof)
if not data:
# Need to try for June data from the previous TM year (can't be any farther back)
monthend = getmonthend(6, asof.year)
tmyearpiece = gettmyearfordate(date(asof.year, 6, 30))
(data, reportdate, reportmonthend) = getreport('clubperf', district, monthend=monthend, tmyearpiece=tmyearpiece, asof=asof)
if not data:
print("Data not available for ", asof.strftime("%Y-%m-%d"))
else:
writereportfile(data, 'clubperf', reportdate, monthend, tmyearpiece)
# and now do the other two reports
(data, reportdate, monthend) = getreport('areaperf', district, monthend, tmyearpiece, asof)
writereportfile(data, 'areaperf', reportdate, monthend, tmyearpiece)
(data, reportdate, monthend) = getreport('distperf', district, monthend, tmyearpiece, asof)
writereportfile(data, 'distperf', reportdate, monthend, tmyearpiece)
def dolatest(district):
""" Get and write files for the latest available from WHQ """
monthend = ''
tmyearpiece = ''
# Note: We can't fetch areaperf first because we need to give WHQ a valid 'monthend' to get
# back club suspend/charter dates in that report.
for report in ('clubperf', 'areaperf', 'distperf'):
(data, reportdate, monthend) = getreport(report, district, monthend, tmyearpiece)
if monthend and not tmyearpiece:
repmonth = datetime.strptime(monthend, "%m/%d/%Y")
tmyearpiece = gettmyearfordate(repmonth)
writereportfile(data, report, reportdate, monthend, tmyearpiece)
if __name__ == "__main__":
parms = tmparms.tmparms(description=__doc__)
parms.add_argument('--district', type=int)
parms.add_argument('--startdate', default=None)
parms.add_argument('--enddate', default=None)
parms.add_argument('--skipclubs', action='store_true', help='Do not get latest club information.')
myglobals.setup(parms, connect=False)
gotoworkdir()
district = "%0.2d" % int(parms.district)
if not parms.startdate:
# Get today's data
print("Getting the latest performance info")
dolatest(district)
# And get and write current club data unless told not to
# WHQ doesn't supply date information, but it's always as of yesterday
if not parms.skipclubs:
import getclubs
getclubs.writeClubData(parms.district, open(makefilename('clubs', date.today() - timedelta(1)), 'w'))
else:
# We are getting historical data
startdate = datetime.strptime(cleandate(parms.startdate), '%Y-%m-%d').date()
if parms.enddate:
enddate = datetime.strptime(cleandate(parms.enddate), '%Y-%m-%d').date()
else:
enddate = startdate
d = startdate
while d <= enddate:
doreportsfor(district, d)
d += timedelta(1)