Skip to content

Instantly share code, notes, and snippets.

@cherryblossom000
Last active December 15, 2022 08:31
Show Gist options
  • Save cherryblossom000/bb7aff8d8ad84c831ed58ef7673db03b to your computer and use it in GitHub Desktop.
Save cherryblossom000/bb7aff8d8ad84c831ed58ef7673db03b to your computer and use it in GitHub Desktop.
Get grades from Canvas — Elvish version
#!/usr/bin/env elvish
var result = (
xhs ^
--check-status --ignore-stdin --timeout 2.5 ^
example.instructure.com/api/graphql ^
Authorization:'Bearer '$E:CANVAS_TOKEN ^
[email protected] |
from-json
)
if (has-key $result errors) {
fail $result[errors]
}
var data = $result[data]
var subjects = [(keys $data)]
# Alternatively specify the subjects in the order that you want
# them to be in in the output
# var subjects = [Subject1 Subject2]
each {|subject|
var score = $data[$subject][grades][currentScore]
put $subject': '(
if (eq $score $nil) {
put '--'
} else {
printf '%6.2f%%' $score
}
)
} $subjects |
to-lines |
column -t
{
Subject1: node(id: "<enrollmentId1>") { ...grade }
Subject2: node(id: "<enrollmentId2>") { ...grade }
# ...
}
fragment grade on Node {
...on Enrollment {
grades { currentScore }
}
}
$ ./grades
English:  12.34%
Maths:    56.78%
Science:  90.12%
History:  --

Same as grades.sh but written in Elvish.

Requirements

Tested on Elvish v0.17.0 and xh v0.10.0–v0.14.1.

Setup

1. Variables

  • Get an API token
    • https://example.instructure.com/profile/settings → ‘Approved Integrations’ → ‘New Access Token’
var host = example.instructure.com # replace with actual host
set-env CANVAS_TOKEN '<your API access token>'

2. Enrollment Ids

Replace <courseId1>, <courseId2> etc. with the ids of the courses that you want to get the grades of. The id of the course can be found by looking at the course’s URL: https://example.instructure.com/courses/courseId.

xhs $host/api/graphql Authorization:Bearer' '$E:CANVAS_TOKEN query='{
  legacyNode(_id: "'(
    xhs $host/api/v1/users/self Authorization:Bearer' '$E:CANVAS_TOKEN |
    from-json |
    exact-num (one)[id]
  )'", type: User) {
    ...on User {
      Subject1: enrollments(courseId: "<courseId1>") { id }
      Subject2: enrollments(courseId: "<courseId2>") { id }
    }
  }
}' |
  from-json |
  put (one)[data][legacyNode]

The result will be something like this:

▶ [&Subject1=[[&id=<enrollmentId1>]] &Subject2=[[&id=<enrollmentId2>]]]

Use these ids (<enrollmentId1>, <enrollmentId2>, etc.) in query.graphql.

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