Parses Hansard data from the Australian Parliament.
Included as the Parser component for OpenAustralia.org using git submodule. After a change merges here, the submodule pointer in the umbrella openaustralia repository needs bumping before production picks it up.
See for installation instructions https://openaustralia.github.io/openaustralia/install-parser.html
You need to clone the TWFY repo for database schema and test setup:
cd ..
git clone git@github.com:openaustralia/twfy.gitAlternatively, you can clone the openaustralia repo with submodules:
cd ..
git clone git@github.com:openaustralia/openaustralia.git
cd openaustralia
git submodule init
git submodule updateThis gives access to the DB schema needed for tests, and if you want to test the php and perl scripts from the other repos, those as well.
Use Docker to run MySQL 8.4 for testing:
make services-up # Start MySQL in the background, once it's readyIf port 3306 is already in use, or you run more than one worktree at once, see Port clashes in AGENTS.md.
The test configuration uses root/root credentials and openaustralia_test database automatically. The database schema is automatically loaded from ../twfy/db/schema.sql when tests run.
Startup fails loudly on purpose, so a broken setup gets noticed instead of hiding behind a container that looks fine.
If setup fails, or takes longer than START_LIMIT (5 minutes here, set in .devcontainer/compose.yaml), the app
container stops, make up reports the failure, and the dev container shows an error instead of opening.
To fix it:
make statusshows which container stopped, andmake logs SERVICE=appshows why.make shellopens a shell in a freshappcontainer even when it has stopped, so you can rerun the setup by hand and fix the problem.make up, or reopen the dev container, to start again.
On a slow machine, a longer limit can go in .env, for example START_LIMIT=20m.
Install mise (preferred) or rbenv, plus the Ubuntu packages the gems need (see
.devcontainer/Dockerfile), then:
make setup # Ruby from .ruby-version, gems, configuration.yml and ../rblib, and starts MySQLEdit configuration.yml as needed.
- Uncomment the development options so you dont need to configure twfy and have a working php
Called from the OpenAustralia.org web application (twfy - another git submodule AKA They Work For You):
- 1:37am daily — dailyupdate (staging + production)
bundle exec parse-member-links.rb
- 9:05am Mon-Fri — morningupdate (staging + production) — this is the one that calls parse-speeches.rb
bundle exec parse-speeches.rb previous-working-daybundle exec sitemap.rb
- These do not appear to call the parser
- 4:23am Sunday — weeklyupdate (staging + production)
- 10:00am Mon-Fri — alertmailer (staging + production)
#{web_root}/rblib/config.rb- required bylib/configuration.rb- clone from
https://github.com/openaustralia/rblib/ #{web_root}/twfy/conf/general- used by MySociety::Config.set_file (can override in configuration.yml)
- clone from
#{conf.web_root}/twfy/bin/run scripts/xml2db.pl- called byparse-speeches.rb#{conf.web_root}/twfy/bin/run scripts/xml2db.pl(perl) - called byparse-members.rb(Use--no-loadto skip)#{conf.web_root}/twfy/bin/run scripts/mpinfoin.pl(perl) - called byparse-member-links.rb
See How to Install for detailed instructions
# create members information
bundle exec ./parse-members.rb --no-load
# to download images
mkdir -p ../openaustralia/twfy/www/docs/images/mpsXL
# export DEBUG=1 # to enable debugging output for request throttling
bundle exec ./member-images.rb
# 3 images per person in ../openaustralia/twfy/www/docs/images/{mps,mpsL,mpsXL}
# Crontab runs:
bundle exec parse-member-links.rb
bundle exec parse-speeches.rb previous-working-day
bundle exec sitemap.rb
# Misc Utilities
# This splits and combines several large pdfs containing the Register of Members' Interests into one pdf per Senator/Member
# Unable to test without data/register_of_interests/senate/2010_06.pdf and other pdfs being present
# (The pdfs do not exist on production or staging)
bundle exec ruby register-split.rbThe steps above (make services-up + bundle exec rspec) get you a working test database, but that's
throwaway and only used by the spec suite. To actually run the parser against real Hansard data and see it land in
a browsable TWFY site, use twfy's own dev stack instead:
# In the twfy checkout (sibling of this one)
cd ../twfy
make docker # builds and starts the docker-compose stack (mysql + webhost)
make docker-db-migrate
make docker-db-seedtwfy/conf/general (symlinked from shared/ at deploy time, but present directly in a docker dev checkout) points
DB_HOST at mysql, the docker-compose internal hostname - not reachable from your host shell. twfy/Makefile.dev
bridges this for you: it overrides DB_HOST=127.0.0.1, DB_PORT (mapped to 3306 by default) and RAWDATA
(this repo's xml_path, so downloaded/rewritten XML ends up somewhere sane) and calls this repo's scripts via
bin/run. From twfy/:
make -f Makefile.dev parse-members # seed people/electorates first
make -f Makefile.dev parse-speeches ARGS="2025.01.01 2025.01.31" # backfill a real month
make -f Makefile.dev xapian-index # make the new speeches searchable(Note the -f Makefile.dev - it's a separate file from twfy's main Makefile, not included by it, so plain
make parse-speeches won't find the target.)
Without --no-load (the Makefile.dev targets don't pass it), parse-speeches.rb loads into the database
automatically as its last step - but only once, after every day in the range has been fetched and parsed. A
crash partway through a long range (a bad day's data, a network blip) means nothing from that run gets loaded, no
matter how much of the range succeeded before the crash. For a large backfill, prefer a handful of smaller date
ranges over one huge one, so a failure only costs you that chunk.
parse-speeches.rb reads each day from the local origxml cache and only asks APH for days it doesn't have.
make pull-xml fills that cache from https://data.openaustralia.org.au/origxml/ instead, so a development
checkout can parse real days without APH. It is a Go tool in tools/pull-xml, so it needs Go on the host:
make pull-xml # every day the server has, for both sources
make pull-xml YEAR=2025 # one year
make pull-xml SOURCE=origxml # one source (origxml or scrapedxml) instead of both
make pull-xml ARGS="--from=2020-02-04 --to=2020-02-13"
make pull-xml ARGS=--dry-run # list what the server has, download nothingMake takes any --option for itself, so the year is YEAR=2025, not --year=2025, and the source is SOURCE=, not
--source= inside ARGS. Run directly (cd tools/pull-xml && go run . --year=2025), the tool takes --year, --all
or --from and --to, and one --source (origxml by default); it insists on one of them so a run can't copy the
whole archive by accident.
It fetches ten days at once (--workers), with a progress bar on a terminal. It copies a day only when it is missing
locally or newer on the server (If-Modified-Since, using the local file's modification time), and never overwrites
a local file that is newer, such as one refreshed from APH (--force does). It stops if the server answers 429 or
503, and --delay makes each worker wait between requests. A first run for every year is thousands of requests;
later runs are mostly "up to date" answers.
Production's copies were cached the morning after each sitting, so older days are still at proof stage.
make pull-xml-test runs its tests; CI doesn't run them.
The scrapedxml source is production's parsed output, which xml2db.pl loads into the database without running the
parser. Old days there were parsed before the parser wrote a proof attribute, so they load as unknown.
There's no local "before" baseline to diff against - a fresh dev DB has no prior parser run to compare with. The
useful comparison is against production, which is still real Hansard data, parsed and public:
https://data.openaustralia.org.au/scrapedxml/ has the final per-day, per-house XML for every day production has
ever parsed. Pull the same date range you backfilled locally and diff structurally (speech counts, speakername
tallies, approximate_wordcount sums) rather than byte-for-byte, since cosmetic differences (eg how an em dash is
encoded) don't mean anything's wrong.
make test runs the specs in Docker, as CI does (make test ARGS=spec/lib/name_spec.rb runs just one file), and
make lint runs RuboCop. They need nothing but Docker.
To run them on the host after make setup:
make services-up # Start MySQL via Docker, if it isn't running
EXCLUDE_NEWS=1 bundle exec rspec # as CI does; the sitemap's news section needs PHP
bundle exec rubocop
make down # Stop MySQL (make clobber also deletes its data)The specs load the schema from ../twfy/db/schema.sql if you have it, else from spec/fixtures/schema.sql.
While we have largely automated data updates to OpenAustralia.org, the
parliamentary calendar and people records (and their ministerial roles) must be updated manually. Every
by-election, resignation, party change, general election and ministerial reshuffle is a hand edit to the CSV files
in data/.
The full workflows, file formats and conventions are documented in docs/data-updates.md. In short:
- Someone leaves parliament: fill in the end date and reason on their row in
data/representatives.csvordata/senators.csv. - Someone enters parliament: add a person row to
data/people.csvand a membership row to the chamber file. - Someone changes party (or becomes Speaker): close their current membership row with reason
changed_partyand add a new one. - Ministerial reshuffle: reconcile
data/ministers.csv/data/shadow-ministers.csvagainst the tabled Ministerial Arrangements document. - Recess/sitting dates: edit
recess.phpin the twfy repository, not this one.
Check any data change with bundle exec ./parse-members.rb --no-load before opening a PR.
Parliament publishes each day's Hansard as a proof (draft) first and finalises it later, and the parser caches the
first copy it fetches. refresh-proof-days.rb finds the days in the origxml cache that are still at proof stage
(without calling APH), fetches a fresh copy of up to --limit of them (default 100, each house's day counts as one),
and loads the changed days into the database. Nothing runs it on a schedule.
bundle exec ./refresh-proof-days.rb --limit=20
bundle exec ./refresh-proof-days.rb --no-load # only generate the XML
Order. It starts at a day 12 months ago (--around=DATE changes it) and works outward, alternating between the
nearest day on or after that date and the nearest day before it, so each run reaches toward today and toward the
past together. On the same date the Representatives come before the Senate.
Safety. It never picks a day from the last 9 weeks. The alert mailer skips anything dated more than 8 weeks ago
(AlertMaxAge in twfy), so loading an older day again cannot email anyone, and the extra week covers the gap between a
run and the next mailer run. This applies to every mode.
Rate limits. If APH answers 403 or 429 (the agent has already retried with a growing wait), the script stops at once, loads the days it finished, and leaves the rest for the next run. Nothing marks those days as done, so they are still in the list next time.
--unknown works on the days we cannot yet say are proof or final instead: those whose scrapedxml has no proof
attribute, because the day was parsed before the parser wrote one. A day whose cached copy is final is parsed again
from the cache without calling APH, and a day whose cached copy is proof is refreshed from APH. Each day gets its
attribute, so it drops out of the list, and a daily run with a small --limit works through the backlog.
bundle exec ./refresh-proof-days.rb --unknown --limit=10
It keeps the cached copy unless the new one is valid, as parse-speeches.rb --proof does.
Every weekday the ./parse-speeches.rb script gets run by cron to parse and import new speeches into OpenAustralia.org.
From time to time this fails. We can see this when there's a missing day of debates on OpenAustralia.org on the
calendar, e.g. http://www.openaustralia.org.au/debates/?y=2017
Or by checking the cron output which is emailed to the OAF web-administrators Google Group and posted in the
#openaustralia-log Slack channel. It will look something like this:
Start time: 2017-09-05 09:05:01 AEST
Parsing from APH to XML and loading into the database
parse-speeche: 0% | | ETA: --:--:--
parse-speeche: 50% |ooooooooooooooooooooo | ETA: 00:00:19
./../openaustralia-parser/lib/hansard_parser.rb:197:in `throw': uncaught throw `2017-09-04 senate: Couldn't figure out who Brockman, S is in division (voting no)' (NameError)
from ../../openaustralia-parser/lib/hansard_parser.rb:197:in `parse_date_house'
from ../../openaustralia-parser/lib/hansard_parser.rb:193:in `map'
from ../../openaustralia-parser/lib/hansard_parser.rb:193:in `parse_date_house'
from ../../openaustralia-parser/lib/hansard_parser.rb:159:in `each'
from ../../openaustralia-parser/lib/hansard_parser.rb:159:in `parse_date_house'
from ../../openaustralia-parser/parse-speeches.rb:94
Xapian indexing
Running rssgenerate
./sitemap.rb:386:in `notify_search_engines': uninitialized constant Sitemap::PINGMYMAP_API_URL (NameError)
from ./sitemap.rb:453
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap1.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap2.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap3.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap4.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap5.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap6.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap7.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap8.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap9.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap10.xml.gz)...
Writing sitemap file (/srv/www/www.openaustralia.org/current/twfy/www/docs/sitemaps/sitemap11.xml.gz)...
Whole thing done time: 2017-09-05 10:40:40
You can see in the output above there's an exception thrown by the parser. It's complaining about not being able to figure out who the person is that's voted in a division. This is one of the most common failures. The fix is to add the person or an alternative name for them (see docs/data-updates.md) and run the parser again for the missing days.
The other most common failure is that APH has published something wacky. To fix this you need to delete the cache for that day and run the parser again.
Once you've loaded the speeches, you'll need to index the days you just loaded.
After you've fixed a parsing issue and loaded in data from the missing
days, you may need to manually add those dates to the index. The
morningupdate cron job normally indexes yesterday's speeches, but if
that's missed we don't have anything that will automatically find and
index the missing days.
However, this is fairly easy to do. To index all speeches between the 25th June 2018 and 17th August 2018:
export XAPIANDB=/srv/www/production/shared/search/searchdb
/srv/www/production/current/twfy/search/index.pl $XAPIANDB daterange2018-06-25 2018-08-17
xapian indexing debate 2018-06-25
xapian indexing lords 2018-06-25
xapian indexing debate 2018-06-26
xapian indexing lords 2018-06-26
xapian indexing debate 2018-06-27
xapian indexing lords 2018-06-27
xapian indexing debate 2018-06-28
xapian indexing lords 2018-06-28
xapian indexing debate 2018-08-13
xapian indexing lords 2018-08-13
xapian indexing debate 2018-08-14
xapian indexing lords 2018-08-14
xapian indexing debate 2018-08-15
xapian indexing lords 2018-08-15
xapian indexing debate 2018-08-16
xapian indexing lords 2018-08-16
If you've fixed a failure and you load lots of missing data into OpenAustralia.org it will automatically send lots of email alerts the next day. This probably isn't what the subscribers want so you need a way to skip sending email alerts for the data you've just loaded.
tl;dr Bump the timestamp and batch number in alerts-lastsent which is in twfy/scripts.
The alerts-lastsent file has two lines, the first is a Unix timestamp and the second is a batch ID.
After you've loaded debates into the DB and run indexing update the first line to the current time as this
will be later than the index you just did - you can find out the time with date +%s. Then increment the batch ID on
the second line and save the file.
Now when you run alertmailer.php it shouldn't send out alerts for any of the debates you've just loaded. There's more
detail in this helpful email from mySociety's Matthew Somerville.
I've never quite understood the relationship between the alert emailer and the xapian index. Unfortunately, looking through the code hasn't helped me much. :-(
Sorry, it's all a bit strewn about. I'll try and explain below.
Let's say I make some fixes to the parser and I regenerate all the XML files. I then reload this into the database and do a xapian reindex on everything. How can I do this without generating spurious email alerts the next day for any pages that have changed?
Unbelievably, it might just work, although there are a number of extra things you can do if you're worried about it ( or just test it better than we ever did ;) ). Here's the full process of how email alerts runs, then some history, and then some actually maybe useful stuff ;)
Firstly, the database. When you load data with xml2db.pl, a brand new GID gets new created and modified timestamps set to NOW(). When you reload data, if the GID is the same then only the modified timestamp is updated to NOW(), the created doesn't change ever.
When running a Xapian index with index.pl, if you use "sincefile" as we do, then it only indexes GIDs that have a modified column dated since the last time it ran. If you run Xapian index with a date range etc., then it will index or reindex everything on those dates.
Whatever is decided to be indexed, each gets a new batch number (that goes up 1 each time you run Xapian indexing and it indexes something), and also gets a "created" value consisting of its hansard created column concatenated with its hpos.
When you run the alert email script, it only fetches those GIDs that have: a) a batch number larger than the highest batch number last time the script was run; b) a created higher than the last time the script was run; c) is later than the manual date given in the script (line 174ish).
So let's do a worked example. You've fixed a parser bug which has caused 10 GIDs on 2001-01-01 to change, and 5 new GIDs to appear on 2002-02-02. You ran the email alert script yesterday, before doing this, up to batch 100. You reload the XML for those two days, the 10 GIDs on 2001-01-01 get their modified column updated, the 5 new GIDS on 2002-02-02 get inserted. You reindex with sincefile, and those 15 GIDs get indexed (or you reindex with those two dates and everything on those 2 days gets indexed), with batch ID 101. You run the email alert script, which will only return things: * in batch ID 101 * created since yesterday - ie the 5 new GIDs from 2002-02-02, but not the 10 from 2001-01-01.
Hope that makes sense. Now, the original theory was that people would want to be alerted to new stuff even if it was old. But it turns out that a) they don't (they'll complain saying why have they got an alert for old stuff), b) all of our old "new" stuff wasn't actually new, but was because of parser fixes or because they make spelling corrections and our model means everything on that day gets a new GID in that case - which is awful but it's too late to change now...
So what we do. I try to make sure any changing of old content isn't added/indexed along with new stuff - ie. run major changes during holidays etc.. Then when the loading/indexing is done, I can manually bump the batch number/ timestamp in the alerts-lastsent file so that the alert script thinks it's been run on that data already and will just ignore it.
If there's some new stuff indexed as well, which you do want to send alerts for, then that's what the manual date in the alert script is for, to just say "ignore anything before this date completely". Bit yucky, I know, but then so's everything.
Phew, hope that's of use to you, and happy Easter!
ATB, Matthew
bundle exec rake
This runs the RSpec suite (also available directly via bundle exec rspec). There is currently no CI, so a green
local run is the only gate.
bundle exec rubocop
Either check Dependabot alerts for the main branch, taking note of when the last check was run, or run manually on current branch:
bundle exec ruby-audit
bundle exec bundle-audit
Many instances have a *_count or count property that serves as a unique identifier.
Despite the name, it does not mean "how many"! It can be:
- the number of instances created so far, making it volatile if rows are added or removed in the source data, or
- read directly from a file (eg
data/people.csv), and is not always sequential nor increasing in value.
Period for instance has a id property that will not be unique when a person serves multiple times for the same house
in different capacities, eg for a different party.
Copyright OpenAustralia Foundation Limited. Licensed under the Affero GPL. See the LICENSE.txt file for more details.
