<commit_before>"""

Title:        levep

Description:  Identify the next review time for the earliest apprentice radical
              and kanji of the user's level

Author:       <NAME>

Email:        <EMAIL>

Date Created: 2013-07-02

"""
import urllib
import json
from datetime import datetime
import calendar
import os.path


INFTYPES = ["radicals", "kanji"]


def convertJSON(url):
    """
    Read a URL string and deserialise it as JSON.
    """
    return json.load(urllib.urlopen(url))


def lev_cal(baseURL):
    """
    Obtain's the user's level.
    """
    url = baseURL + "/user-information"
    userInfo = convertJSON(url)
    return userInfo["user_information"]["level"]  


def list_read(inftype, baseURL, level):
    """
    Return length of time until the soonest, current level's radical or kanji.
    """
    url = baseURL + "/{}/{}".format(inftype, level)
    jout = convertJSON(url)

    #Identify the lowest available date
    av_dates = [jout["requested_information"][i]["stats"]["available_date"]
                for i in range(len(jout["requested_information"]))
                if jout["requested_information"][i]["stats"] is not None]
    try:
        lowest = min(av_dates)
    except:
        return None  # no unlocked inftype for the current level

    #Identify the current time
    ctime = datetime.utcnow()
    cunix = calendar.timegm(ctime.utctimetuple())

    #Calculate the next review time
    revnext = int(lowest) - cunix
    if revnext < 0:
        revnext = 0

    #Convert to a more readable format
    seconds = revnext % 60
    minutes = revnext / 60 % 60
    hours = revnext / 3600 % 24
    days = revnext / 86400
    
    return (days, hours, minutes, seconds)


def getAPI(fname, nokeyURL):
    """
    Grabs the user's API key, or requests and stores a new one from user input
    if it is not found.
    """
    fl = open(fname, "a").close()  # create an empty file if none exists
    fl = open(fname, "r+")
    data = fl.read()

    #Create the file to store the API key, if none exists.
    if not data:
        fl = open(fname, "w")
        api = reqAPI(nokeyURL)
        fl.write(api)
        print "\nSuccess! API key stored.\n\n"
        fl.close()
        return api

    #Read the stored API key
    return data


def reqAPI(nokeyURL):
    """
    Requests the user's API key.
    """
    while True:
        api = raw_input("Type your API key and press 'Enter'.\n")

        #API keys have 32 characters.
        if not len(api) == 32:
            print "\nInvalid API key. Please try again.\n\n"
        else:

            #Check that the correct API key was typed.
            try:
                jout = convertJSON(nokeyURL + api + "/user-information")
                user = jout["user_information"]["username"]
                crct = raw_input("\nAre you {}? If so, type 'Y'".format(user) +
                                 " and then press 'Enter', else just press " +
                                 "'Enter'.\n")
                if crct.upper() == "Y":
                    return api
                else:
                    print "Not you? Please try again.\n\n"

            #No user found for the given API key
            except:
                print ("\nNo user found for the API key provided. " +
                       "Please try again.\n\n")


#API key storage location
fname = "api.dat"

#Construct basic URL
nokeyURL = "http://www.wanikani.com/api/user/"
api = getAPI(fname, nokeyURL)
baseURL = nokeyURL + api

#Obtain user's level
level = lev_cal(baseURL)

#Output the results
for inftype in INFTYPES:
    time = list_read(inftype, baseURL, level)
    if time is None:
        print "No unlocked {} for the current level.".format(inftype)
    else:
        if inftype == "radicals":
            inftype = "radical"
        print "Next {}: {} days {} hours {} minutes {} seconds".format(inftype,
                                                                       time[0],
                                                                       time[1],
                                                                       time[2],
                                                                       time[3])

#Check if one wants to change their API key.
inp = raw_input("\nDid you want to change your API key? If so, type 'reset' " +
                "and press 'Enter'. Else, just press 'Enter' to close the " +
                "program.\n")
if inp.lower() == "reset":
    os.remove(fname)
    print
    getAPI(fname, nokeyURL)<commit_msg>Moved hanging code into functions as is right and proper.<commit_after>"""

Title:        levep

Description:  Identify the next review time for the earliest apprentice radical
              and kanji of the user's level

Author:       <NAME>

Email:        <EMAIL>

Date Created: 2013-07-02

"""
import urllib
import json
from datetime import datetime
import calendar
import os.path


def main():
    #API key storage location
    fname = "api.dat"

    #Construct basic URL
    nokeyURL = "http://www.wanikani.com/api/user/"
    api = getAPI(fname, nokeyURL)
    baseURL = nokeyURL + api

    #Obtain user's level
    level = lev_cal(baseURL)

    #Output the length of time
    resout(baseURL, level)

    #Check if the user wants to change their API key.
    inp = raw_input("\nDid you want to change your API key? If so, type " +
                    "'reset' and press 'Enter'. Else, just press 'Enter' " +
                    "to close the program.\n")
    if inp.lower() == "reset":
        os.remove(fname)
        print
        main()


def convertJSON(url):
    """
    Read a URL string and deserialise it as JSON.
    """
    return json.load(urllib.urlopen(url))


def lev_cal(baseURL):
    """
    Obtain's the user's level.
    """
    url = baseURL + "/user-information"
    userInfo = convertJSON(url)
    return userInfo["user_information"]["level"]


def list_read(inftype, baseURL, level):
    """
    Return length of time until the soonest, current level's radical or kanji.
    """
    url = baseURL + "/{}/{}".format(inftype, level)
    jout = convertJSON(url)

    #Identify the lowest available date
    av_dates = [jout["requested_information"][i]["stats"]["available_date"]
                for i in range(len(jout["requested_information"]))
                if jout["requested_information"][i]["stats"] is not None]
    try:
        lowest = min(av_dates)
    except:
        return None  # no unlocked inftype for the current level

    #Identify the current time
    ctime = datetime.utcnow()
    cunix = calendar.timegm(ctime.utctimetuple())

    #Calculate the next review time
    revnext = int(lowest) - cunix
    if revnext < 0:
        revnext = 0

    #Convert to a more readable format
    seconds = revnext % 60
    minutes = revnext / 60 % 60
    hours = revnext / 3600 % 24
    days = revnext / 86400

    return (days, hours, minutes, seconds)


def getAPI(fname, nokeyURL):
    """
    Grabs the user's API key, or requests and stores a new one from user input
    if it is not found.
    """
    fl = open(fname, "a").close()  # create an empty file if none exists
    fl = open(fname, "r+")
    data = fl.read()

    #Create the file to store the API key, if none exists.
    if not data:
        fl = open(fname, "w")
        api = reqAPI(nokeyURL)
        fl.write(api)
        print "\nSuccess! API key stored.\n\n"
        fl.close()
        return api

    #Read the stored API key
    return data


def reqAPI(nokeyURL):
    """
    Requests the user's API key.
    """
    while True:
        api = raw_input("Type your API key and press 'Enter'.\n")

        #API keys have 32 characters.
        if not len(api) == 32:
            print "\nInvalid API key. Please try again.\n\n"
        else:

            #Check that the correct API key was typed.
            try:
                jout = convertJSON(nokeyURL + api + "/user-information")
                user = jout["user_information"]["username"]
                crct = raw_input("\nAre you {}? If so, type 'Y'".format(user) +
                                 " and then press 'Enter', else just press " +
                                 "'Enter'.\n")
                if crct.upper() == "Y":
                    return api
                else:
                    print "Not you? Please try again.\n\n"

            #No user found for the given API key
            except:
                print ("\nNo user found for the API key provided. " +
                       "Please try again.\n\n")


def resout(baseURL, level):
    """
    Outputs the length of time until the soonest, current level's radicals
    and kanji.
    """
    INFTYPES = ["radicals", "kanji"]
    for inftype in INFTYPES:
        time = list_read(inftype, baseURL, level)
        if time is None:
            print "No unlocked {} for the current level.".format(inftype)
        else:
            if inftype == "radicals":
                inftype = "radical"
            print ("Next {}: {} days ".format(inftype, time[0]) +
                   "{} hours {} minutes {} seconds".format(time[1], time[2],
                                                           time[3]))

if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.10771233322486169, 'fraction_numerical': 0.007593014426727411, 'mean_word_length': 1.8386699507389161, 'pattern_counts': {'":': 7, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2153118', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2661, 'n_words': 904}