Skip to content

Commit

Permalink
mensa and index: use the reimplemented Mensa API which now uses the s…
Browse files Browse the repository at this point in the history
…ame json format as the THI API
  • Loading branch information
M4GNV5 committed Jan 9, 2021
1 parent e5178f7 commit 9d518a4
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 12 deletions.
30 changes: 30 additions & 0 deletions rogue-thi-app/lib/reimplemented-api-client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import MemoryCache from './memory-cache'
import LocalStorageCache from './localstorage-cache'

const CACHE_NAMESPACE = 'reimplemented-api-client'
const CACHE_TTL = 10 * 60 * 1000

const KEY_GET_MENSA_PLAN = 'getMensaPlan'

let cache
if (typeof localStorage === 'undefined') {
cache = new MemoryCache({
ttl: CACHE_TTL
})
} else {
cache = new LocalStorageCache({
namespace: CACHE_NAMESPACE,
ttl: CACHE_TTL
})
}

export async function getMensaPlan () {
let res = cache.get(KEY_GET_MENSA_PLAN)
if (!res) {
res = await fetch('/api/mensa').then(res => res.json())
}

cache.set(KEY_GET_MENSA_PLAN, res)

return res.data
}
68 changes: 68 additions & 0 deletions rogue-thi-app/pages/api/mensa.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,73 @@ const URL_EN = 'https://www.max-manager.de/daten-extern/sw-erlangen-nuernberg/xm

const cache = new MemoryCache({ ttl: CACHE_TTL })

const dayTexts = [
'Sonntag',
'Montag',
'Dienstag',
'Mittwoch',
'Donnerstag',
'Freitag',
'Samstag',
]
function pad2(val) {
return val.toString().padStart(2, '0')
}
function dateToTHIFormat(date) {
return `${pad2(date.getDate())}.${pad2(date.getMonth() + 1)}.${pad2(date.getFullYear())}`;
}
function convertToTHIFormat (sourceData) {
let sourceDays = sourceData.speiseplan.tag
if(!Array.isArray(sourceDays))
sourceDays = [sourceDays]

const days = sourceData.speiseplan.tag.map(day => {
const date = new Date(day._attributes.timestamp * 1000)

let sourceItems = day.item
if(!Array.isArray(sourceItems))
sourceItems = [sourceItems]

const items = {}
const addInReg = /\s*\((.*?)\)\s*/
sourceItems.forEach((item, i) => {
let text = item.title._text
let addIns = new Set()
while(addInReg.test(text)) {
const [addInText, addIn] = text.match(addInReg)
text = text.replace(addInText, ' ')

const newAddins = addIn.split(',')
newAddins.forEach(newAddin => addIns.add(newAddin))
}

items[i] = {
zusatz: [...addIns].join(','),
name: [
'',
text.trim(),
item.preis1._text,
item.preis2._text,
item.preis3._text,
]
}
})

return {
tag: `${dayTexts[date.getDay()]} ${dateToTHIFormat(date)}`,
gerichte: items,
}
})

const now = new Date()
return {
data: days,
status: 0,
date: dateToTHIFormat(now),
time: `${pad2(now.getHours())}:${pad2(now.getMinutes())}:${pad2(now.getSeconds())}`,
}
}

async function fetchPlan (lang) {
const url = (lang || 'de') === 'de' ? URL_DE : URL_EN

Expand All @@ -15,6 +82,7 @@ async function fetchPlan (lang) {
if (!plan) {
const resp = await fetch(url)
plan = xmljs.xml2js(await resp.text(), { compact: true })
plan = convertToTHIFormat(plan)

cache.set(url, plan)
}
Expand Down
17 changes: 9 additions & 8 deletions rogue-thi-app/pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ import styles from '../styles/Home.module.css'
import AppNavbar from '../lib/AppNavbar'
import InstallPrompt from '../lib/InstallPrompt'
import { obtainSession, forgetSession } from '../lib/thi-session-handler'
import { getTimetable, getMensaPlan, getPersonalData } from '../lib/thi-api-client'
import { getTimetable, getPersonalData } from '../lib/thi-api-client'
import { getMensaPlan } from '../lib/reimplemented-api-client'
import { formatNearDate, formatFriendlyTime } from '../lib/date-utils'

const IMPRINT_URL = process.env.NEXT_PUBLIC_IMPRINT_URL
Expand All @@ -57,7 +58,7 @@ async function getTimetablePreview (session) {
}

async function getMensaPlanPreview (session) {
const days = await getMensaPlan(session)
const days = await getMensaPlan()

return Object.values(days[0].gerichte)
.map(x => x.name[1])
Expand Down Expand Up @@ -109,20 +110,20 @@ export default function Home () {
const router = useRouter()

useEffect(async () => {
const session = await obtainSession(router)

try {
setTimetable(await getTimetablePreview(session))
setMensaPlan(await getMensaPlanPreview())
} catch (e) {
console.error(e)
setTimetableError(e)
setMensaPlanError(e)
}

const session = await obtainSession(router)

try {
setMensaPlan(await getMensaPlanPreview(session))
setTimetable(await getTimetablePreview(session))
} catch (e) {
console.error(e)
setMensaPlanError(e)
setTimetableError(e)
}
}, [])

Expand Down
6 changes: 2 additions & 4 deletions rogue-thi-app/pages/mensa.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import { faExclamationTriangle } from '@fortawesome/free-solid-svg-icons'
import styles from '../styles/Timetable.module.css'

import AppNavbar from '../lib/AppNavbar'
import { obtainSession } from '../lib/thi-session-handler'
import { getMensaPlan } from '../lib/thi-api-client'
import { getMensaPlan } from '../lib/reimplemented-api-client'
import { formatNearDate } from '../lib/date-utils'

import allergenMap from '../data/allergens.json'
Expand Down Expand Up @@ -45,8 +44,7 @@ export default function Timetable () {

useEffect(async () => {
try {
const session = await obtainSession(router)
const data = await getMensaPlan(session)
const data = await getMensaPlan()

const days = data.map(x => ({
date: parseGermanDate(x.tag),
Expand Down

1 comment on commit 9d518a4

@M4GNV5
Copy link
Member Author

@M4GNV5 M4GNV5 commented on 9d518a4 Jan 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ref #7

Please sign in to comment.