Last active
June 8, 2017 19:03
-
-
Save benburrill/905299eb795f99797f0e to your computer and use it in GitHub Desktop.
This file contains 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
# Fill this out with your real username/password | |
USERNAME = "???" | |
PASSWORD = "???" |
This file contains 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 kacpaw | |
# Create kapi_config.py. It should have the variables USERNAME and PASSWORD (your KA login) | |
import kapi_config | |
import pickle # for storing responded-to comments | |
import re # regex | |
from math import sqrt | |
from time import sleep | |
# first we use the KASession class to login to KA | |
session = kacsaw.KASession(kapi_config.USERNAME, kapi_config.PASSWORD) | |
# now we create a program object representing the KA program in question | |
program = kacsaw.Program("4617827881975808") | |
# these create answer strings | |
answer_format = "The square root of your number is `{}`.".format | |
fail_format = "`sqrt({})` doesn't make any sense to me!".format | |
# this creates a full reply from the answer(s) to questions | |
reply_format = ("Your answer(s):\n{}\n" | |
"This bot checks the comments every 10 minutes assuming that " | |
"my computer is on and the program is running, so be patient!").format | |
# this is the file name where comments that have been replied to are stored | |
# we need this because we don't want to re-reply to comments whenever the program is restarted | |
found_file_name = "found.pik" | |
# matches a question | |
pattern = re.compile(r"^\s*The number is[\s`]+(.+?)(?:[\s.!`]|$)", re.IGNORECASE | re.MULTILINE) | |
# a helper function for saving data to the found.pik file | |
def save(data): | |
# opens the file in binary write mode | |
with open(found_file_name, "wb") as found_file: | |
# and dumps the data into it | |
pickle.dump(data, found_file) | |
# pretty similar - loads from the file instead of saving to it | |
def load(): | |
try: | |
with open(found_file_name, "rb") as found_file: | |
return pickle.load(found_file) | |
# since found.pik stores a list, if the file doesn't exist, return an empty one | |
except FileNotFoundError: return [] | |
def make_answer(num_str): | |
try: | |
result = sqrt(float(num_str)) | |
except ValueError: # error, probably in parsing the input_str, but also applies to domain errors | |
print("Couldn't calculate sqrt of", num_str) | |
return fail_format(num_str) | |
else: # no error | |
print("Success! Got", result) | |
return answer_format(result) | |
def make_reply(comment_str): | |
groups = pattern.findall(comment_str) | |
if groups: # there's a match! | |
print("Found new match:", repr(reply.text_content)) | |
# separate answer strings with new lines | |
return reply_format("\n".join(map(make_answer, groups))) | |
raise ValueError("No matches found!") # so we can easily tell if a match was found | |
# load found comments on startup | |
found = load() | |
while True: | |
for reply in program.get_replies(): | |
if reply.id not in found: # we haven't found it yet | |
try: reply.reply(session, make_reply(reply.text_content)) | |
except ValueError: continue # doesn't match, go on to the next reply | |
# keep track of of the id internally | |
found.append(reply.id) | |
# and save it to the pickle file for future runs | |
save(found) | |
# wait 10 minutes before trying again | |
sleep(10 * 60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment