Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
import socket
import time
import sqlite3
import requests
import urllib
from functions import *
from google import search
from bs4 import BeautifulSoup
conn = sqlite3.connect('chatBotDB.db')
c = conn.cursor()
import os, sys
from random import *
from lxml import html
def Main():
#Give ChatBot server an IP address and port
host = "127.0.0.1"
port = 50036
#Create Socket and bind server to socket
thisSocket = socket.socket()
thisSocket.bind((host,port))
#Listen for clients
thisSocket.listen(1)
#Connect to client
conn, addr = thisSocket.accept()
#print Connect ip address
print ("The Connection ip is : " + str(addr))
#Repear forever
while True:
sessionUsername = ""
checkYes = ("yes", "Yes", "y", "ok", "OK", "sure", "Sure")
checkNo = ("No", "Never", "N", "n", "nop", "no", "NO")
checkSaudations =("hi", "Hi foodie", "hello", "Hello")
checkGuest = ("guest", "guest account", "g")
checkCreate = ("create", "account", "create account","new account")
#Receive info from client
receiveMess = conn.recv(1024).decode()
#if no info from client end loop
if not receiveMess:
break
#Print info from client
print ("Message from User to Chatbot : " + str(receiveMess))
typeAccount = 2
#set return message
checkUser = "Hi user, are you already registered ?"
conn.send(checkUser.encode())
receiveCheckUser = conn.recv(1024).decode()
#Antonio Code
if receiveCheckUser in checkYes:
#while loop that will keep going until username and password are entered correctly
checkUserDetails = False
while checkUserDetails == False:
#loginUser variable is asking the user to give their username
loginUser= "Ok can you please give me your username. "
conn.send(loginUser.encode())
recieveLoginUser= conn.recv(1024).decode()
#loginPassword is asking the user for their password
loginPassword= "Ok can you now enter your password. "
conn.send(loginPassword.encode())
recieveLoginPassword= conn.recv(1024).decode()
checkLogin = loginForm(recieveLoginUser, recieveLoginPassword) #Luke function
if checkLogin:
typeAccount = 1 #checkLogin is the return which is true or false
congrats ="Logged in successfully" #type accounts 1- registered and logged in 2- guest
conn.send(congrats.encode())
sessionUsername = recieveLoginUser
#print(showData(recieveLoginUser, recieveLoginPassword)) #!!!!!! watch out
checkUserDetails = True
else:
failLogin = "Something appears to be wrong "
conn.send(failLogin.encode())
checkUserDetails = False
else:
askCreateOrGuest = "Do you want to create an account or continue as a guest user? "
conn.send(askCreateOrGuest.encode())
responseCOG = conn.recv(1024).decode()
if responseCOG in checkCreate: #if value is in checkCreate tuple, means user wants to create account if not, user use guest account
passwords = False
while passwords == False:
askLocation = "What's your location ? " #ask all data needed to register
conn.send(askLocation.encode()) #antonio code
mainLocation = conn.recv(1024).decode()
askName = "Type your username "
conn.send(askName.encode())
username = conn.recv(1024).decode()
askPassword = "Please type your password "
conn.send(askPassword.encode())
password = conn.recv(1024).decode()
checkPassword = "Please re-enter your password "
conn.send(checkPassword.encode())
password2 = conn.recv(1024).decode()
if password == password2:
dados = registerForm(username,password, mainLocation) #run the functions untill get two passwords iquals
if dados == True:
messageRegist ="Account created"
conn.send(messageRegist.encode())
sessionUsername = username
typeAccount = 1
else:
messageRegistError ="Something wrong"
conn.send(messageRegistError.encode())
passwords = True
else:
typeAccount = 2
usingGuest = "hi user, now you are using a guest account."
conn.send(usingGuest.encode())
suggests = "\nDo you want me to suggest you some options around Coventry ?"
conn.send(suggests.encode())
checkSuggestion = conn.recv(1024).decode()
if checkSuggestion in checkYes:
page = requests.get('https://www.tripadvisor.co.uk/Restaurants-g186403-Coventry_West_Midlands_England.html')
tree = html.fromstring(page.content)
restaurants = tree.xpath('//a[@class="poiTitle"]/text()')
print ("Restaurants: ")
shuffle(restaurants)
i=0
myList = []
while i<4:
print (restaurants[i])
myList.append(restaurants[i]+ "\n")
i=i+1
j=0
while j < len(myList):
conn.send(myList[j].encode())
j=j+1
#Antonio Code
if typeAccount == 1:
moreOptions= "Want to have more options not necessarly being over Coventry ?"
conn.send(moreOptions.encode())
checkMoreOptions = conn.recv(1024).decode()
if checkMoreOptions in checkYes: #check if user wants to know more options
locationRequest = "Do you want to use your main location as your location ?"
conn.send(locationRequest.encode())
checkLocation = conn.recv(1024).decode()
if checkLocation in checkYes: #check if user wants to use location value at the database
location = takeLocation(sessionUsername)
print("user main location "+location)
else:
askNewLocation = "Please type a new location"
conn.send(askNewLocation.encode())
location = conn.recv(1024).decode()
askKitchen = "Which type of kitchen will you want to try ?"
conn.send(askKitchen.encode())
typeRestaurant = conn.recv(1024).decode()
searchGap = typeRestaurant + "restaurants in" + location
for url in search(searchGap, stop=2):
conn.send(url.encode())
print(url)
presentLink = "\n As you can see, there is a link redirecting to your possible destinations"
conn.send(presentLink.encode())
else:
afterSuggetions = "You have chosen to use a guest account, so, you are not able to go further"
conn.send(afterSuggetions.encode())
regards = "Thank you guest"
conn.send(regards.encode())
conn.close()
if __name__ == '__main__':
Main()