Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. import socket
  2. import string
  3. import os
  4. import time
  5. import sys
  6. import threading
  7. import itertools
  8. from urllib.request import urlopen
  9. import json
  10. from user import User
  11. clear = lambda: os.system("cls")
  12. USERS = []
  13.  
  14. def makeUser(name, place, kills):
  15. newUser = User(name,place,kills)
  16. return newUser
  17.  
  18. def loadJson():
  19. with open("users.json") as json_data:
  20. data = json.load(json_data)
  21.  
  22. if len(data["users"]) > 0:
  23. USERS = []
  24.  
  25. for user in data["users"]:
  26. newUser = makeUser(user["username"], user["place"], user["kills"])
  27. USERS.append(newUser)
  28.  
  29.  
  30.  
  31.  
  32. def saveJson():
  33. data = {}
  34. data["users"] = []
  35. for x in USERS:
  36. data["users"].append({
  37. "username": x.get_name(),
  38. "place": x.get_place(),
  39. "kills": x.get_kills()
  40. })
  41. with open("users.json", "w") as outfile:
  42. json.dump(data, outfile)
  43.  
  44.  
  45.  
  46. if True:
  47. #Twitch credentials
  48. HOST = "irc.twitch.tv"
  49. NICK = "tagptroll1"
  50. PORT = 6667
  51. PASS = "NiceTry"
  52.  
  53.  
  54.  
  55. #Send credentials
  56. s = socket.socket()
  57. s.connect((HOST, PORT))
  58. s.send(bytes("PASS " + PASS + "\r\n", "UTF-8"))
  59. s.send(bytes("NICK " + NICK + "\r\n", "UTF-8"))
  60. s.send(bytes("JOIN #" + NICK + " \r\n", "UTF-8"))
  61.  
  62. def send_message(message):
  63. s.send(bytes("PRIVMSG #" + NICK + " :#" + message + "\r\n", "UTF-8"))
  64.  
  65. def animate():
  66. for c in itertools.cycle(['|', '/', '-', '\\']):
  67. if done:
  68. break
  69. sys.stdout.write('\rloading ' + c)
  70. sys.stdout.flush()
  71. time.sleep(0.1)
  72.  
  73.  
  74.  
  75. #Functions
  76. def update_mods():
  77. global done
  78.  
  79. clear()
  80. done = False
  81. t = threading.Thread(target=animate)
  82. t.start()
  83.  
  84. #fetch moderators
  85. response = urlopen('https://tmi.twitch.tv/group/user/' + NICK + '/chatters')
  86. readable = response.read().decode('utf-8')
  87. chatlist = json.loads(readable)
  88. chatters = chatlist['chatters']
  89. moderators = chatters['moderators']
  90. moderators.append("tagptroll1")
  91. done = True
  92. clear()
  93. send_message("done")
  94. print("\rDone! MODS LOADED: " + str(moderators)+"\nChat:\n", end="")
  95.  
  96. def get_end_letters(x):
  97. if int(x) % 10 == 1 and int(x) != 11:
  98. x = "st"
  99. elif int(x) % 10 == 2 and int(x) != 12:
  100. x = "nd"
  101. elif int(x) % 10 == 3 and int(x) != 13:
  102. x = "rd"
  103. else:
  104. x = "th"
  105. return str(x)
  106.  
  107. def isNumber(s):
  108. try:
  109. int(s)
  110. return True
  111. except ValueError:
  112. return False
  113.  
  114. def user_in(user):
  115. for x in USERS:
  116. print(str(x.get_name()))
  117. if user.get_name() == x.get_name():
  118. return True
  119. else:
  120. return False
  121.  
  122. def find_user(user):
  123. for x in USERS:
  124. if user.get_name() == x.get_name():
  125. return x
  126. else:
  127. None
  128.  
  129. def addUser(username, x, y):
  130. newUser = User(username, x, y)
  131. if not(user_in(newUser)):
  132. USERS.append(newUser)
  133. send_message("Entered~")
  134. else:
  135. newUser = find_user(newUser)
  136. send_message("{}, you have already entered with {}{} place and {} kills".format(newUser.get_name(),
  137. newUser.get_place(),
  138. get_end_letters(newUser.get_kills()),
  139. newUser.get_kills()))
  140. def firsti():
  141. clear()
  142. update_mods()
  143. loadJson()
  144.  
  145. firsti()
  146.  
  147. #Commands
  148. def parseToCommand(messageList, username):
  149. command = messageList[0]
  150. length = len(messageList)
  151.  
  152. try:
  153. s1 = messageList[1]
  154. s2 = messageList[2]
  155. except IndexError:
  156. None
  157. else:
  158. None
  159.  
  160. if command == "!bet" and length == 3:
  161. try:
  162. isNumber(s1) and isNumber(s2)
  163. except IndexError:
  164. None
  165. else:
  166. for x in USERS:
  167. print(str(x.get_name()))
  168. addUser(username, s1, s2)
  169. saveJson()
  170.  
  171. elif command == "!updatemods":
  172. update_mods()
  173. elif command == "!savebets":
  174. saveJson()
  175. elif command == "!loadbets":
  176. loadJson()
  177.  
  178.  
  179.  
  180.  
  181.  
  182. while True:
  183. line = str(s.recv(1024))
  184. if "End of /NAMES list" in line:
  185. break
  186.  
  187. # -- Chat reading --
  188. while True:
  189. for line in str(s.recv(1024)).split('\\r\\n'):
  190. parts = line.split(':')
  191. if len(parts) < 3:
  192. continue
  193.  
  194. if "QUIT" not in parts[1] and "JOIN" not in parts[1] and "PART" not in parts[1]:
  195. message = parts[2][:len(parts[2])]
  196.  
  197. usernamesplit = parts[1].split("!")
  198. username = usernamesplit[0]
  199.  
  200. print(username + ": " + message)
  201.  
  202. messageList = message.split(" ")
  203.  
  204. parseToCommand(messageList, username)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement