Skip to content

Instantly share code, notes, and snippets.

@xentec
Last active January 8, 2025 09:46
Show Gist options
  • Save xentec/5a7392a0c5b744c81a28ffd269931359 to your computer and use it in GitHub Desktop.
Save xentec/5a7392a0c5b744c81a28ffd269931359 to your computer and use it in GitHub Desktop.
CLI mensa plan for OTH Regensburg. Requires Nushell to run.
#!/usr/bin/env nu
def fetch [week: int] {
let url = $"https://www.stwno.de/infomax/daten-extern/csv/HS-R-tag/($week).csv"
(
http get --raw $url
| decode "iso-8859-1"
| str replace --regex "\n(\n|(;|\\())" "$2" # fix bullshit
| from csv -s ";"
)
}
# Show compact canteen food plan
def main [
selection?: string, # Filter by food type (vegan, etc.)
--details, # Include allergene details
--week: int, # Specify specific week to display
] {
let now = (date now)
let w = if $week == null { $now | format date '%V' | str trim --left --char '0' | into int } else { $week }
(
fetch $w
| reject tag preis
| if $week == null {
where datum == ($now | format date "%d.%m.%Y")
| reject datum
} else { $in }
| if not $details {
update name {str replace --regex '([^\(]+)\(.+\)' '$1' | str trim}
} else { $in }
| if $selection != null {
update kennz {split row ','}
| filter {|r| $selection in $r.kennz }
| update kennz {str join ','}
} else { $in }
)
}
# Fetch all years worth of data as sanitized CSV
def "main all" [] {
let year_ago = (date now) - 52wk
(
1..=52
| par-each {|w| fetch $w | upsert kw $w}
| flatten --all
| reject tag preis
| update datum {into datetime}
| where datum > $year_ago # fix leftover data
| move kw --after datum
| update stud {str replace ',' '.' | into float}
| update bed {str replace ',' '.' | into float}
| update gast {str replace ',' '.' | into float}
| rename --column {stud: preis-stud, bed: preis-bedienstet, gast: preis-gast}
| sort-by kw
| update datum {format date "%Y-%m-%d"}
| to csv
)
}
@DestinyofYeet
Copy link

Expanded it a little bit into a function. Now you can filter by the 'kennz' row (if it is vegan, etc)

def get-mensa-plan [ selection?: string] {
  let now = (date now)
  let week = ($now | format date '%V' | str trim -l -c '0')
  let url = $"https://www.stwno.de/infomax/daten-extern/csv/HS-R-tag/($week).csv"

  (
    http get --raw $url |
      decode "iso-8859-1" |
      str replace -a "\n(\n|(;|\\())" "$2" |  # fix bullshit
      from csv -s ";" |
      where datum == ($now | format date "%d.%m.%Y") |
      update name {|row| $row.name | str replace '([^\(]+)\(.+\)' '$1' } |
      select warengruppe name kennz stud bed
  )
    | if $selection != null  { $in | where kennz == $selection } else { $in }
}

@xentec
Copy link
Author

xentec commented Nov 13, 2024

Updated. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment