|
| 1 | +import { defaultSlugify } from 'utils/content' |
| 2 | +import { CreateBlockie } from 'utils/account' |
| 3 | +import { PRETALX_CONFIG } from 'utils/config' |
| 4 | +import dayjs from 'dayjs' |
| 5 | +import utc from 'dayjs/plugin/utc' |
| 6 | + |
| 7 | +dayjs.extend(utc) |
| 8 | + |
| 9 | +const cache = new Map() |
| 10 | + |
| 11 | +export async function GetRooms() { |
| 12 | + const rooms = await exhaustResource(`/events/${PRETALX_CONFIG.PRETALX_EVENT_NAME}/rooms`) |
| 13 | + return rooms.map((i: any) => { |
| 14 | + return { |
| 15 | + id: `${PRETALX_CONFIG.PRETALX_EVENT_NAME}_${i.name?.en ? defaultSlugify(i.name?.en) : String(i.id)}`, |
| 16 | + name: i.name?.en ?? '', |
| 17 | + description: i.description?.en ?? '', |
| 18 | + info: i.speaker_info?.en ?? '', |
| 19 | + capacity: i.capacity, |
| 20 | + } |
| 21 | + }) |
| 22 | +} |
| 23 | + |
| 24 | +export async function GetSpeakers() { |
| 25 | + const speakersData = await exhaustResource(`/events/${PRETALX_CONFIG.PRETALX_EVENT_NAME}/speakers`) |
| 26 | + const speakers = speakersData.map((i: any) => { |
| 27 | + const twitter = i.answers?.find((i: any) => i.question.id === PRETALX_CONFIG.PRETALX_QUESTIONS_TWITTER)?.answer |
| 28 | + const github = i.answers?.find((i: any) => i.question.id === PRETALX_CONFIG.PRETALX_QUESTIONS_GITHUB)?.answer |
| 29 | + |
| 30 | + let speaker: any = { |
| 31 | + id: defaultSlugify(i.name), |
| 32 | + sourceId: i.code, |
| 33 | + name: i.name, |
| 34 | + avatar: i.avatar ?? CreateBlockie(i.name), |
| 35 | + description: i.biography ?? '', |
| 36 | + } |
| 37 | + |
| 38 | + if (twitter) speaker.twitter = twitter |
| 39 | + if (github) speaker.github = github |
| 40 | + |
| 41 | + return speaker |
| 42 | + }) |
| 43 | + |
| 44 | + return speakers |
| 45 | +} |
| 46 | + |
| 47 | +export async function GetSessions() { |
| 48 | + const talks = await exhaustResource(`/events/${PRETALX_CONFIG.PRETALX_EVENT_NAME}/talks`) |
| 49 | + |
| 50 | + const sessions = talks.map((i: any) => { |
| 51 | + const expertise = i.answers?.find((i: any) => i.question.id === PRETALX_CONFIG.PRETALX_QUESTIONS_EXPERTISE)?.answer as string |
| 52 | + const tagsAnswer = i.answers?.find((i: any) => i.question.id === PRETALX_CONFIG.PRETALX_QUESTIONS_TAGS)?.answer as string |
| 53 | + |
| 54 | + return { |
| 55 | + id: `${PRETALX_CONFIG.PRETALX_EVENT_NAME}_${defaultSlugify(i.title)}`, |
| 56 | + sourceId: i.code, |
| 57 | + title: i.title, |
| 58 | + description: i.description ?? i.abstract, |
| 59 | + track: i.track?.en ?? '', |
| 60 | + type: i.submission_type?.en ?? '', |
| 61 | + expertise: expertise ?? '', |
| 62 | + tags: tagsAnswer |
| 63 | + ? tagsAnswer.includes(',') |
| 64 | + ? tagsAnswer.split(',').map((i) => i.replace(/['"]+/g, '').trim()) |
| 65 | + : tagsAnswer.split(' ').map((i) => i.replace(/['"]+/g, '').trim()) |
| 66 | + : [], |
| 67 | + language: 'en', |
| 68 | + speakers: i.speakers.map((i: any) => i.code), |
| 69 | + slot_start: dayjs.utc(i.slot.start).toDate(), |
| 70 | + slot_end: dayjs.utc(i.slot.end).toDate(), |
| 71 | + slot_roomId: i.slot?.room ? `${PRETALX_CONFIG.PRETALX_EVENT_NAME}_${defaultSlugify(i.slot.room.en)}` : null, |
| 72 | + } |
| 73 | + }) |
| 74 | + |
| 75 | + return sessions |
| 76 | +} |
| 77 | + |
| 78 | +async function exhaustResource(slug: string, limit = PRETALX_CONFIG.DEFAULT_LIMIT, offset = 0, results = [] as any): Promise<any> { |
| 79 | + return get(`${slug}${slug.includes('?') ? '&' : '?'}limit=${limit}&offset=${offset}`).then((data: any) => { |
| 80 | + results.push(data.results) |
| 81 | + if (data.next) { |
| 82 | + console.log('GET', slug, 'TOTAL COUNT', data.count) |
| 83 | + return exhaustResource(slug, limit, offset + limit, results) |
| 84 | + } else { |
| 85 | + console.log('Return results', slug, results.flat().length) |
| 86 | + return results.flat() |
| 87 | + } |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +async function get(slug: string) { |
| 92 | + if (cache.has(slug)) { |
| 93 | + return cache.get(slug) |
| 94 | + } |
| 95 | + |
| 96 | + const response = await fetch(`${PRETALX_CONFIG.PRETALX_BASE_URI}${slug}`, { |
| 97 | + headers: { |
| 98 | + Authorization: `Token ${PRETALX_CONFIG.PRETALX_API_KEY}`, |
| 99 | + }, |
| 100 | + }) |
| 101 | + |
| 102 | + const data = await response.json() |
| 103 | + cache.set(slug, data) |
| 104 | + return data |
| 105 | +} |
0 commit comments