Skip to content

Commit 4d00230

Browse files
authored
Merge pull request #3687 from nhsuk/gias-download
Add GIAS download command to deployed envs
2 parents ebae793 + 575a28d commit 4d00230

File tree

4 files changed

+68
-53
lines changed

4 files changed

+68
-53
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ gem "govuk_design_system_formbuilder"
4444
gem "govuk_markdown"
4545
gem "jsonb_accessor"
4646
gem "jwt"
47+
gem "mechanize"
4748
gem "notifications-ruby-client"
4849
gem "okcomputer"
4950
gem "omniauth_openid_connect"
@@ -85,7 +86,6 @@ group :development do
8586
gem "aws-sdk-rds", "~> 1"
8687
gem "aws-sdk-s3", "~> 1"
8788
gem "hotwire-livereload"
88-
gem "mechanize"
8989
gem "prettier_print", require: false
9090
gem "rladr"
9191
gem "rubocop-govuk", require: false

app/lib/mavis_cli.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ def self.progress_bar(total)
2222
require_relative "mavis_cli/generate/consents"
2323
require_relative "mavis_cli/generate/vaccination_records"
2424
require_relative "mavis_cli/gias/check_import"
25+
require_relative "mavis_cli/gias/download"
2526
require_relative "mavis_cli/gias/import"

app/lib/mavis_cli/gias/download.rb

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# frozen_string_literal: true
2+
3+
# To get the latest version of the zip:
4+
# 1. Go to https://get-information-schools.service.gov.uk/Downloads
5+
# 2. Check "Establishment fields CSV"
6+
# 3. Submit
7+
# 4. Download the zip file
8+
# 5. Place it in db/data/dfe-schools.zip
9+
#
10+
# Alternatively, you can run this task.
11+
12+
module MavisCLI
13+
module GIAS
14+
class Download < Dry::CLI::Command
15+
desc "Download GIAS schools data"
16+
17+
def call
18+
require "mechanize"
19+
20+
puts "Starting schools data download process..."
21+
agent = Mechanize.new
22+
23+
puts "Visiting the downloads page"
24+
page =
25+
agent.get("https://get-information-schools.service.gov.uk/Downloads")
26+
27+
puts "Checking the establishment fields CSV checkbox"
28+
form = page.form_with(action: "/Downloads/Collate")
29+
form.checkbox_with(id: "establishment-fields-csv-checkbox").check
30+
31+
puts "Submitting the form"
32+
download_page = form.submit
33+
34+
# There is a meta refresh on the download_page. Mechanize didn't seem to
35+
# follow it so we're just refreshing that page manually until the button
36+
# shows up below.
37+
wait_time = 0
38+
until (
39+
download_form =
40+
download_page.form_with(action: "/Downloads/Download/Extract")
41+
) || wait_time > 60
42+
puts "Waiting for the 'Results.zip' link to appear..."
43+
sleep(2)
44+
wait_time += 2
45+
download_page_uri = download_page.uri
46+
download_page = agent.get(download_page_uri)
47+
end
48+
49+
if download_form
50+
download_button = download_form.button_with(value: "Results.zip")
51+
puts "'Results.zip' link found, downloading the file..."
52+
download_file = agent.click(download_button)
53+
puts "Overwriting db/data/dfe-schools.zip"
54+
download_file.save!("db/data/dfe-schools.zip")
55+
puts "File downloaded successfully to db/data/dfe-schools.zip"
56+
else
57+
puts "Download button never appeared, aborting"
58+
end
59+
end
60+
end
61+
end
62+
63+
register "gias" do |prefix|
64+
prefix.register "download", GIAS::Download
65+
end
66+
end

lib/tasks/schools.rake

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,6 @@
11
# frozen_string_literal: true
22

33
namespace :schools do
4-
# To get the latest version of the zip:
5-
# 1. Go to https://get-information-schools.service.gov.uk/Downloads
6-
# 2. Check "Establishment fields CSV"
7-
# 3. Submit
8-
# 4. Download the zip file
9-
# 5. Place it in db/data/dfe-schools.zip
10-
#
11-
# Alternatively, you can run this task.
12-
desc "Download schools data"
13-
task download: :environment do
14-
require "mechanize"
15-
16-
puts "Starting schools data download process..."
17-
agent = Mechanize.new
18-
19-
puts "Visiting the downloads page"
20-
page = agent.get("https://get-information-schools.service.gov.uk/Downloads")
21-
22-
puts "Checking the establishment fields CSV checkbox"
23-
form = page.form_with(action: "/Downloads/Collate")
24-
form.checkbox_with(id: "establishment-fields-csv-checkbox").check
25-
26-
puts "Submitting the form"
27-
download_page = form.submit
28-
29-
# There is a meta refresh on the download_page. Mechanize didn't seem to
30-
# follow it so we're just refreshing that page manually until the button
31-
# shows up below.
32-
wait_time = 0
33-
until (
34-
download_form =
35-
download_page.form_with(action: "/Downloads/Download/Extract")
36-
) || wait_time > 60
37-
puts "Waiting for the 'Results.zip' link to appear..."
38-
sleep(2)
39-
wait_time += 2
40-
download_page_uri = download_page.uri
41-
download_page = agent.get(download_page_uri)
42-
end
43-
44-
if download_form
45-
download_button = download_form.button_with(value: "Results.zip")
46-
puts "'Results.zip' link found, downloading the file..."
47-
download_file = agent.click(download_button)
48-
puts "Overwriting db/data/dfe-schools.zip"
49-
download_file.save!("db/data/dfe-schools.zip")
50-
puts "File downloaded successfully to db/data/dfe-schools.zip"
51-
else
52-
puts "Download button never appeared, aborting"
53-
end
54-
end
55-
564
desc "Add a school to a organisation."
575
task :add_to_organisation,
586
%i[ods_code team_name] => :environment do |_task, args|

0 commit comments

Comments
 (0)