-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Added scraper for daily workforce absence data
- Loading branch information
Showing
5 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/lukecarr/dfe-attendance/internal/scraper" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func MakeWorkforceAbsenceCmd() *cobra.Command { | ||
return &cobra.Command{ | ||
Use: "workforce", | ||
Short: "Scrapes (daily) workforce absence data for schools", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
url, _ := cmd.Root().Flags().GetString("dfe-url") | ||
out, _ := cmd.Flags().GetString("output") | ||
scraper.WorkforceAbsence(url, out) | ||
}, | ||
} | ||
} | ||
|
||
func init() { | ||
cmd := MakeWorkforceAbsenceCmd() | ||
cmd.PersistentFlags().String("output", "workforce_absence.csv", "The output CSV file for workforce absence data") | ||
rootCmd.AddCommand(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package scraper | ||
|
||
func WorkforceAbsence(url, out string) { | ||
Generic(url, "data/table_1d_daily_workforce_absence_in_education_settings_during_covid_19_.csv", out) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
import axios from 'axios' | ||
import * as cheerio from 'cheerio' | ||
import AdmZip from 'adm-zip' | ||
|
||
const getCsvText = async () => { | ||
const { data } = await axios.get('https://explore-education-statistics.service.gov.uk/find-statistics/attendance-in-education-and-early-years-settings-during-the-coronavirus-covid-19-outbreak') | ||
|
||
const $ = cheerio.load(data) | ||
|
||
const downloadUrl = $('a[href]').filter(function () { return $(this).text() === 'Download all data' }).first().attr('href') | ||
|
||
const { data: zipData } = await axios.get(downloadUrl, { responseType: 'arraybuffer' }) | ||
const zip = new AdmZip(zipData) | ||
const csvText = zip.readAsText('data/table_1d_daily_workforce_absence_in_education_settings_during_covid_19_.csv', 'utf8') | ||
|
||
return csvText | ||
} | ||
|
||
export default async function handler(_: NextApiRequest, res: NextApiResponse) { | ||
const csvText = await getCsvText() | ||
|
||
if (process.env.NODE_ENV === 'production') res.setHeader('Cache-Control', `public,max-age=${60 * 60 * 24},immutable`) | ||
|
||
res.status(200) | ||
.setHeader('Content-Type', 'text/csv') | ||
.setHeader('Content-Disposition', 'attachment;filename=daily_workforce_absence.csv') | ||
.send(csvText) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
a7035f2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: