forked from d101tm/tmstats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeteducationals.py
More file actions
executable file
·89 lines (69 loc) · 3.18 KB
/
geteducationals.py
File metadata and controls
executable file
·89 lines (69 loc) · 3.18 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
#!/usr/bin/env python3
# Get Educational Awards
from bs4 import BeautifulSoup
import requests
import sys
import tmglobals
import tmutil
myglobals = tmglobals.tmglobals()
def inform(*args, **kwargs):
""" Print information to 'file' unless suppressed by the -quiet option.
suppress is the minimum number of 'quiet's that need be specified for
this message NOT to be printed. """
suppress = kwargs.get('suppress', 1)
file = kwargs.get('file', sys.stderr)
if parms.quiet < suppress:
print(' '.join(args), file=file)
### Insert classes and functions here. The main program begins in the "if" statement below.
def makekey(membername, clubname, award, awarddate):
return '%s+%s+%s+%s' % (membername, clubname, award, awarddate)
if __name__ == "__main__":
# Handle parameters
import tmparms
parms = tmparms.tmparms()
parms.add_argument('--quiet', '-q', action='count', default=0)
parms.add_argument('--district', type=int)
# Do global setup
myglobals.setup(parms)
conn = myglobals.conn
curs = myglobals.curs
# Get the existing awards (significant parts only) so we don't create duplicates. We can't
# let the datbase do it because we don't have a valid unique key - a person can earn
# identical awards for the same club on the same date.
existing = set()
curs.execute("SELECT membername, clubnumber, award, awarddate FROM awards")
for (membername, clubnumber, award, awarddate) in curs.fetchall():
existing.add(makekey(membername, clubnumber, award, tmutil.stringify(awarddate)))
# Get the data from Toastmasters
url = "http://reports.toastmasters.org/reports/dprReports.cfm?r=3&d=%s&s=Club&sortOrder=0" % parms.district
data = requests.get(url).text
# Parse it
soup = BeautifulSoup(data, 'html.parser')
awards = []
# We want siblings of the first table row of class "content"
starter = soup.find('tr', class_='content')
line = starter.find_next_sibling("tr")
while line:
candidate = [s.strip() for s in line.stripped_strings]
line = line.find_next_sibling("tr")
if candidate[3].startswith("P") and candidate[3][-1] not in ['1', '2', '3', '4', '5']:
continue # Skip pending awards
# Reformat the date
dparts = candidate[4].split('/')
candidate[4] = dparts[2] + '-' + dparts[0] + '-' + dparts[1]
tmyear = int(dparts[2])
month = int(dparts[0])
if month <= 6:
tmyear = tmyear - 1
candidate.append(tmyear)
candidate.append(parms.district)
if makekey(candidate[5], candidate[0], candidate[3], candidate[4]) not in existing:
awards.append(candidate)
# And insert awards into the database
if awards:
changecount = curs.executemany("INSERT INTO awards (clubnumber, division, area, award, awarddate, membername, clubname, clublocation, tmyear, district) VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", awards)
else:
changecount = "No"
inform("%s %s added" % (changecount, "awards" if changecount != 1 else "award"))
conn.commit()
conn.close()