|
1 | 1 | # frozen_string_literal: true
|
2 | 2 |
|
3 | 3 | 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 |
| - |
56 | 4 | desc "Add a school to a organisation."
|
57 | 5 | task :add_to_organisation,
|
58 | 6 | %i[ods_code team_name] => :environment do |_task, args|
|
|
0 commit comments