Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.95 KB | None | 0 0
  1. import urllib.request
  2. import socket
  3. import re
  4.  
  5. #settings
  6. SERVER = 'irc.freenode.net'
  7. CHANNEL = input('Channel name: ')
  8. PASSWORD = input('password: ')
  9. USER_NAME = 'melonlord'
  10. NICK = 'melon_bot'
  11.  
  12. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)     #defines the socket
  13. print("connecting to: " + SERVER)
  14. irc.connect((SERVER, 6667))    #connects to the server
  15. irc.send(bytes('PASS %s\r\n' % PASSWORD, 'UTF-8'))
  16. irc.send(bytes('USER %s %s melon :%s\r\n' % (USER_NAME, NICK, NICK), 'UTF-8'))     #user authentication
  17. irc.send(bytes('NICK %s\r\n' % NICK, 'UTF-8'))      #sets nick
  18. irc.send(bytes('JOIN %s\r\n' % CHANNEL, 'UTF-8'))       #join the chan
  19. irc.send(bytes('PRIVMSG %s :Greetings %s o/\r\n' % (CHANNEL, CHANNEL), 'UTF-8'))        #entrance message
  20.  
  21. def getWeather(city):
  22.     try:
  23.         f = urllib.request.urlopen('http://api.openweathermap.org/data/2.5/weather?q=' + city)
  24.     except:
  25.         return 1
  26.     s = f.read().decode("UTF-8")
  27.     match = re.search('.*?temp":(.*?),.*', s)
  28.     if match:
  29.         return match.group(1)
  30.     else:
  31.         return 0
  32.    
  33. while 1:    #da loop
  34.     data = irc.recv(1024).decode("UTF-8")       #receive the text
  35.     print(data)     #print text to console
  36.    
  37.     if data.find('PING') != -1:     #check if PING is found
  38.         irc.send(bytes('PONG ' + data.split() [1] + '\r\n', 'UTF-8'))       #PONG
  39.  
  40.     #quit  
  41.     m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+)melonlord PRIVMSG %s :.mel quit\s*$' % CHANNEL, data)
  42.     if m:    
  43.         irc.send(bytes('PRIVMSG %s :Goodbye :3\r\n' % CHANNEL, 'UTF-8'))
  44.         irc.send(bytes('QUIT\r\n', 'UTF-8'))
  45.         break
  46.  
  47.     #weather
  48.     m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+) PRIVMSG %s :.(weather|w) (.*)$' % CHANNEL, data)
  49.     if m:
  50.         temp = getWeather(m.group(5))
  51.         if temp != 0 and temp != 1:
  52.             fah = (float(temp) - 273.15) * 1.800 + 32.00
  53.             cel = float(temp) - 273.15
  54.             temp = 'It is ' + str(round(fah, 1)) + '°F(' + str(round(cel, 1)) + '°C) in ' + m.group(5)
  55.         elif temp == 0:
  56.             temp = 'invalid entry'
  57.         else:
  58.             temp = 'page access denied - please request your entry again'
  59.         irc.send(bytes('PRIVMSG %s :%s\r\n' % (CHANNEL, temp), 'UTF-8'))
  60.  
  61.     #greeting response
  62.     m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+) PRIVMSG %s :((h|H)i|(h|H)ey|(h|H)ello|(h|H)ey(a|o)|(y|Y)o|(s|S)up) melon_bot\s*$' % CHANNEL, data)
  63.     if m:
  64.         if m.group(1) != 'Melonus':
  65.             greeter = re.search(':(.+?)!', data)
  66.             irc.send(bytes('PRIVMSG %s :Hey %s o/\r\n' % (CHANNEL, greeter.group(1)), 'UTF-8'))
  67.         else:
  68.             irc.send(bytes('PRIVMSG %s :Hello Master Melon :)\r\n' % CHANNEL, 'UTF-8'))
  69.  
  70.     #ACTION response
  71.     m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+) PRIVMSG %s :\x01ACTION (.*?) melon_bot\s*\x01$' % CHANNEL, data)
  72.     if m:
  73.         irc.send(bytes('PRIVMSG %s :Thanks %s for the %s\r\n' % (CHANNEL, m.group(1), m.group(4)), 'UTF-8'))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement