Advertisement
mastterbejo

Untitled

Dec 13th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.60 KB | None | 0 0
  1. import getpass
  2. from termcolor import colored
  3. from pusher import Pusher
  4. import pysher
  5. from dotenv import load_dotenv
  6. import os
  7. import json
  8.  
  9. load_dotenv(dotenv_path='.env')
  10.  
  11. class terminalChat():
  12. pusher = None
  13. channel = None
  14. chatroom = None
  15. clientPusher = None
  16. user = None
  17. users = {
  18. "samuel": "samuel'spassword",
  19. "daniel": "daniel'spassword",
  20. "tobi": "tobi'spassword",
  21. "sarah": "sarah'spassword"
  22. }
  23. chatrooms = ["sports", "general", "education", "health", "technology"]
  24.  
  25. ''' The entry point of the application'''
  26. def main(self):
  27. self.login()
  28. self.selectChatroom()
  29. while True:
  30. self.getInput()
  31.  
  32. ''' This function handles logon to the system. In a real world app,
  33. you might need to connect to API's or a database to verify users '''
  34.  
  35. def login(self):
  36. username = input("Please enter your username: ")
  37. password = getpass.getpass("Please enter %s's Password:" % username)
  38. if username in self.users:
  39. if self.users[username] == password:
  40. self.user = username
  41. else:
  42. print(colored("Your password is incorrect", "red"))
  43. self.login()
  44. else:
  45. print(colored("Your username is incorrect", "red"))
  46. self.login()
  47.  
  48. ''' This function is used to select which chatroom you would like to connect to '''
  49. def selectChatroom(self):
  50. print(colored("Info! Available chatrooms are %s" % str(self.chatrooms), "blue"))
  51. chatroom = input(colored("Please select a chatroom: ", "green"))
  52. if chatroom in self.chatrooms:
  53. self.chatroom = chatroom
  54. self.initPusher()
  55. else:
  56. print(colored("No such chatroom in our list", "red"))
  57. self.selectChatroom()
  58.  
  59. ''' This function initializes both the Http server Pusher as well as the clientPusher'''
  60. def initPusher(self):
  61. self.pusher = Pusher(app_id=os.getenv('PUSHER_APP_ID', None), key=os.getenv('PUSHER_APP_KEY', None), secret=os.getenv('PUSHER_APP_SECRET', None), cluster=os.getenv('PUSHER_APP_CLUSTER', None))
  62. self.clientPusher = pysher.Pusher(os.getenv('PUSHER_APP_KEY', None), os.getenv('PUSHER_APP_CLUSTER', None))
  63. self.clientPusher.connection.bind('pusher:connection_established', self.connectHandler)
  64. self.clientPusher.connect()
  65.  
  66. ''' This function is called once pusher has successfully established a connection'''
  67. def connectHandler(self, data):
  68. self.channel = self.clientPusher.subscribe(self.chatroom)
  69. self.channel.bind('newmessage', self.pusherCallback)
  70.  
  71. ''' This function is called once pusher receives a new event '''
  72. def pusherCallback(self, message):
  73. message = json.loads(message)
  74. if message['user'] != self.user:
  75. print(colored("{}: {}".format(message['user'], message['message']), "blue"))
  76. print(colored("{}: ".format(self.user), "green"))
  77.  
  78. ''' This function is used to get the user's current message '''
  79. def getInput(self):
  80. message = input(colored("{}: ".format(self.user), "green"))
  81. self.pusher.trigger(self.chatroom, u'newmessage', {"user": self.user, "message": message})
  82.  
  83.  
  84. if __name__ == "__main__":
  85. terminalChat().main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement