Created
October 12, 2015 02:33
-
-
Save benburrill/75a04280a157f3c4dd93 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import json | |
| import base64 | |
| import requests | |
| import mimetypes | |
| import urllib.parse | |
| from pprint import pprint | |
| from requests_oauthlib import OAuth1 | |
| CONSUMER_KEY = "INSERT KEY HERE" | |
| CONSUMER_SECRET = "INSERT SECRET HERE" | |
| USERNAME = "INSERT KA USERNAME HERE" | |
| PASSWORD = "INSERT KA PASSWORD HERE" | |
| class KAGame: | |
| def __init__(self, key, secret, username, password, user_agent="Ben-Burrill-Bot Python"): | |
| self.key = key | |
| self.secret = secret | |
| self.session = requests.Session() | |
| self.session.headers["User-Agent"] = user_agent | |
| self.auth = self.get_auth(username, password) | |
| def get_auth(self, username, password): | |
| auth = self.get_token( | |
| "https://www.khanacademy.org/api/auth2/request_token", | |
| OAuth1(self.key, self.secret), | |
| signature_type="query" | |
| ) | |
| resp = self.session.post( | |
| "https://www.khanacademy.org/api/auth2/authorize", | |
| auth=auth, | |
| params={ | |
| "identifier": username, | |
| "password": password | |
| } | |
| ) | |
| return self.get_token( | |
| "https://www.khanacademy.org/api/auth2/access_token", | |
| auth=auth | |
| ) | |
| def get_comments(self, program_id, **params): | |
| for page in self.get_comment_data(program_id, **params): | |
| yield from page["feedback"] | |
| def reply_to_comment(self, comment_key, comment, topic="computer-programming"): | |
| self.session.post( | |
| "https://www.khanacademy.org/api/internal/discussions/{comment_key}/replies".format( | |
| comment_key=comment_key | |
| ), | |
| auth=self.auth, | |
| data=json.dumps({ | |
| "text":comment, | |
| "topic_slug":topic | |
| }) | |
| ) | |
| def post_program(self, program_id, program_data, image_url): | |
| body = self.get_program_data(program_id) | |
| body["revision"].update({ | |
| "image_url": "data:{mimetype};base64,{data}".format( | |
| mimetype=mimetypes.guess_type(image_url)[0], | |
| data=base64.b64encode(self.session.get(image_url).text.encode()) | |
| ), | |
| "code": program_data | |
| }) | |
| result = self.session.put( | |
| "https://www.khanacademy.org/api/internal/scratchpads/{program_id}".format( | |
| program_id=program_id | |
| ), | |
| data=json.dumps(body), | |
| auth=self.auth | |
| ) | |
| print(result) | |
| print(result.request.headers) | |
| # print(result.text) | |
| def get_token(self, url, auth, signature_type="auth_header"): | |
| token_resp = self.session.get(url, auth=auth) | |
| token = dict(urllib.parse.parse_qsl(token_resp.text, strict_parsing=True)) | |
| return OAuth1( | |
| self.key, self.secret, | |
| token["oauth_token"], | |
| token["oauth_token_secret"], | |
| signature_type=signature_type | |
| ) | |
| def get_comment_data(self, program_id, **params): | |
| comments = json.loads(self.session.get( | |
| "https://www.khanacademy.org/api/internal/discussions/scratchpad/{program_id}/comments".format( | |
| program_id=program_id | |
| ), params=dict({ | |
| "sort": 1, | |
| "subject": "all", | |
| "lang": "en", | |
| "limit": 10 | |
| }, **params) | |
| ).text) | |
| yield comments | |
| if not comments["isComplete"]: | |
| yield from self.get_comment_data(program_id, cursor=comments["cursor"]) | |
| def get_program_data(self, program_id): | |
| return json.loads(self.session.get( | |
| "https://www.khanacademy.org/api/labs/scratchpads/{program_id}".format( | |
| program_id=program_id | |
| ) | |
| ).text) | |
| game = KAGame(CONSUMER_KEY, CONSUMER_SECRET, USERNAME, PASSWORD) | |
| # tests: | |
| # with open("comments", "w", encoding="utf-8") as f: | |
| # for comment in game.get_comments("5451500160155648"): | |
| # print(comment["authorNickname"], "(with", comment["sumVotesIncremented"], "votes)", "said:", file=f) | |
| # print(comment["content"], end="\n\n", file=f) | |
| # game.post_program("4617827881975808", | |
| # "// this comment was made by a python script!", | |
| # "https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png" | |
| # ) | |
| game.login_test() | |
| # for comment in game.get_comments("4617827881975808"): | |
| # game.reply_to_comment(comment["key"], "Hello, I am a robot!") | |
| # token_resp = requests.get( | |
| # "https://www.khanacademy.org/api/v1/user?username=benburrill", | |
| # auth=game.auth | |
| # ) | |
| # print(token_resp) | |
| # print(token_resp.request.headers) | |
| # print(token_resp.text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment