GithubHelp home page GithubHelp logo

jb-1980 / khan_api_wrapper Goto Github PK

View Code? Open in Web Editor NEW
7.0 2.0 2.0 34 KB

Simple, direct Python 3 wrapper for the Khan Academy API

License: MIT License

Python 100.00%
khanacademy khan-api python3

khan_api_wrapper's Introduction

khan_api_wrapper (Python 3 Wrapper for Khan Academy API)


About

This is a simple implementation of using the Khan Academy API with python. It uses the alternative method of logging in with your own account, and should be sufficient if you are only looking to retrieve data from your students.

Requires

  • requests
  • rauth

Set up:

Install:

$ pip install khan_api_wrapper

Register your app with Khan Academy, to get the necessary tokens. That is it, you should now be able to use the wrapper in your python script.

General use:

test in an interactive shell:

$ python
> from khan_api_wrapper.khan import KhanAcademySignIn, KhanAPI
>
> consumer_key = "Key from registering app"
> consumer_token = "Token from registering app"
> khan_identifier = "username_of_account_used_to_register_app"
> khan_password = "password_of_account_used_to_register_app"
> kauth = KhanAcademySignIn(consumer_key, consumer_token, khan_identifier, khan_password)
> token, secret = kauth.authorize_self()
>
> kapi = KhanAPI(consumer_key, consumer_token, token, secret)
> kapi.user() # should print your user data to console.
>
> # Use your own endpoint
> kapi.get("/api/internal/user/missions") # should show your missions

Examine khan.py for all the available methods or example.py for ideas on how to use in your application.

Token freshness:

Through trial I have discovered that the access token and secret are valid for 2 weeks. So you may consider storing them in a separate file or database, and write a function to only fetch tokens if they are expired.

def get_tokens():

    # fetch token data from saved json file
    with open("tokens.json", "r") as f:
        tokens = json.loads(f.read())

    # check if tokens are expired
    now = time.time()
    if now - tokens["timestamp"] > 3600 * 24 * 14:
        kauth = KhanAcademySignIn(consumer_key, consumer_secret, token, uname, pwd)
        token, secret = kauth.authorize_self()

        # update file with new tokens and timestamp
        with open("tokens.json", "w") as t:
            t.write(
                json.dumps({"token": token, "secret": secret, "timestamp": now})
            )

        # return fresh tokens
        return token, secret

    # tokens are still valid, so return them
    return tokens["token"], tokens["secret"]

# Then use the function to ensure we only use fresh tokens when necessary
token, secret = get_tokens()
kapi = KhanAPI(consumer_key, consumer_secret, token, secret)
...

khan_api_wrapper's People

Contributors

flaviobarros avatar jb-1980 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

khan_api_wrapper's Issues

Your example have bugs

Firstly thank you! You have solved a big issue for me with this library! I was trying to use it now and I detected some mistakes in the example documentation. In the example you have used

token, secret = kauth.authenticate_self()

but looking at your code the correct is

token, secret = kauth.authorize_self()

Where you use

kapi = KhanApi(consumer_key, consumer_token, token, secret)

the correct is

kapi = KhanAPI(consumer_key, consumer_token, token, secret)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.