Automatically open WaniKani when it's review time

For Python instructions, see post 8 below.
To disable/re-enable this easily, see post 13 below.

Hey all,

This is something I made for myself that I find very useful for me. It’s a small script that runs every hour and checks if there’s a new WaniKani review session available. If there is, it will open up WaniKani for me, or else it will just let me know how many hours are left until review time and disappear.

The major benefit for this method is that you are taken to the reviews as soon as they are ready. This means that you won’t have to check for reviews on your own, and it’s great for people like me who want to go through WaniKani as quickly as possible and stay on top of their Japanese studies.

These instructions are written for my computer environment, which is Windows 10 and Google Chrome and with PHP software. If any of you work with Mac OS/Firefox/Python/etc and want to convert these commands over and share them here, please do :slight_smile:.

1) Install PHP

If you already have a web server installed and you know how to run PHP from the command line, then you can skip this step.

Go here, http://php.net/downloads.php, and click “Windows downloads” under the most recent Current Stable PHP version. And then click on the “Zip” download link under either “VC15 x64 Thread Safe” or “VC15 x86 Thread Safe”, depending on your operating system. If you don’t know, it’s probably “x64”, and if x64 doesn’t run, then try x86.

When the Zip file is downloaded, open it, and extra it to C:\php. You may have to create this folder first if it doesn’t exist. After you extract it, you should end up with a file located at C:\php\php.exe.

To test if this worked correctly, open a Command Prompt and type the command C:\php\php.exe -?. If it comes back with an error message, go back and try again.

2) Create the code file

Open up Notepad or your favorite code editor. Copy and paste the code section below, and modify the API key and your timezone as necessary. Your API key is found at https://www.wanikani.com/api. The available timezone values are found at http://php.net/manual/en/timezones.php.

Save this file somewhere where you can find it. I recommend putting it in your user folder, which is located at C:\Users(your username), but placing it on your desktop or in your root directory will work, too. For my example, I placed it in C:\Users\Ben\wanikani.txt.

To test that it works, open up a command prompt and run the script from php. This is the command to run it on my computer:

C:\php\php.exe -dextension=openssl C:\Users\Ben\wanikani.txt

If everything works good so far, you should see a window like this (or WaniKani will open if it’s review time).

The code file:

<?php

$api_key = "YOUR_API_KEY";
$timezone = "America/New_York";

date_default_timezone_set($timezone);
$api_call = json_decode(file_get_contents("https://www.wanikani.com/api/user/$api_key/study-queue"), true);
$seconds_til_review = $api_call["requested_information"]["next_review_date"] - time();
$hours_til_review = round($seconds_til_review / 60 / 60, 2);

if ($hours_til_review < 0.15) {
    print shell_exec('SCHTASKS /F /Create /TN PHP_OPEN /TR "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --new-window https://www.wanikani.com/review\'" /SC DAILY /RU INTERACTIVE') . "<br />\n";
    print shell_exec('SCHTASKS /RUN /TN PHP_OPEN') . "<br />\n";
    print shell_exec('SCHTASKS /DELETE /TN PHP_OPEN /F') . "<br />\n";
}
else {
    print "Hours til review: " . $hours_til_review;
    sleep(10);
}

3) Create a scheduled task

Open up Task Scheduler. The quick way to do this is to hit the Start Button and type “task scheduler”.

Inside Task Scheduler, on the right, click “Create Task…”.

Under the General tab, enter WaniKani in the Name field. Also, check the “Run with highest privileges” checkbox.

Under the Triggers tab, click “New…”. Set the Start time to the next hour plus 5 seconds or us (for example 4:00:05 PM). The five seconds gives room for time differences between your computer and WaniKani’s servers. If you find that the WaniKani window opens but the reviews aren’t actually ready, then change the 5 seconds to 15 seconds or more. Also, check off the “Repeat task every” checkbox, and change the “for a duration of” field to “Indefinitely”. Click OK to close the “New Trigger” window.

Under the Actions tab, click “New…”. For “Program/script”, type C:\php\php.exe, and for “Add arguments”, type -dextension=openssl C:\Users\Ben\wanikani.txt. Modify these two textboxes as necessary to fit where you installed PHP and where you saved the code file. Click OK to close the “New Action” window.

Click OK to create the task. To check if it worked, click on “Task Scheduler Library” on the left, and you should see your “Wanikani” task. If you don’t see it, go back from the top of step 3 and try again. Right-click on the task and click Run, and you should get a little Command Prompt window that stays open for 10 seconds, or a WaniKani window.

4) 日本語を勉強する!

Thank you for checking this out. If it helps, let me know :slight_smile:

11 Likes

I would love to get this to work! But I’m having problems. I get this error:

Could anyone please offer advice? Thanks in advance!

1 Like

Hi @MereBear82!

Sure. It looks to me that you left some extra characters in your API request URL. You need to remove those from the code to get it to work.

Change your top code line from

$api_key = "{{abc1234etc}}";

to

$api_key = "abc1234etc";

Actually, I see now how my original code was misleading, because I left some {{ and }} characters in the code file. My bad! I just updated it.

1 Like

Thanks very much!

1 Like

Totally invasive and interrupting - I absolutely love this idea :smile:

16 Likes

y-you’re totally invasive and interrupting!

4 Likes

Super hoping to see a python version of this soon. :slight_smile:

1 Like

Here you go :slight_smile:

from urllib.request import urlopen
import json
import time
import webbrowser

api_key = "YOUR_API_KEY"

data = json.load(urlopen("http://www.wanikani.com/api/user/%s/study-queue" % api_key))
seconds_til_review = data["requested_information"]["next_review_date"]-int(time.time())
hours_til_review = round(seconds_til_review/60/60, 2)

if hours_til_review < 0.15:
  webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe --new-window %s").open("https://www.wanikani.com/review")
else:
  print("Hours til review: %s" % hours_til_review)
  time.sleep(10)

And here’s some instructions.

1) Install Python 3

Download it from here: https://www.python.org/downloads/

2) Create the code file

Open up Notepad or your favorite code editor. Copy and paste that code section above. Replace the “YOUR_API_KEY” text with your API key from here.

Save this file somewhere where you can find it, and make sure to name it with a .py file extension. If you’re using notepad, you’ll need to change the “Save as type” to “All Files (*.*)” like in this image.

image

To test if it works, just double-click on the .py file.

3) Create a scheduled task

Follow the step 3 from the original post above, except when instead of typing C:\php\php.exe for the “Program/script”, just browse to your .py file. And also, do not type anything for “Add arguments”.

4) Burn some turtles

:wink:

3 Likes

Swoon Thank you so much! I can’t wait to set this up! Thank you! Thank you! Thank you!

1 Like

Best of luck! et me know if you have any ideas for improvement or other small apps :slight_smile: Hope to see you around!

1 Like

Would this work better with Python 2 or 3?

I’m not sure, but go ahead and use Python 3. I’ll update my instructions, thank you.

1 Like

Update – I started playing Elder Scrolls IV Oblivion again, and found how annoying it can be when the review notice comes up when you’re doing something important. So here’s a quick way to enable/disable the notice.

1) Create a batch file

This batch will toggle the active status of the scheduled task, so that it will either disable or re-enable the scheduled task.

Open up Notepad and paste this code section in. Change the line near the top from set taskName=WaniKani to suit how you named your scheduled task. Make sure to save the file as a .bat file with the “Save as type” as “All files (*.*)” like demonstrated in step 2 in this post above, and also make sure to save it to a place where you won’t lose it.

ToggleWK.bat
@echo off
set taskName=WaniKani
set uniqueFileName=%tmp%\bat~%RANDOM%%RANDOM%%RANDOM%.tmp
schtasks /Query /TN "%taskName%" /FO list | find "Status:" | find "Ready" /c>%uniqueFileName%
set /p result=<%uniqueFileName%
del /f %uniqueFileName%
if %result% EQU 1 (
    schtasks /change /disable /TN "%taskName%"
    echo The task was disabled.
) else (
    schtasks /change /enable /TN "%taskName%"
    echo The task was enabled.
)
pause

2) Create a shortcut on your desktop

The batch file needs to be run as administrator to be able to change tasks. One way to do this is to create a shortcut and make the shortcut be run as administrator always, like so:

Right-click your batch file and click on Create Shortcut. This will create a shortcut in the folder where the batch file is. If you have a lot of files in the folder and don’t see the shortcut, scroll down to the bottom, or close the window and go back in to the folder.

CreateShortcut

Right-click on the shortcut and click Properties.

Go to the Shortcut tab and click on Advanced…

Advanced

Make sure that the checkbox next to Run as administrator is checked off.

Run%20as%20admin

Hit OK. Optionally, change the icon by hitting Change Icon… When you’re done, hit OK.

Move the shortcut to your desktop, and rename it to something nice.

image

3) Go do something important, disruption-free, like your reviews

Well there you go. Now any time you need to be disruption-free for a while, double-click this shortcut. Just remember to reactivate it when you’re done!