Skip to content
Permalink
Browse files
Added comments, fixed little bug
Choosing categories now works even by typing "I qould like <Categegory>
  • Loading branch information
mateussa committed Nov 20, 2017
1 parent abaa6ea commit 82e58ae2dfcf4c8c1332e7dda9c110b47c77e457
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
@@ -1,5 +1,5 @@
# Gets JSON file data from a url # Gets JSON file data from a url
def readJSON(url): def readJSON(url): # Andre
''' Gets JSON file data from a url ''' ''' Gets JSON file data from a url '''
import urllib.request import urllib.request
import json import json
@@ -22,7 +22,7 @@ def readJSON(url):


# If desn't exist, ask for a new token and save it on the file, if it does, then # If desn't exist, ask for a new token and save it on the file, if it does, then
# read it from the file # read it from the file
def tokenGetSet(): def tokenGetSet(): # Andre
''' Gets the token from the file, or requests a new one, outputs the token ''' ''' Gets the token from the file, or requests a new one, outputs the token '''
from pathlib import Path from pathlib import Path


@@ -44,7 +44,7 @@ def tokenGetSet():


# Checks OpentDB categories and output them and the respective ID, if input true # Checks OpentDB categories and output them and the respective ID, if input true
# only output the categories names # only output the categories names
def getCategories(onlyCategories = False, nrCat = 0): def getCategories(onlyCategories = False, nrCat = 0): # Andre
''' Gets opentDB possibible categories and ID, if true just show the categories ''' ''' Gets opentDB possibible categories and ID, if true just show the categories '''
import html import html
import random import random
@@ -75,7 +75,7 @@ def getCategories(onlyCategories = False, nrCat = 0):
return(categories) return(categories)


# Gets questions from OpentDB, with defined criteria # Gets questions from OpentDB, with defined criteria
def getOpentDB(category, difficulty, nrQuestions = 1): def getOpentDB(category, difficulty, nrQuestions = 1): # Andre
''' With a set number of questions, category and difficulty level ''' With a set number of questions, category and difficulty level
outputs the questions with that criteria ''' outputs the questions with that criteria '''
import html import html
@@ -166,7 +166,7 @@ def getOpentDB(category, difficulty, nrQuestions = 1):


return(questionSet) return(questionSet)


def getBirthday(): def getBirthday(): # Andre
''' Get a random birthday and output a dictionairy with name as string, ''' Get a random birthday and output a dictionairy with name as string,
the date as string and a questions and righ answer as strings ''' the date as string and a questions and righ answer as strings '''
import json import json
@@ -183,7 +183,7 @@ def getBirthday():


return(birthdaySet) return(birthdaySet)


def getHistory(): def getHistory(): # Andre
''' Get a random history event and output a dictionairy with the event as string, ''' Get a random history event and output a dictionairy with the event as string,
the date as string and a questions and righ answer as strings ''' the date as string and a questions and righ answer as strings '''
import json import json
@@ -200,7 +200,7 @@ def getHistory():


return(historySet) return(historySet)


def getQuote(): def getQuote(): # Andre
''' Get a random quote and output a dictionairy with the quotetype as string, ''' Get a random quote and output a dictionairy with the quotetype as string,
the name and quote as string and a questions and righ answer as strings ''' the name and quote as string and a questions and righ answer as strings '''
import json import json
@@ -218,7 +218,7 @@ the name and quote as string and a questions and righ answer as strings '''
return(quoteSet) return(quoteSet)


# Main function to get questions, choose database and check for the error messages # Main function to get questions, choose database and check for the error messages
def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): # Andre
''' Given a category, diffculty as strings and number ''' Given a category, diffculty as strings and number
of questions as integer return a dictionary with of questions as integer return a dictionary with
type, question, possible answers and correct answer ''' type, question, possible answers and correct answer '''
@@ -261,7 +261,7 @@ def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"):
return(questionSet) return(questionSet)




def getGoogleSearch(toSearch): def getGoogleSearch(toSearch): # Andre
''' With a input search as string, return the first google search as a string''' ''' With a input search as string, return the first google search as a string'''


search = readJSON("https://www.googleapis.com/customsearch/v1?q={}&cx=001954664739637419008%3Aprvczty2gta&num=1&safe=medium&key=AIzaSyArPcOPqon_MSxFKkB41qCexrCVZ5AHfoU" search = readJSON("https://www.googleapis.com/customsearch/v1?q={}&cx=001954664739637419008%3Aprvczty2gta&num=1&safe=medium&key=AIzaSyArPcOPqon_MSxFKkB41qCexrCVZ5AHfoU"
@@ -4,7 +4,7 @@ import random
from DataAPI import getQuestion, getCategories from DataAPI import getQuestion, getCategories
from determineUserInput import determineUserInput from determineUserInput import determineUserInput


def sendMessage(message, EOM = True): def sendMessage(message, EOM = True): # Andre
''' Given a message input, and a False value, send the message and the ''' Given a message input, and a False value, send the message and the
client keeps wayting for another message ''' client keeps wayting for another message '''
resp = "" resp = ""
@@ -20,7 +20,7 @@ def sendMessage(message, EOM = True):
print("SERVER: {}".format(message)) print("SERVER: {}".format(message))
resp = conn.recv(1024).decode() # Waits for client feedback resp = conn.recv(1024).decode() # Waits for client feedback


def receiveMessage(): def receiveMessage(): # Andre
''' Receive a message from the client and check if received correctly, ''' Receive a message from the client and check if received correctly,
returns the message ''' returns the message '''
message = conn.recv(1024).decode() message = conn.recv(1024).decode()
@@ -37,7 +37,7 @@ def receiveMessage():


return(answerUserInput) return(answerUserInput)


def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): # Andre
''' With input of the type of answer code expected and a string question, ''' With input of the type of answer code expected and a string question,
a list of sentences if not expected answer, and the default fallback a list of sentences if not expected answer, and the default fallback
answer returns the answer to the question ''' answer returns the answer to the question '''
@@ -48,10 +48,10 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer):
answer = receiveMessage() answer = receiveMessage()
while (answer[1] != answerType or answer[0] == "" and len(noAnswers) > 0): while (answer[1] != answerType or answer[0] == "" and len(noAnswers) > 0):


if(answer[1] == 0 and answerType == -1): if(answerType == -1):
for cat in sendMessages: for cat in sendMessages:
if (answer[0].casefold() == cat.casefold()): if (cat.casefold() in answer[0].casefold()):
answer = (answer[0], -1) answer = (cat, -1)
break break
break break


@@ -79,7 +79,7 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer):


return (answer[0]) return (answer[0])


def oneQuestion(qType): def oneQuestion(qType): # Andre
''' Output a set of questions with a category choosen by the user ''' ''' Output a set of questions with a category choosen by the user '''
if (qType == "OpentDB"): if (qType == "OpentDB"):
cat = getCategories(True, 3) cat = getCategories(True, 3)
@@ -98,7 +98,7 @@ def oneQuestion(qType):
if ("Error" in questionSet): if ("Error" in questionSet):
sendMessage(questionSet[1], False) sendMessage(questionSet[1], False)
sendMessage("Lets try again.", False) sendMessage("Lets try again.", False)
return (True) return(questionSet)


# If question set of Multiple type # If question set of Multiple type
if (questionSet["Type"] == "Multiple"): if (questionSet["Type"] == "Multiple"):
@@ -145,7 +145,7 @@ def oneQuestion(qType):
sendMessage("-" * 50, False) sendMessage("-" * 50, False)




def quizChallange(nrQuestions): def quizChallange(nrQuestions): # Andre
if (nrQuestions < 1 or nrQuestions > 50): if (nrQuestions < 1 or nrQuestions > 50):
sendMessage("For a Quiz challenge ou have to choose between 2 and 50 quesitons.") sendMessage("For a Quiz challenge ou have to choose between 2 and 50 quesitons.")
return(True) return(True)
@@ -243,18 +243,26 @@ while True:


sendMessage("You got {} out of {} quesions right!".format(score, nr), False) sendMessage("You got {} out of {} quesions right!".format(score, nr), False)


if (type(message) == str and "birthday challange".casefold() in message.casefold()): elif (type(message) == str and "birthday challange".casefold() in message.casefold()):
oneQuestion("Birthday") oneQuestion("Birthday")
if (type(message) == str and "history challange".casefold() in message.casefold()):
elif (type(message) == str and "history challange".casefold() in message.casefold()):
oneQuestion("History") oneQuestion("History")
if (type(message) == str and "quote challange".casefold() in message.casefold()):
elif (type(message) == str and "quote challange".casefold() in message.casefold()):
oneQuestion("Quote") oneQuestion("Quote")

elif (type(message) == str and "challange".casefold() in message.casefold()):
message = askSomething(0, ["You can ask for a question, quiz, history, birthday or quote challange."],
["You can ask me anything else, I will try to help you."], None)
continue

elif (message == "END"): elif (message == "END"):
break break




message = askSomething(0, ["If you want you can ask anything else!"], message = askSomething(0, ["If you want you can ask anything else!"],
["You can choose more Questions to challenge your self."], None) ["You can ask me anything else, I will try to help you."], None)




sendMessage("See you next time! Bye!") sendMessage("See you next time! Bye!")

0 comments on commit 82e58ae

Please sign in to comment.