Skip to content

Instantly share code, notes, and snippets.

@AnimaWish
Last active August 30, 2019 22:43
Show Gist options
  • Save AnimaWish/e303286c269fb095065d8d1ecacfd95c to your computer and use it in GitHub Desktop.
Save AnimaWish/e303286c269fb095065d8d1ecacfd95c to your computer and use it in GitHub Desktop.
Hits wanikani API v1.4 to fetch user defined mnemonics for readings and meanings
import http.client
import json
import sys
API_KEY = sys.argv[1]
separator = "\t"
conn = http.client.HTTPSConnection('www.wanikani.com')
itemTypes = ["radicals", "kanji", "vocabulary"]
csv = open("mnemonics.csv", "w")
csv.write("type{}character{}meaning{}level{}meaning_note{}reading_note\n".format(separator, separator, separator, separator, separator)) # i'm too lazy to use a csv lib atm
for itemType in itemTypes:
print(itemType)
url = "/api/user/" + API_KEY + "/" + itemType
conn.request("GET", url)
resp = conn.getresponse()
s = resp.read()
jsons = json.loads(s)
collection = jsons["requested_information"]
if itemType == "vocabulary":
collection = jsons["requested_information"]["general"]
for item in collection:
if not isinstance(item, str) and item != None and item["user_specific"] != None:
character = item["character"]
if character == None:
character = ""
meaning = ""
if item["user_specific"]["meaning_note"] != None:
meaning = item["user_specific"]["meaning_note"].replace("\n", " ").replace(separator, " ")
print(meaning)
reading = ""
if item["user_specific"]["reading_note"] != None:
reading = item["user_specific"]["reading_note"].replace("\n", " ").replace(separator, " ")
print(reading)
if meaning != "" or reading != "":
csv.write(itemType + separator + character + separator + item["meaning"].split(separator)[0] + separator + str(item["level"]) + separator + meaning + separator + reading+"\n")
csv.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment