Advertisement
Guest User

Untitled

a guest
Apr 8th, 2016
718
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.71 KB | None | 0 0
  1. # steamidfinder.py
  2. # Twitch bot by Logey420
  3. # Last Updated 2016/03/31 - 13:51 (GMT)
  4. # Any dates shown are in the format YYYY/MM/DD
  5.  
  6.  
  7. from os import system
  8. import re
  9. import socket
  10. import urllib.request
  11.  
  12. HOST = "irc.twitch.tv" # Hostname of the IRC-Server in this case, Twitch's
  13. PORT = 6667 # Default IRC-Port
  14. CHAN = "#" + input("Channel to enter?:\n\t#") # Channelname = #{Nickname}
  15. channelName = CHAN.strip("#")
  16. NICK = "evilbot254" # Nickname = Twitch username
  17. PASS = open("oauth.txt", "r").read() # www.twitchapps.com/tmi/ will help to retrieve the required authkey
  18. # replace above line with:
  19. # PASS = "oauth:exampletoken"
  20.  
  21. system("title " + CHAN)
  22.  
  23.  
  24. def send_pong(msg):
  25. con.send(bytes('PONG %s\r\n' % msg, 'UTF-8'))
  26.  
  27.  
  28. def send_message(chan, msg):
  29. con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
  30.  
  31.  
  32. def send_nick(nick):
  33. con.send(bytes('NICK %s\r\n' % nick, 'UTF-8'))
  34.  
  35.  
  36. def send_pass(password):
  37. con.send(bytes('PASS %s\r\n' % password, 'UTF-8'))
  38.  
  39.  
  40. def join_channel(chan):
  41. con.send(bytes('JOIN %s\r\n' % chan, 'UTF-8'))
  42.  
  43.  
  44. def part_channel(chan):
  45. con.send(bytes('PART %s\r\n' % chan, 'UTF-8'))
  46.  
  47.  
  48. def get_sender(msg):
  49. result = ""
  50. for char in msg:
  51. if char == "!":
  52. break
  53. if char != ":":
  54. result += char
  55. return result
  56.  
  57.  
  58. def get_message(msg):
  59. result = ""
  60. i = 3
  61. length = len(msg)
  62. while i < length:
  63. result += msg[i] + " "
  64. i += 1
  65. result = result.lstrip(':')
  66. return result
  67.  
  68.  
  69. def parse_message(msg):
  70. if len(msg) >= 1:
  71. msg = msg.split(' ')
  72. options = {'!claudu': claudia,
  73. '!idfind': command_findID,
  74. '!myid': command_myID,
  75. '!findmyid': command_myID,
  76. '!sourcecode': command_source_code}
  77. if msg[0] in options:
  78. options[msg[0]]()
  79.  
  80.  
  81. def command_findID():
  82. twitchUser = message[8:].strip(" ")
  83. try:
  84. page = urllib.request.urlopen("https://api.twitch.tv/api/channels/%s" % twitchUser)
  85. pageContent = str(page.read())
  86. pageData = pageContent.split(",")
  87. steamID = pageData[13].strip('"steam_id":""')
  88. if "steam_id" not in pageData[13]:
  89. steamID = pageData[14].strip('"steam_id":""')
  90. if steamID == "null":
  91. messageToSend = "@%s -> %s's Steam and Twitch accounts are not linked. The accounts can be linked here: https://www.twitch.tv/settings/connections" % (sender, twitchUser)
  92. else:
  93. messageToSend = "@%s -> %s's steamID64 is: %s" % (sender, twitchUser, steamID)
  94. elif steamID == "null":
  95. messageToSend = "@%s -> %s's Steam and Twitch accounts are not linked. The accounts can be linked here: https://www.twitch.tv/settings/connections" % (sender, twitchUser)
  96. else:
  97. messageToSend = "@%s -> %s's steamID64 is: %s" % (sender, twitchUser, steamID)
  98. send_message(CHAN, messageToSend)
  99. print(NICK + ": " + messageToSend)
  100. except:
  101. send_message(CHAN, "@%s -> API error! Please try again." % sender)
  102. print(NICK + ": " + "@%s -> API error! Please try again." % sender)
  103.  
  104. def command_myID():
  105. try:
  106. page = urllib.request.urlopen("https://api.twitch.tv/api/channels/%s" % sender)
  107. pageContent = str(page.read())
  108. pageData = pageContent.split(",")
  109. steamID = pageData[13].strip('"steam_id":""')
  110. if "steam_id" not in pageData[13]:
  111. steamID = pageData[14].strip('"steam_id":""')
  112. if steamID == "null":
  113. messageToSend = "@%s -> Your Steam and Twitch accounts are not linked. You can link them here: https://www.twitch.tv/settings/connections" % sender
  114. else:
  115. messageToSend = "@%s -> Your steamID64 is: %s" % (sender, steamID)
  116. elif steamID == "null":
  117. messageToSend = "@%s -> Your Steam and Twitch accounts are not linked. You can link them here: https://www.twitch.tv/settings/connections" % sender
  118. else:
  119. messageToSend = "@%s -> Your steamID64 is: %s" % (sender, steamID)
  120. send_message(CHAN, messageToSend)
  121. print(NICK + ": " + messageToSend)
  122. except:
  123. send_message(CHAN, "@%s -> API error! Please try again." % sender)
  124. print(NICK + ": " + "@%s -> API error! Please try again." % sender)
  125.  
  126. def command_source_code():
  127. send_message(CHAN, "@%s -> Here is my source code: http://pastebin.com/4j5dwB3c" % sender)
  128. print(NICK + ": " + "@%s -> Here is my source code: http://pastebin.com/4j5dwB3c" % sender)
  129. def claudia():
  130. send_message(CHAN, "@%s -> sup claudia." % sender)
  131. print(NICK + ": " + "@%s -> sup claudia." % sender)
  132.  
  133.  
  134. con = socket.socket()
  135. con.connect((HOST, PORT))
  136.  
  137. send_pass(PASS)
  138. send_nick(NICK)
  139. join_channel(CHAN)
  140.  
  141. data = ""
  142.  
  143. while True:
  144. try:
  145. data = data+con.recv(1024).decode('UTF-8')
  146. data_split = re.split(r"[~\r\n]+", data)
  147. data = data_split.pop()
  148.  
  149. for line in data_split:
  150. line = str.rstrip(line)
  151. line = str.split(line)
  152.  
  153. if len(line) >= 1:
  154. if line[0] == 'PING':
  155. send_pong(line[1])
  156.  
  157. if line[1] == 'PRIVMSG':
  158. sender = get_sender(line[0])
  159. message = get_message(line)
  160. parse_message(message)
  161.  
  162. print(sender + ": " + message)
  163.  
  164. except socket.error:
  165. print("Socket died")
  166.  
  167. except socket.timeout:
  168. print("Socket timeout")
  169.  
  170.  
  171. raw_input()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement