API v2 get audio files for review

hej.

so I wanted to write a little script for myself so I can listen to all my vocab reviews, so I looked in to the API v2 (apparently v1 doesnt give out the audio files).

My script works so far, but it is really heavy, as it hast to call a specific ID for each review.
Is there an easier/network friendlier option to get to the audio files?

<?php

$headerTag = "Wanikani-Revision: 20170710";
$authorization = "Authorization: Bearer AUTH TOKEN";

function getVocabularyData($ch, $id, $authorization, $i) {
    
    $url = "https://api.wanikani.com/v2/subjects/" . $id . "/";
    //curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $headerTag ));
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization ));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
    $result = curl_exec($ch);
    
    $vocabulary = json_decode($result, TRUE);

    if($vocabulary['object'] == "vocabulary"){
        echo "ID : " . $vocabulary['id'] . "<br>";
        echo "Meaning : " . $vocabulary['data']['meanings'][0]['meaning']. "<br>";
        echo "<audio src=" . $vocabulary['data']['pronunciation_audios'][0]['url'] . " controls ></audio>";
        $i = $i +1;
    }else{
        echo "not a vocabulary" . $vocabulary['object'] . "<br>";
    }
    echo "<p>";
    
    return $i;
}

$ch = curl_init();
    
$url = "https://api.wanikani.com/v2/assignments?subject_types=vocabulary";
//$url = "https://api.wanikani.com/v2/reviews";
 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $authorization ));
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
$result = curl_exec($ch);
$decoded = json_decode($result, TRUE);

$i = 0;
foreach ($decoded['data'] as $key => $value) { 
    
    if($i<10){
        echo $value['data']['subject_id'] . "<br>";
        $i = getVocabularyData($ch, $value['data']['subject_id'], $authorization, $i);
    }
}
 
var_dump($decoded); 
 
curl_close($ch); 

?>

Thanks,
Phil

1 Like

You can use the Wanikani Open Framework, which caches item data, then just fetch the audio URLs from there.

wkof.include('ItemData');
wkof.ready('ItemData').then(get_items);

function get_items() {
    config = {...};
    wkof.ItemData.get_items(config).then(do_stuff);
}

function do_stuff(items) {
    // Do stuff
}
1 Like

I was wondering about this, but from my understanding I would need to build a greasemonkey script, rather my own standalone webpage. correct?
or how would I include that into my standalone php project?

Oh, I don’t know anything about creating websites I’m just an amateur at programming

I am sure that @rfindley could help you, though

Is it not possible to simply reference the script(s) you need in a script tag and use it directly (ie. bake the reference in without needing tampermonkey to inject it in)?

Maybe… if Open Framework doesn’t make assumptions about what domain it’s running on (I can’t remember).

But I am running a very-slightly-modified version of Open Framework on wkstats.com/v2. No userscript needed.