Advertisement
Guest User

Untitled

a guest
Jun 5th, 2016
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.34 KB | None | 0 0
  1. import re
  2. import socket
  3. import json
  4. import urllib.request
  5. from unicodedata import *
  6.  
  7.  
  8.  
  9.  
  10. # --------------------------------------------- Start Settings ----------------------------------------------------
  11. HOST = "irc.twitch.tv" # Hostname of the IRC-Server in this case twitch's
  12. PORT = 6667 # Default IRC-Port
  13. CHAN = "#aerozztv" # Channelname = #{Nickname}
  14. NICK = "Evilbot254" # Nickname = Twitch username
  15. PASS = "oauth:y93pv2z5ii0usy6538esd30fc2gdr3" # www.twitchapps.com/tmi/ will help to retrieve the required authkey
  16. # --------------------------------------------- End Settings -------------------------------------------------------
  17.  
  18.  
  19. # --------------------------------------------- Start Functions ----------------------------------------------------
  20. def send_pong(msg):
  21. con.send(bytes('PONG %s\r\n' % msg, 'UTF-8'))
  22.  
  23.  
  24. def send_message(chan, msg):
  25. con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
  26.  
  27.  
  28. def send_nick(nick):
  29. con.send(bytes('NICK %s\r\n' % nick, 'UTF-8'))
  30.  
  31.  
  32. def send_pass(password):
  33. con.send(bytes('PASS %s\r\n' % password, 'UTF-8'))
  34.  
  35.  
  36. def join_channel(chan):
  37. con.send(bytes('JOIN %s\r\n' % chan, 'UTF-8'))
  38.  
  39.  
  40. def part_channel(chan):
  41. con.send(bytes('PART %s\r\n' % chan, 'UTF-8'))
  42. # --------------------------------------------- End Functions ------------------------------------------------------
  43.  
  44.  
  45. # --------------------------------------------- Start Helper Functions ---------------------------------------------
  46. def get_sender(msg):
  47. result = ""
  48. for char in msg:
  49. if char == "!":
  50. break
  51. if char != ":":
  52. result += char
  53. return result
  54.  
  55.  
  56. def get_message(msg):
  57. result = ""
  58. i = 3
  59. length = len(msg)
  60. while i < length:
  61. result += msg[i] + " "
  62. i += 1
  63. result = result.lstrip(':')
  64. return result
  65.  
  66.  
  67. def parse_message(msg):
  68. if len(msg) >= 1:
  69. msg = msg.split(' ')
  70. options = {'!test': command_test,
  71. '!hello': command_hello,
  72. '!price': command_price,
  73. '!mom': command_mom,
  74. '!steamprofile': command_steamprofile,
  75. '!steamid64': command_testing,
  76. '!followingsince': command_following,
  77. '!inventoryprice': command_inventory,
  78. '!asdf': command_asdf}
  79. if msg[0] in options:
  80. options[msg[0]]()
  81. # --------------------------------------------- End Helper Functions -----------------------------------------------
  82.  
  83.  
  84. # --------------------------------------------- Start Command Functions --------------------------------------------
  85. def command_test():
  86. send_message(CHAN, 'testing some stuff')
  87.  
  88. def command_hello():
  89. send_message(CHAN, 'Hello my name is evilbot254')
  90.  
  91. def command_mom():
  92. send_message(CHAN, 'Your mom is gay')
  93.  
  94. def back_apiError():
  95. send_message(CHAN, "@%s -> This bot sucks please try again later." % sender)
  96.  
  97. def command_testing():
  98. global target
  99. msg = message[:-1].split(" ")
  100. cmd = msg[0].lower()
  101. if len(msg) >= 2:
  102. target = msg[1].strip("@").lower()
  103. else:
  104. target = sender
  105. page = urllib.request.urlopen("https://api.twitch.tv/api/channels/%s" % target).read().decode("utf-8")
  106. jsonData = json.loads(page)
  107. steamid64 = jsonData["steam_id"]
  108. if steamid64:
  109. if target == sender:
  110. send_message(CHAN, "@%s -> Your steamid64 is %s" % (sender,steamid64))
  111. else:
  112. send_message(CHAN, "@%s -> %s's steamid64 is %s" % (sender,target,steamid64))
  113. else:
  114. if target == sender:
  115. send_message(CHAN, "@%s -> Your steam account is not linked." % sender)
  116. else:
  117. send_message(CHAN, "@%s -> %s's Steam account is not linked." % (sender, target))
  118.  
  119. def command_price():
  120. global target
  121. msg = message[:-1].split(" ")
  122. cmd = msg[0].lower()
  123. if len(msg) >= 2:
  124. target = msg[1:]
  125. skin = ""
  126. for word in target:
  127. skin = skin + word + " "
  128. skin = skin[:-1]
  129. skin = skin.replace(" ", "%20")
  130. skin = skin.replace("star", "★")
  131. skin = skin.replace("stattrak", "StatTrak")
  132. try:
  133. req = urllib.request.Request("http://csgobackpack.net/api/GetItemPrice/?currency=USD&id=%s&time=7&icon=1" % skin, headers={'User-Agent': 'Mozilla/5.0'})
  134. page = urllib.request.urlopen(req).read().decode('utf-8')
  135. jsonData = json.loads(page)
  136. try:
  137. average = jsonData["average_price"]
  138. lowest = jsonData["lowest_price"]
  139. send_message(CHAN, 'The average price is $%s and the lowest price is $%s' % (average,lowest))
  140. except KeyError:
  141. send_message(CHAN, "Invalid name!")
  142. except socket.error:
  143. send_message(CHAN, "@%s -> %s is not a valid name." (sender, skin))
  144. except UnicodeEncodeError:
  145. send_message(CHAN, "Knives are not working yet")
  146. else:
  147. send_message(CHAN, "You must provide a skin name.")
  148.  
  149.  
  150. def command_steamprofile():
  151. global target
  152. msg = message[:-1].split(" ")
  153. cmd = msg[0].lower()
  154. if len(msg) >= 2:
  155. target = msg[1].strip("@").lower()
  156. else:
  157. target = sender
  158. page = urllib.request.urlopen("https://api.twitch.tv/api/channels/%s" % target).read().decode("utf-8")
  159. jsonData = json.loads(page)
  160. steamid64 = jsonData["steam_id"]
  161. if steamid64:
  162. if target == sender:
  163. send_message(CHAN, "@%s -> Your steamprofile is https://steamcommunity.com/profiles/%s" % (sender,steamid64))
  164. else:
  165. send_message(CHAN, "@%s -> %s's steamidprofile is https://steamcommunity.com/profiles/%s" % (sender,target,steamid64))
  166. else:
  167. if target == sender:
  168. send_message(CHAN, "@%s -> Your steam account is not linked." % sender)
  169. else:
  170. send_message(CHAN, "@%s -> %s's Steam account is not linked." % (sender, target))
  171.  
  172.  
  173. def command_following():
  174. global target
  175. msg = message[:-1].split(" ")
  176. cmd = msg[0].lower()
  177. if len(msg) >= 2:
  178. target = msg[1].strip("@").lower()
  179. else:
  180. target = sender
  181.  
  182. page = urllib.request.urlopen(" https://apis.rtainc.co/twitchbot/following?channel=AerozzTV&user=%s" % target).read().decode("utf-8")
  183. if ("is not following" in page):
  184. if (target == sender):
  185. send_message(CHAN, "@%s -> You are not following" % sender)
  186. else:
  187. send_message(CHAN, "@%s -> %s is not following" % (sender,target))
  188. else:
  189.  
  190. if (target == sender):
  191. send_message(CHAN, "@%s -> you have been following for %s " % (sender,page))
  192. else:
  193. send_message(CHAN, "@%s -> %s Has been following for %s" % (sender,target,page))
  194.  
  195. def command_inventory():
  196. global target
  197. msg = message[:-1].split(" ")
  198. cmd = msg[0].lower()
  199. if len(msg) >= 2:
  200. target = msg[1].strip("@").lower()
  201. else:
  202. target = sender
  203. try:
  204. page = urllib.request.urlopen("https://api.twitch.tv/api/channels/%s" % target).read().decode("utf-8")
  205. jsonData = json.loads(page)
  206. steamid64 = jsonData["steam_id"]
  207. if steamid64:
  208. req = urllib.request.Request("http://csgobackpack.net/api/GetInventoryValue/?id=%s" % steamid64, headers={'User-Agent': 'Mozilla/5.0'})
  209. page = urllib.request.urlopen(req).read().decode('utf-8-sig')
  210. jsonData = json.loads(page)
  211. price = jsonData["value"]
  212. if target == sender:
  213. send_message(CHAN,"@%s -> Your Inventory Value is: %s" % (sender,price))
  214. else:
  215. send_message(CHAN,"@%s -> %s's Inventory Value is %s" % (sender,target,price))
  216. else:
  217. if target == sender:
  218. send_message(CHAN,"@%s -> Your steam account must be linked to check your inventory value!" %sender)
  219. else:
  220. send_message(CHAN,"@%s -> %s's Steam account is not linked." %(sender, target))
  221. except socket.error:
  222. send_message(CHAN,"@%s -> That Twitch username seems to be invalid." % sender)
  223.  
  224.  
  225. def command_asdf():
  226. send_message(CHAN, 'asdfster')
  227.  
  228. # --------------------------------------------- End Command Functions ----------------------------------------------
  229.  
  230. con = socket.socket()
  231. con.connect((HOST, PORT))
  232.  
  233. send_pass(PASS)
  234. send_nick(NICK)
  235. join_channel(CHAN)
  236.  
  237. data = ""
  238.  
  239. while True:
  240. try:
  241. data = data+con.recv(1024).decode('UTF-8')
  242. data_split = re.split(r"[~\r\n]+", data)
  243. data = data_split.pop()
  244.  
  245. for line in data_split:
  246. line = str.rstrip(line)
  247. line = str.split(line)
  248.  
  249. if len(line) >= 1:
  250. if line[0] == 'PING':
  251. send_pong(line[1])
  252.  
  253. if line[1] == 'PRIVMSG':
  254. sender = get_sender(line[0])
  255. message = get_message(line)
  256. parse_message(message)
  257.  
  258. print(sender + ": " + message)
  259.  
  260. except socket.error:
  261. print("Socket died")
  262.  
  263. except socket.timeout:
  264. print("Socket timeout")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement