203 lines
7.6 KiB
Python
203 lines
7.6 KiB
Python
import lxml.etree # Used by imageFunc
|
|
import re # Used by roll_dice
|
|
import datetime # Used by helloFunc
|
|
import json # Used by spellFunc
|
|
import random # Used by imageFunc
|
|
import urllib # Used by imageFunc and triviaStart
|
|
import imdb # Used by movieFunc
|
|
import time # Used for logging
|
|
import logging # Used for... you know... logging
|
|
import wikia # Used by findWikiPage
|
|
|
|
from .roll import dice
|
|
|
|
logging.basicConfig(filename="gwendolyn.log", level=logging.INFO)
|
|
|
|
# Capitalizes all words except some of them
|
|
no_caps_list = ["of","the"]
|
|
def cap(s):
|
|
word_number = 0
|
|
lst = s.split()
|
|
res = ''
|
|
for word in lst:
|
|
word_number += 1
|
|
if word not in no_caps_list or word_number == 1:
|
|
word = word.capitalize()
|
|
res += word+" "
|
|
res = res[:-1]
|
|
return res
|
|
|
|
def time_in_range(start, end, x):
|
|
"""Return true if x is in the range [start, end]"""
|
|
if start <= end:
|
|
return start <= x <= end
|
|
else:
|
|
return start <= x or x <= end
|
|
|
|
# Responds with a greeting of a time-aprpriate maner
|
|
def helloFunc(author):
|
|
now = datetime.datetime.now()
|
|
if time_in_range(now.replace(hour=5, minute=0, second=0, microsecond=0),now.replace(hour=10, minute=0, second=0, microsecond=0), now):
|
|
return("Good morning, "+str(author))
|
|
elif time_in_range(now.replace(hour=10, minute=0, second=0, microsecond=0),now.replace(hour=13, minute=0, second=0, microsecond=0), now):
|
|
return("Good day, "+str(author))
|
|
elif time_in_range(now.replace(hour=13, minute=0, second=0, microsecond=0),now.replace(hour=18, minute=0, second=0, microsecond=0), now):
|
|
return("Good afternoon, "+str(author))
|
|
elif time_in_range(now.replace(hour=18, minute=0, second=0, microsecond=0),now.replace(hour=22, minute=0, second=0, microsecond=0), now):
|
|
return("Good evening, "+str(author))
|
|
elif time_in_range(now.replace(hour=22, minute=0, second=0, microsecond=0),now.replace(hour=23, minute=59, second=59, microsecond=0), now):
|
|
return("Good night, "+str(author))
|
|
else:
|
|
return("Why hello, "+str(author))
|
|
|
|
# Finds a random picture online
|
|
def imageFunc():
|
|
# Picks a type of camera, which decides the naming scheme
|
|
cams = ("one","two","three","four")
|
|
cam = random.choice(cams)
|
|
logThis("Chose cam type "+cam)
|
|
if cam == "one":
|
|
a = str(random.randint(0 ,9))
|
|
b = str(random.randint(0,9))
|
|
c = str(random.randint(0,9))
|
|
d = str(random.randint(0,9))
|
|
search = ("img_"+a+b+c+d)
|
|
elif cam == "two":
|
|
a = str(random.randint(2012,2016))
|
|
b = str(random.randint(1,12)).zfill(2)
|
|
c = str(random.randint(1,29)).zfill(2)
|
|
search = ("IMG_"+a+b+c)
|
|
elif cam == "three":
|
|
a = str(random.randint(1,500)).zfill(4)
|
|
search = ("IMAG_"+a)
|
|
elif cam == "four":
|
|
a = str(random.randint(0,9))
|
|
b = str(random.randint(0,9))
|
|
c = str(random.randint(0,9))
|
|
d = str(random.randint(0,9))
|
|
search = ("DSC_"+a+b+c+d)
|
|
|
|
logThis("Searching for "+search)
|
|
|
|
# Searches for the image and reads the resulting web page
|
|
page = urllib.request.urlopen("https://www.bing.com/images/search?q="+search+"&safesearch=off")
|
|
read = page.read()
|
|
tree = lxml.etree.HTML(read)
|
|
images = tree.xpath('//a[@class = "thumb"]/@href')
|
|
|
|
# Picks an image
|
|
number = random.randint(1,len(images))-1
|
|
image = images[number]
|
|
|
|
logThis("Picked image number "+str(number))
|
|
|
|
# Returns the image
|
|
logThis("Successfully returned an image")
|
|
return(image)
|
|
|
|
def logThis(message : str):
|
|
localtime = time.asctime(time.localtime(time.time()))
|
|
print(localtime+" - "+message)
|
|
logging.info(localtime+" - "+message)
|
|
|
|
# Finds a page from the Senkulpa Wikia
|
|
def findWikiPage(search : str):
|
|
logThis("Trying to find wiki page for "+search)
|
|
wikia.set_lang("da")
|
|
searchResults = wikia.search("senkulpa",search)
|
|
if len(searchResults) > 0:
|
|
try:
|
|
searchResult = searchResults[0].replace(",","%2C")
|
|
logThis("Found it")
|
|
page = wikia.page("senkulpa",searchResult)
|
|
content = page.content.replace(u'\xa0', u' ').split("\n")[0]
|
|
|
|
images = page.images
|
|
if len(images) > 0:
|
|
image = images[len(images)-1]+"/revision/latest?path-prefix=da"
|
|
return page.title, content, image
|
|
else:
|
|
return page.title, content, ""
|
|
except:
|
|
logThis("Fucked up")
|
|
return "", "Sorry. Fucked that one up", ""
|
|
else:
|
|
logThis("Couldn't find the page")
|
|
return "", "Couldn't find page", ""
|
|
|
|
def triviaStart(channel : str):
|
|
with open("resources/trivia.json", "r") as f:
|
|
triviaFile = json.load(f)
|
|
|
|
logThis("Trying to find a trivia question for "+channel)
|
|
|
|
if channel not in triviaFile["questions"]:
|
|
with urllib.request.urlopen("https://opentdb.com/api.php?amount=10&type=multiple") as response:
|
|
data = json.loads(response.read())
|
|
|
|
logThis("Found the question \""+data["results"][0]["question"]+"\"")
|
|
answers = data["results"][0]["incorrect_answers"]
|
|
answers.append(data["results"][0]["correct_answer"])
|
|
random.shuffle(answers)
|
|
correctAnswer = answers.index(data["results"][0]["correct_answer"]) + 97
|
|
|
|
triviaFile["questions"][channel] = {"answer" : str(chr(correctAnswer)),"players" : {}}
|
|
with open("resources/trivia.json", "w") as f:
|
|
json.dump(triviaFile,f,indent=4)
|
|
|
|
question = data["results"][0]["question"].replace("'","\'").replace(""","\"")
|
|
|
|
return question, answers, correctAnswer
|
|
else:
|
|
logThis("There was already a trivia question for that channel")
|
|
return "There's already a trivia question going on. Try again in like, a minute", "", ""
|
|
|
|
def triviaOtherThing(user : str, channel : str, command : str):
|
|
with open("resources/trivia.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
if command in ["a","b","c","d"]:
|
|
if channel in data["questions"]:
|
|
if user not in data["questions"][channel]["players"]:
|
|
logThis(user+" answered the question in "+channel)
|
|
|
|
data["questions"][channel]["players"][user] = command
|
|
|
|
with open("resources/trivia.json", "w") as f:
|
|
json.dump(data,f,indent=4)
|
|
|
|
return "Locked in "+user+"'s answer"
|
|
else:
|
|
return user+" has already answered this question"
|
|
else:
|
|
return "There's no question right now"
|
|
elif command == "p":
|
|
items = {k: v for k, v in sorted(data["users"].items(), key=lambda item: item[1])}
|
|
|
|
return "\n".join(['%s: %s' % (key, value) for (key, value) in items.items()])
|
|
else:
|
|
return "I didn't quite understand that"
|
|
|
|
def triviaCountPoints(channel : str):
|
|
with open("resources/trivia.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
logThis("Counting points for question in "+channel)
|
|
|
|
if channel in data["questions"]:
|
|
for player, answer in data["questions"][channel]["players"].items():
|
|
if answer == data["questions"][channel]["answer"]:
|
|
if player in data["users"]:
|
|
points = data["users"][player]
|
|
data["users"][player] = points + 1
|
|
else:
|
|
data["users"][player] = 1
|
|
|
|
with open("resources/trivia.json", "w") as f:
|
|
json.dump(data,f,indent=4)
|
|
|
|
else:
|
|
logThis("Couldn't find the question")
|
|
|
|
return None
|