From 82e58ae2dfcf4c8c1332e7dda9c110b47c77e457 Mon Sep 17 00:00:00 2001 From: "Andre Silva (mateussa)" Date: Mon, 20 Nov 2017 23:44:18 +0000 Subject: [PATCH] Added comments, fixed little bug Choosing categories now works even by typing "I qould like --- DataAPI.py | 18 +++++++++--------- Server.py | 34 +++++++++++++++++++++------------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/DataAPI.py b/DataAPI.py index b7f2f63..ded30ea 100644 --- a/DataAPI.py +++ b/DataAPI.py @@ -1,5 +1,5 @@ # Gets JSON file data from a url -def readJSON(url): +def readJSON(url): # Andre ''' Gets JSON file data from a url ''' import urllib.request 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 # read it from the file -def tokenGetSet(): +def tokenGetSet(): # Andre ''' Gets the token from the file, or requests a new one, outputs the token ''' from pathlib import Path @@ -44,7 +44,7 @@ def tokenGetSet(): # Checks OpentDB categories and output them and the respective ID, if input true # 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 ''' import html import random @@ -75,7 +75,7 @@ def getCategories(onlyCategories = False, nrCat = 0): return(categories) # 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 outputs the questions with that criteria ''' import html @@ -166,7 +166,7 @@ def getOpentDB(category, difficulty, nrQuestions = 1): return(questionSet) -def getBirthday(): +def getBirthday(): # Andre ''' Get a random birthday and output a dictionairy with name as string, the date as string and a questions and righ answer as strings ''' import json @@ -183,7 +183,7 @@ def getBirthday(): return(birthdaySet) -def getHistory(): +def getHistory(): # Andre ''' 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 ''' import json @@ -200,7 +200,7 @@ def getHistory(): return(historySet) -def getQuote(): +def getQuote(): # Andre ''' 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 ''' import json @@ -218,7 +218,7 @@ the name and quote as string and a questions and righ answer as strings ''' return(quoteSet) # 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 of questions as integer return a dictionary with type, question, possible answers and correct answer ''' @@ -261,7 +261,7 @@ def getQuestion(category, difficulty, nrQuestions = 1, qSource = "OpentDB"): return(questionSet) -def getGoogleSearch(toSearch): +def getGoogleSearch(toSearch): # Andre ''' 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" diff --git a/Server.py b/Server.py index 051513e..dc762cf 100644 --- a/Server.py +++ b/Server.py @@ -4,7 +4,7 @@ import random from DataAPI import getQuestion, getCategories 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 client keeps wayting for another message ''' resp = "" @@ -20,7 +20,7 @@ def sendMessage(message, EOM = True): print("SERVER: {}".format(message)) 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, returns the message ''' message = conn.recv(1024).decode() @@ -37,7 +37,7 @@ def receiveMessage(): 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, a list of sentences if not expected answer, and the default fallback answer returns the answer to the question ''' @@ -48,10 +48,10 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): answer = receiveMessage() 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: - if (answer[0].casefold() == cat.casefold()): - answer = (answer[0], -1) + if (cat.casefold() in answer[0].casefold()): + answer = (cat, -1) break break @@ -79,7 +79,7 @@ def askSomething(answerType, sendMessages, noAnswers, defaultAnswer): return (answer[0]) -def oneQuestion(qType): +def oneQuestion(qType): # Andre ''' Output a set of questions with a category choosen by the user ''' if (qType == "OpentDB"): cat = getCategories(True, 3) @@ -98,7 +98,7 @@ def oneQuestion(qType): if ("Error" in questionSet): sendMessage(questionSet[1], False) sendMessage("Lets try again.", False) - return (True) + return(questionSet) # If question set of Multiple type if (questionSet["Type"] == "Multiple"): @@ -145,7 +145,7 @@ def oneQuestion(qType): sendMessage("-" * 50, False) -def quizChallange(nrQuestions): +def quizChallange(nrQuestions): # Andre if (nrQuestions < 1 or nrQuestions > 50): sendMessage("For a Quiz challenge ou have to choose between 2 and 50 quesitons.") return(True) @@ -243,18 +243,26 @@ while True: 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") - if (type(message) == str and "history challange".casefold() in message.casefold()): + + elif (type(message) == str and "history challange".casefold() in message.casefold()): 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") + + 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"): break 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!")