summary refs log tree commit diff
path: root/src/main.rs
blob: 17a95c7269869f9f60a827a1db54395089124b11 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// SPDX-License-Identifier: AGPL-3.0-or-later

//! # VUW Course scraper
//!
//! This is a simple program capable of parsing VUWs courses from the registry. It cannot correctly
//! parse prerequisites, however.

use std::{collections::HashMap, fs};

use scraper::Html;
use serde::{Deserialize, Serialize};
use tracing::level_filters::LevelFilter;
use vuw_course_scraper::{parse_document, Course};

/// Utility struct for exporting to JSON.
#[derive(Clone, Deserialize, Serialize)]
struct JsonExport<'a> {
	/// [`HashMap`] of all courses.
	#[serde(borrow)]
	courses: HashMap<&'a str, Course<'a>>,
}

fn main() {
	tracing_subscriber::fmt()
		.with_max_level(LevelFilter::INFO)
		.init();

	let html = &fs::read_to_string("./courses.html").expect("file does not exist");

	let document = Html::parse_document(html);

	let course_map = parse_document(&document);

	fs::write(
		"./export.json",
		simd_json::serde::to_string(&JsonExport {
			courses: course_map,
		})
		.expect("json should parse correctly"),
	)
	.expect("file should be writable");
}