Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib.request
- import socket
- import re
- #settings
- SERVER = 'irc.freenode.net'
- CHANNEL = input('Channel name: ')
- PASSWORD = input('password: ')
- USER_NAME = 'melonlord'
- NICK = 'melon_bot'
- irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #defines the socket
- print("connecting to: " + SERVER)
- irc.connect((SERVER, 6667)) #connects to the server
- irc.send(bytes('PASS %s\r\n' % PASSWORD, 'UTF-8'))
- irc.send(bytes('USER %s %s melon :%s\r\n' % (USER_NAME, NICK, NICK), 'UTF-8')) #user authentication
- irc.send(bytes('NICK %s\r\n' % NICK, 'UTF-8')) #sets nick
- irc.send(bytes('JOIN %s\r\n' % CHANNEL, 'UTF-8')) #join the chan
- irc.send(bytes('PRIVMSG %s :Greetings %s o/\r\n' % (CHANNEL, CHANNEL), 'UTF-8')) #entrance message
- def getWeather(city):
- try:
- f = urllib.request.urlopen('http://api.openweathermap.org/data/2.5/weather?q=' + city)
- except:
- return 1
- s = f.read().decode("UTF-8")
- match = re.search('.*?temp":(.*?),.*', s)
- if match:
- return match.group(1)
- else:
- return 0
- while 1: #da loop
- data = irc.recv(1024).decode("UTF-8") #receive the text
- print(data) #print text to console
- if data.find('PING') != -1: #check if PING is found
- irc.send(bytes('PONG ' + data.split() [1] + '\r\n', 'UTF-8')) #PONG
- #quit
- m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+)melonlord PRIVMSG %s :.mel quit\s*$' % CHANNEL, data)
- if m:
- irc.send(bytes('PRIVMSG %s :Goodbye :3\r\n' % CHANNEL, 'UTF-8'))
- irc.send(bytes('QUIT\r\n', 'UTF-8'))
- break
- #weather
- m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+) PRIVMSG %s :.(weather|w) (.*)$' % CHANNEL, data)
- if m:
- temp = getWeather(m.group(5))
- if temp != 0 and temp != 1:
- fah = (float(temp) - 273.15) * 1.800 + 32.00
- cel = float(temp) - 273.15
- temp = 'It is ' + str(round(fah, 1)) + '°F(' + str(round(cel, 1)) + '°C) in ' + m.group(5)
- elif temp == 0:
- temp = 'invalid entry'
- else:
- temp = 'page access denied - please request your entry again'
- irc.send(bytes('PRIVMSG %s :%s\r\n' % (CHANNEL, temp), 'UTF-8'))
- #greeting response
- 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)
- if m:
- if m.group(1) != 'Melonus':
- greeter = re.search(':(.+?)!', data)
- irc.send(bytes('PRIVMSG %s :Hey %s o/\r\n' % (CHANNEL, greeter.group(1)), 'UTF-8'))
- else:
- irc.send(bytes('PRIVMSG %s :Hello Master Melon :)\r\n' % CHANNEL, 'UTF-8'))
- #ACTION response
- m = re.search('^:([^ ]+)!([^ ]+)@([^ ]+) PRIVMSG %s :\x01ACTION (.*?) melon_bot\s*\x01$' % CHANNEL, data)
- if m:
- 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