Skip to content

Commit 59af235

Browse files
committed
feat: sync courses cronjob
1 parent 47b1a05 commit 59af235

File tree

8 files changed

+1130
-497
lines changed

8 files changed

+1130
-497
lines changed

src/config/algolia.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import algoliasearch from "algoliasearch";
2+
import type { Context } from "hono";
3+
import { env } from "hono/adapter";
4+
5+
const algolia = (c: Context) => {
6+
const { ALGOLIA_APP_ID, ALGOLIA_API_KEY } = env<{
7+
ALGOLIA_APP_ID: string;
8+
ALGOLIA_API_KEY: string;
9+
}>(c);
10+
11+
return algoliaWithEnv(ALGOLIA_APP_ID, ALGOLIA_API_KEY);
12+
}
13+
14+
export const algoliaWithEnv = (appId: string, apiKey: string) => {
15+
const client = algoliasearch(appId, apiKey);
16+
const index = client.initIndex("nthu_courses");
17+
return index;
18+
}
19+
20+
export default algolia;

src/config/supabase_server.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,20 @@ import type { Database } from "../types/supabase";
33
import type { Context } from "hono";
44
import { env } from "hono/adapter";
55

6+
export const supabaseWithEnv = (url: string, key: string) => {
7+
if (!url || !key) {
8+
throw new Error("Supabase credentials not found");
9+
}
10+
return createClient<Database>(url, key);
11+
}
12+
613
const supabase_server = (c: Context) => {
714
const { SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY } = env<{
815
SUPABASE_URL: string;
916
SUPABASE_SERVICE_ROLE_KEY: string;
1017
}>(c);
11-
if (!SUPABASE_URL || !SUPABASE_SERVICE_ROLE_KEY) {
12-
throw new Error("Supabase credentials not found");
13-
}
14-
return createClient<Database>(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY);
18+
return supabaseWithEnv(SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY);
1519
};
1620

21+
1722
export default supabase_server;

src/headless-ais/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import courses from "./courses";
55
import inthu from "./inthu";
66
import eeclass from "./eeclass";
77
import grades from "./grades";
8+
import type { ScheduledController, Scheduler } from "@cloudflare/workers-types";
9+
import { scrapeArchivedCourses } from '../scheduled/syllabus';
810

911
const app = new Hono()
1012
.route("/auth", auth)
@@ -13,4 +15,4 @@ const app = new Hono()
1315
.route("/eeclass", eeclass)
1416
.route("/grades", grades);
1517

16-
export default app;
18+
export default app;

src/index.ts

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import shortlink from "./shortlink";
1111
import issue from "./issue";
1212
import headlessAis from "./headless-ais";
1313
import planner from "./planner-replication";
14-
import type { D1Database } from "@cloudflare/workers-types";
14+
import { D1Database } from '@cloudflare/workers-types';
15+
import { scrapeArchivedCourses, scrapeSyllabus, syncCoursesToAlgolia } from './scheduled/syllabus';
1516

1617
export type Bindings = {
1718
DB: D1Database;
@@ -38,4 +39,28 @@ export const app = new Hono<{ Bindings: Bindings }>()
3839
.route("/issue", issue)
3940
.route("/planner", planner);
4041

41-
export default app;
42+
43+
const APIHandler = {
44+
...app,
45+
async scheduled(
46+
controller: ScheduledController,
47+
env: Env,
48+
ctx: ExecutionContext,
49+
) {
50+
console.log("cron processed");
51+
ctx.waitUntil(new Promise(async (resolve) => {
52+
const semester = '11320';
53+
// Scrape archived courses and syllabus
54+
try {
55+
const cache = await scrapeArchivedCourses(env, semester);
56+
await scrapeSyllabus(env, semester, cache);
57+
resolve(void 0);
58+
} catch (error) {
59+
console.error("Error during scheduled task:", error);
60+
}
61+
}))
62+
},
63+
64+
};
65+
66+
export default APIHandler;

0 commit comments

Comments
 (0)