Advertisement
Guest User

Untitled

a guest
Mar 27th, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.56 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. #https://tmi.twitch.tv/group/user/pablogoru/chatters
  4. #:bot_urogap!bot_urogap@bot_urogap.tmi.twitch.tv PRIVMSG #pablogoru :Eso es un link!
  5.  
  6. import socket, time, re, datetime, os, requests
  7. from tkinter import *
  8.  
  9. HOST = "irc.chat.twitch.tv"
  10. PORT = 6667
  11. NICK = "bot_angel"
  12. PASS = "pene"
  13. CHAN = "#pablogoru"
  14.  
  15. def conectar():
  16. s = socket.socket()
  17. s.connect((HOST, PORT))
  18. s.send("PASS ".encode("utf-8") + PASS.encode("utf-8") +"\r\n".encode("utf-8"))
  19. s.send("NICK ".encode("utf-8") + NICK.encode("utf-8") +"\r\n".encode("utf-8"))
  20. s.send("JOIN ".encode("utf-8") + CHAN.encode("utf-8") +"\r\n".encode("utf-8"))
  21. s.send("CAP REQ :twitch.tv/membership\r\n".encode("utf-8"))
  22. s.send("CAP REQ :twitch.tv/tags\r\n".encode("utf-8"))
  23. return s
  24.  
  25. def online(quiero = "viewers"):
  26. link = requests.get("https://tmi.twitch.tv/group/user/%s/chatters" % (CHAN[1:]))
  27. jason = link.json()
  28. return jason["chatters"][quiero]
  29.  
  30. def link(mensaje):
  31. link_str = re.search("\w\.([a-z]{2,})", mensaje.lower())
  32. if link_str and not re.search("!link", mensaje.lower()):
  33. if link_str.group(1).upper() in LISTA: return True
  34. return False
  35.  
  36. def mayus(mensaje):
  37. if len(mensaje.replace(" ", ""))>=5:
  38. if len(re.sub("[^A-Z]", "", mensaje))>len(mensaje.replace(" ", ""))/2: return True
  39. return False
  40.  
  41. def tiempo_online(hora_inicio):
  42. tiempo = datetime.datetime.now() - hora_inicio
  43. dias = tiempo.days
  44. horas = tiempo.seconds // 3600
  45. minutos = tiempo.seconds % 3600 // 60
  46. segundos = tiempo.seconds % 60
  47. #milisegundos = tiempo.microseconds % 1000000 // 1000
  48. #microsegundos = tiempo.microseconds % 1000
  49. str_tiempo = str(dias) + " días, "+ str(horas) + " horas, "+ str(minutos) + " minutos y " + str(segundos)+ " segundos :)"# + str(milisegundos) + " milisegundos y " + str(microsegundos) + "microsegundos :)"
  50. return str(str_tiempo)
  51.  
  52. def leer(file):
  53. lista = []
  54. if not os.path.exists(file): return "error"
  55. lector = open(file).readlines()
  56. for i in lector:
  57. lista.append(i.replace("\n",""))
  58. return lista
  59.  
  60. LISTA = leer("domain.txt")
  61.  
  62. hora_inicio = datetime.datetime(2016, 3, 18, 22, 4, 0)
  63.  
  64. lista_errores = [" ", "End", "of", "/NAMES", "list"]
  65.  
  66. mensajes = []
  67.  
  68. MODS = []
  69. VIEWERS = []
  70.  
  71. RATE = (20/30)
  72. STOP = False
  73. MUTE = True
  74. DEBUG = True
  75. CMD = "?$%&/!"
  76.  
  77. MSG = {"bastardo" : "Dime.",
  78. "amor" : "Nadie te quiere.",
  79. "online" : VIEWERS,
  80. "mods": MODS}
  81.  
  82. def xprint(*vartuple):
  83. if DEBUG: print(*vartuple)
  84. #print("\n")
  85.  
  86. def chat(sock, msg, ignorar = False):
  87. global mensajes
  88. if len(mensajes) >= 20 and 30-(mensajes[0]-time.time())<30:
  89. time.sleep(30-(mensajes[0]-time.time()))
  90. if len(mensajes)>20: mensajes.pop(0)
  91. if ignorar or not MUTE:
  92. mensajes.append(time.time())
  93. mensaje_final="PRIVMSG " + CHAN + " :" + str(msg) +"\r\n"
  94. sock.send(mensaje_final.encode("utf-8"))
  95. xprint("BOT:", msg)
  96. else: xprint("~BOT:", msg)
  97.  
  98. def bucle():
  99. global STOP, DEBUG, MUTE
  100. s = conectar()
  101. #!song: Nombre de la cancion. - !help: Command info. - !faq: Preguntas frecuentes. - !mods: Moderadores online. - !link: Poner un link sin ban. - !uptime: Tiempo online.
  102. #!solve: Resolver operaciones matematicas. - !xdpizza: Contar xd's - !urogap: Mutear/Desmutear a bot_urogap. - !online: Usuarios online.
  103.  
  104. while not STOP:
  105. respuesta = s.recv(4096).decode("utf-8")
  106.  
  107. for response in respuesta.split("\n"):
  108. response = response.replace("\n", "")
  109. response = response.replace("\r", "")
  110.  
  111. if "PING :tmi.twitch.tv" in response: #responde al ping para no desconectarse
  112. s.send("PONG :tmi.twitch.tv\r\n".encode("utf-8"))
  113.  
  114. elif ":{0}.tmi.twitch.tv".format(NICK) in response:
  115. xprint(response)
  116. for nombre in response[len(":{0}.tmi.twitch.tv 353 {0} = {1}".format(NICK, CHAN)):].split(" "):
  117. if nombre not in VIEWERS and nombre not in lista_errores and len(nombre) > 1:
  118. xprint("+", nombre.replace(":",""))
  119. VIEWERS.append(nombre.replace(":",""))
  120.  
  121. elif re.search("PRIVMSG", response): #@color=#FF0000;display-name=bot_urogap;emotes=;mod=1;room-id=30985069;subscriber=0;turbo=0;user-id=119422161;user-type=mod :bot_urogap!bot_urogap@bot_urogap.tmi.twitch.tv PRIVMSG #pablogoru :magicinventor -> NO PONGAS TANTAS MAYUS!
  122. username = re.search("display-name=(\w+)", response).group(1)
  123. message = re.search("{} :(.+)".format(CHAN), response).group(1)#pablogoru :
  124. username_mod = int(re.search("mod=(\d)", response).group(1))
  125. if username_mod:
  126. xprint("MOD", username + ": " + message)
  127. else:
  128. xprint(username + ": " + message)
  129. if str(message)[0] in CMD:
  130. message = message.lower()
  131. if message.startswith("uptime", 1): chat(s, tiempo_online(hora_inicio))
  132. elif message.startswith("exit", 1) and username in MODS + ["minecraftm2000"]:
  133. chat(s, "Adios.")
  134. STOP = True
  135. elif message.startswith("debug", 1) and username in MODS + ["minecraftm2000"]: DEBUG = DEBUG != True
  136. elif message.startswith("angel", 1) and username in MODS + ["minecraftm2000"]: MUTE = MUTE != True
  137. elif message.startswith("solve", 1):
  138. try:
  139. oper = str(eval(message[7:]))
  140. xprint("Operacíon resuelta")
  141. if oper[0] in CMD + "!.":
  142. chat(s, "Eso no está permitido!")
  143. elif link(oper):
  144. pass
  145. else: chat(s, oper)
  146. except: xprint("ERROR en el !solve")
  147. else:
  148. for palabra in MSG.keys():
  149. if message.startswith(palabra, 1):
  150. chat(s, MSG[palabra])
  151. if username not in MODS and link(message):
  152. chat(s, "LINK")
  153. if username not in MODS and mayus(message):
  154. chat(s, "MAYÚSCULAS")
  155.  
  156. elif re.search("JOIN",response):
  157. username = re.search(":(\w+)", response).group(1)
  158. xprint("+",username)
  159. if username not in VIEWERS:
  160. VIEWERS.append(username)
  161.  
  162. elif re.search("PART",response):
  163. username = re.search(":(\w+)", response).group(1)
  164. xprint("-",username)
  165. if username in VIEWERS: VIEWERS.remove(username)
  166. if username in MODS: MODS.remove(username)
  167.  
  168. elif re.search("\+o (\w+)", response):
  169. username = re.search("\+o (\w+)", response).group(1)
  170. xprint("+o",username)
  171. MODS.append(username)
  172.  
  173. elif re.search("-o (\w+)", response):
  174. username = re.search("-o (\w+)", response).group(1)
  175. xprint("-o",username)
  176. if username in MODS: MODS.remove(username)
  177.  
  178. else:
  179. xprint(response)
  180.  
  181. bucle()
  182. raise SystemExit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement