|
| 1 | +import "server-only" |
| 2 | +import { stripHtml } from "string-strip-html" |
| 3 | +import { SchedSpeaker, ScheduleSession } from "@/app/conf/2023/types" |
| 4 | +import pLimit from "p-limit" |
| 5 | + |
| 6 | +async function fetchData<T>(url: string): Promise<T> { |
| 7 | + try { |
| 8 | + const response = await fetch(url, { |
| 9 | + method: "POST", |
| 10 | + headers: { |
| 11 | + "Content-Type": "application/json", |
| 12 | + "User-Agent": "GraphQL Conf / GraphQL Foundation", |
| 13 | + }, |
| 14 | + }) |
| 15 | + const data = await response.json() |
| 16 | + return data |
| 17 | + } catch (error) { |
| 18 | + throw new Error( |
| 19 | + `Error fetching data from ${url}: ${(error as Error).message || (error as Error).toString()}`, |
| 20 | + ) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +const token = process.env.SCHED_ACCESS_TOKEN_2024 |
| 25 | + |
| 26 | +async function getUsernames(): Promise<string[]> { |
| 27 | + const response = await fetchData<{ username: string }[]>( |
| 28 | + `https://graphqlconf2024.sched.com/api/user/list?api_key=${token}&format=json&fields=username`, |
| 29 | + ) |
| 30 | + return response.map(user => user.username) |
| 31 | +} |
| 32 | + |
| 33 | +const limit = pLimit(40) // rate limit is 30req/min |
| 34 | + |
| 35 | +async function getSpeakers(): Promise<SchedSpeaker[]> { |
| 36 | + const usernames = await getUsernames() |
| 37 | + |
| 38 | + const users = await Promise.all( |
| 39 | + usernames.map(username => |
| 40 | + limit(() => { |
| 41 | + return fetchData<SchedSpeaker>( |
| 42 | + `https://graphqlconf2024.sched.com/api/user/get?api_key=${token}&by=username&term=${username}&format=json&fields=username,company,position,name,about,location,url,avatar,role,socialurls`, |
| 43 | + ) |
| 44 | + }), |
| 45 | + ), |
| 46 | + ) |
| 47 | + |
| 48 | + const result = users |
| 49 | + .filter(speaker => speaker.role.includes("speaker")) |
| 50 | + .map(user => { |
| 51 | + return { |
| 52 | + ...user, |
| 53 | + about: stripHtml(user.about).result, |
| 54 | + } |
| 55 | + }) |
| 56 | + |
| 57 | + return result |
| 58 | +} |
| 59 | + |
| 60 | +async function getSchedule(): Promise<ScheduleSession[]> { |
| 61 | + const sessions = await fetchData<ScheduleSession[]>( |
| 62 | + `https://graphqlconf2024.sched.com/api/session/export?api_key=${token}&format=json`, |
| 63 | + ) |
| 64 | + |
| 65 | + const result = sessions.map(session => { |
| 66 | + const { description } = session |
| 67 | + if (description?.includes("<")) { |
| 68 | + // console.log(`Found HTML element in about field for session "${session.name}"`) |
| 69 | + } |
| 70 | + |
| 71 | + return { |
| 72 | + ...session, |
| 73 | + description: description && stripHtml(description).result, |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + return result |
| 78 | +} |
| 79 | + |
| 80 | +// @ts-expect-error -- fixme |
| 81 | +export const speakers = await getSpeakers() |
| 82 | + |
| 83 | +// @ts-expect-error -- fixme |
| 84 | +export const schedule = await getSchedule() |
0 commit comments