Skip to content

Commit

Permalink
Fix froide output and improve code style
Browse files Browse the repository at this point in the history
  • Loading branch information
quinn-dev committed Jul 14, 2021
1 parent 24183cc commit ca8432f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 51 deletions.
65 changes: 31 additions & 34 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import {
string,
any,
assert,
optional,
nullable
} from 'superstruct'
import ora from 'ora'
import enquirer from 'enquirer'
import pMap from 'p-map'
import {District, SimpleSchool, DetailedSchool} from './types.js'
import { quit, quitWithError } from './cli.js'
import {quit, quitWithError} from './cli.js'

const baseURL = 'https://schulfinder.kultus-bw.de/api/'

Expand Down Expand Up @@ -90,19 +89,19 @@ export const getSchools = async (parameters: string | Record<string, string | nu
})
)

return api.get(endpoint, {
const response = await api.get(endpoint, {
searchParams: parameters
})
.then(response => {
assert(response, ExpectedResponse)
return response
})

assert(response, ExpectedResponse)

return response
}

export const getSchoolsByURL = async (url: string, froide = false, quiet = false) => {
const spinner = ora()

if (!(new URL(url).hostname == 'schulfinder.kultus-bw.de')) {
if (!(new URL(url).hostname === 'schulfinder.kultus-bw.de')) {
quitWithError(`Invalid URL: ${url}`)
}

Expand All @@ -115,21 +114,19 @@ export const getSchoolsByURL = async (url: string, froide = false, quiet = false
} else {
const queryString = Buffer.from(base64String, 'base64').toString('utf-8')

if (froide) {
if (new URLSearchParams(queryString).get('outposts') == '1') {
spinner.warn('You have included outposts in your query. Importing outposts into Froide is highly discouraged.')

const answer = await enquirer.prompt({
type: 'confirm',
name: 'continue',
message: 'Do you want to continue?',
format: value => !!value ? 'yes' : 'no'
}) as {continue: boolean}

if (!answer.continue) {
spinner.fail('Aborted')
quit(1)
}
if (froide && new URLSearchParams(queryString).get('outposts') === '1') {
spinner.warn('You have included outposts in your query. Importing outposts into Froide is highly discouraged.')

const answer: {continue: boolean} = await enquirer.prompt({
type: 'confirm',
name: 'continue',
message: 'Do you want to continue?',
format: value => value ? 'yes' : 'no'
})

if (!answer.continue) {
spinner.fail('Aborted')
quit(1)
}
}

Expand Down Expand Up @@ -162,7 +159,7 @@ export const getSchoolsByDistricts = async (districts: District[], quiet = false
schoolsWithoutDetails.push(...schoolsByDistrict)

spinner.text = `Loading school list: ${district} (${districtCounter}/${districts.length})`

districtCounter += 1
})

Expand Down Expand Up @@ -195,22 +192,22 @@ export const getSchoolDetails = async (uuid: string): Promise<DetailedSchool> =>
lng: number(),
official: number(),
branches: array(
object({
branch_id: number(),
acronym: string(),
description_long: string()
})
object({
branch_id: number(),
acronym: string(),
description_long: string()
})
),
trades: array()
})

return api.get(endpoint, {
const response = await api.get(endpoint, {
searchParams: {uuid}
})
.then(response => {
assert(response, ExpectedResponse)
return response
})

assert(response, ExpectedResponse)

return response
}

export const getMultipleSchoolDetails = async (schools: SimpleSchool[], quiet = false) => {
Expand Down
12 changes: 8 additions & 4 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,18 +104,22 @@ export const quitWithError = (message: string) => {
const schoolsWithoutProblems = []

for (const school of formattedSchools) {
if (school.problems.length > 0) {
const {problems, ...rest} = school

if (problems) {
schoolsWithProblems.push(school)
} else {
schoolsWithoutProblems.push(school)
schoolsWithoutProblems.push(rest)
}
}

writeFileSync(outputFile, unparse(schoolsWithoutProblems))
writeFileSync(options.problems, unparse(schoolsWithProblems))
} else if (problemCount === 0) {
writeFileSync(outputFile, unparse(formattedSchools.map(({problems, ...rest}) => rest)))
} else {
writeFileSync(outputFile, unparse(formattedSchools))
}

writeFileSync(outputFile, unparse(formattedSchools))
} else {
writeFileSync(outputFile, JSON.stringify(schoolsWithDetails))
}
Expand Down
42 changes: 29 additions & 13 deletions src/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,40 @@ export const formatToFroide = (schools: DetailedSchool[]) => {
Otherwise return 'Schule'
*/
const getClassification = () => {
const acronyms = [
'PROG', 'G8', 'SGGG',
'SGGS', 'WGF', 'WGW',
'TGG', 'TGI', 'TGM',
'TGU', 'WGI', 'G9',
'BTG', 'EG', 'TGT',
'TGTM', '6TG', '6WG',
'TGE', 'TGN', '6ESG',
'AG', 'GA3', 'GA7',
const acronyms = new Set([
'PROG',
'G8',
'SGGG',
'SGGS',
'WGF',
'WGW',
'TGG',
'TGI',
'TGM',
'TGU',
'WGI',
'G9',
'BTG',
'EG',
'TGT',
'TGTM',
'6TG',
'6WG',
'TGE',
'TGN',
'6ESG',
'AG',
'GA3',
'GA7',
'FHOEGYM'
]
])

const isGymnasium = ({acronym, description_long}: {acronym: string, description_long: string}) => {
if (acronyms.includes(acronym)) {
const isGymnasium = ({acronym, description_long}: {acronym: string; description_long: string}) => {
if (acronyms.has(acronym)) {
return true
}

if(/[gG]ymnasium/.test(description_long)) {
if (/[gG]ymnasium/.test(description_long)) {
return true
}

Expand Down

0 comments on commit ca8432f

Please sign in to comment.