How to pull number of apprentice items from API?

I have tried to pull the number of apprentice items from the WK API but the numbers never seem to match what my dashboard says on the WK website or Tsurukame on iOS. Those appear to correct yet my figure is much less (54 vs 37).

What I am doing is accessing assignments.srs_stage (‘https://api.wanikani.com/v2/assignments/’) and counting the number of srs_stage entries with numbers from 1 to 4 (0 = lessons, 1-4 = apprentice ‘as far as I know’).

Has something changed recently to throw this way of access off? I checked the code in the lesson cap userscript and that appears to use the same method and also brings back the wrong results.

Any advice on getting accurate apprentice level numbers would be greatly appreciated.

By default, the Open Framework filters out items that WK has hidden (i.e. removed from being taught). Maybe that explains the difference? If you are using wkof.ItemData.get_items(), try passing the option to include_hidden, and see if that makes a difference.

wkof.ItemData.get_items({
  wk_items: {
    options: {
      subjects: true,
      assignments: true,
      include_hidden: true
    }
  }
})
.then(function(items) {
  // Count your apprentice items here
});

Ahh, I am trying to access via either Excel or Apple Shortcuts so not sure how to pass the parameter, but I’ll give it a go.

If not I’ll have to research how to do it in Python which I have basic knowledge in.

In that case, include_hidden isn’t applicable. I’ll ponder if there’s some other factor in the API…

Could it be to do with assignments having a 500 page limit?

image

I wonder if that’s it…

pretty sure, that’s the problem

in Rust I used something like this:

let mut next_page = Some(String::from("https://api.wanikani.com/v2/subjects"));

while next_page.is_some() {
    ...
    next_page = subjects.pages.next_url; 
}

Yeah… Normally you’d fetch each page (of 500) and combine the results.

Sorry, I’m very new to this, it’s the first API I’ve ever used.

Currently trying to find out how to do this in Excel/Apple Shortcuts as I can only see the next url and not a list of them.

Congrats on venturing down the API rabbit hole :slight_smile:

And doing so in Excel/Apple shortcuts… That would be interesting. I’ve not done much external linking in Excel.

It’s amazing how far spreadsheet software has come, especially considering that spreadsheets were one of the earliest general applications in the history of computing. To still retain so much of their original form is nothing short of mind-boggling.

1 Like

Thank you :slight_smile: it’s a bit scary as a complete beginner but I’m sure I can create some great productivity scripts if I succeed.

Right, I’ve managed to do it in Excel Power Query!

I’ll have to try later on today to make it into an Apple Shortcut but it will just be a case of converting the syntax. If I can’t do that I’m sure I can make a Python script to run from a Shorcut.

For anyone looking to get this information and tackle API pagination through power query, please see below code:

let

    // you can change the url here based on what you want to recieve, see api docs here: https://docs.api.wanikani.com/20170710/
    api_url = "https://api.wanikani.com/v2/assignments/",

    // replace KEY with your api token from this url: https://www.wanikani.com/settings/personal_access_tokens
    api_key = "KEY",

    headers = [Headers=[Authorization="Bearer " & api_key]],

    // first page url and data
    initReq = Json.Document(Web.Contents(api_url , headers)),
    initData = initReq[data],

    // gather and cycle through pages after page 1
    gather = (data as list, url) =>
        let
            newUrl = Json.Document(Web.Contents(url, headers))[pages][next_url],
            newReq = Json.Document(Web.Contents(newUrl, headers)),
            newdata = newReq[data],
            data = List.Combine({data, newdata}),
            check = if newReq[pages][next_url] = null then data else @gather(data, newUrl)
        in check,

    // group all data from pages together
    outputList = gather(initData, api_url),

    // expand all data
    #"expand data" = Table.FromRecords(outputList),
    #"remove other columns" = Table.SelectColumns(#"expand data",{"data"}),

    // filter to srs and apprentice items only
    #"expand srs_stage" = Table.ExpandRecordColumn(#"remove other columns", "data", {"srs_stage"}, {"srs_stage"}),
    #"filter to apprentice items only" = Table.SelectRows(#"expand srs_stage", each [srs_stage] >= 1 and [srs_stage] <= 4),

    // count apprentice items and return as value
    #"count apprentice items" = Table.RowCount(#"filter to apprentice items only")
in
    #"count apprentice items"
1 Like

Yeah this seems similar to what I ended up doing, I had to create some kind of loop to keep finding the next page url, storing the data as a list and expand all.

It’s the first time I’ve tried something like that but it seems to work perfectly now.

I might try and recreate it in Python as a learning exercise at some point.

I’ve had a play around and managed to create this script in Apple Shortcuts. It isn’t the most elegant of solutions but I’m very happy with the result.

If anyone else would like to use this Shortcut as is or as a template for their own purposes feel free to use the below link:
https://www.icloud.com/shortcuts/84f3f0bb550c40f8a3d39fc4a1712f12

My use of this will be to have an app icon I press on my home screen, it will check if I have any of the below to do and start them in priority list:

  • WaniKani Reviews (If non, next line)
  • BunPro Reviews (If non, next line)
  • WaniKani Lessons (Below apprentice limit - If non, next line)
  • Ask to Read or Watch
    • Read opens Satori Reader
    • Watch opens Comprehensible Japanese playlist

I like the idea of having 1 button to find what I should be doing instead of checking what options I have available in separate apps.