Opa, e ai pessoal , está é minha primeira vez no blogger (e em qualquer blogger), e vou comentar um pouco sobre um logger que criei, em python, com ajuda do pessoal aqui do NATE para armazenar as mensagens do nosso canal no IRC (#olpc-brasil no channel FreeNode).
Bem inicialmente o Rafael Barbolo me passo um código com a base de como conectar num servidor IRC, que foi encontrado em:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/299411/index_txt
Entretando este criava um arquivo em modo texto (txt) que não é muito completo, claro. Então comecei a elaborar algumas modifições para que o arquivo salvo fosse em html.
Para isto simplesmente criei um cabeçalho, aonde é definido o nome da página ([head]), e aonde outras futuras informações podem ser alteradas (cor de fundo, imagem de fundo, e até mesmo o layout da página).
No corpo do texto ([body]), decidimos que seria muito interessante que cada usuario possuíse uma cor, para deixar mais claro quem falou. Para isso criamos um dicionário, que armazenaria uma cor, definida em uma tupla “circular”, à um nick_name (apelido). Esta atribuição inicialmente era feita ao usuario efetuar o login no IRC, entretando limitava o código para que o logger SEMPRE fosse o primeiro usuário, senão o programa dava KeyError.
Isto foi facilmente resolvido com o uso da exceção KeyError (óbvio :D), onde seria mais interessante atribuir esta cor ao usuário apenas quando o mesmo enviasse mensagens e se já não houvesse Cor relacionada a este.
Também foi incluído o momento do envio da mensagens através da biblioteca time.
Após esta modificações restavam duas coisas: finalizar o código HTML ([/body][/html]) e enviar o arquivo para um servidor ftp.
Bem a parte de finalizar o código foi feita, inicialmente, através de uma exceção (ow coisinha util viu) KeyboardInterrupt, onde ao se finalizar o programa (com ctrl-C) este finalizaria o HTML terminando assim o log.
Já para enviar o arquivo utilizei como base um arquivo encontrado em:
http://snippets.dzone.com/posts/show/711
Assim ao se finalizar o programa o código era finalizado e já enviado ao servidor, porém achei mais interessante fazer estas duas ultimas ações como um outro processo, assim criei um arquivo send.py.
EDITED: Consegui automatizar a rotina de upload, melhorei o layout da pagina e as cores que são atribuidas aos usuarios, também implementei o replace para mudança dos > e < para não interpretar o html e não sumir os códigos que sejam semelhantes a HTML.
a seguir o código do ircLogger.py:
import socket, string, sys, ftplib, time, datetime, os
#some user data, change as per your tasteSERVER = ‘irc.freenode.net’PORT = 6667NICKNAME = ‘logging_bot’CHANNEL = ‘#olpc-brasil’
#open a socket to handle the connectionIRC = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#open a connection with the serverdef irc_conn(): IRC.connect((SERVER, PORT))
#simple function to send data through the socketdef send_data(command): IRC.send(command + ‘\n’)
#join the channeldef join(channel): send_data(”JOIN %s” % channel)
#send login data (customizable)def login(nickname, username=’user’, password = None, realname=’Logger do Canal’, hostname=’????’, servername=’Server’): send_data(”USER %s %s %s %s” % (username, hostname, servername, realname)) send_data(”NICK ” + nickname)
#for entering more colors to the program see http://www.w3schools.com/html/html_colornames.aspcolor = (’#483D8B’, ‘#FF0000′, ‘#00AA00′, ‘#0000FF’, ‘#FFD700′, ‘#A52A2A’, ‘#FF00FF’, ‘#A0A0A0′, ‘#FF8C00′)i=0 #variable to count between colorsnameC={} #declares dictionary for Colors of Nicknames
irc_conn()login(NICKNAME)join(CHANNEL)
def makeHeader(): #make the HTML header filehtml = open(’msg.html’, ‘a+’) #open afile to store the messages filerd = open (’msg.html’, ‘rb’) if string.find(filerd.read(),’[html]’)!=0: #verify if header is alread there filehtml.write(’[html]’+'\n’+ \ ‘[head] \n [meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″]’ + ‘\n’ + \ ‘ [title]Log do IRC olpc-brasil[/title]’ + ‘\n’ + \ ‘[/head]’ + ‘\n’ + \ ‘[body bgcolor=”#F5F5F5″]’ + ‘\n’ + \ ‘[p][h1] Log do IRC do dia ‘ + str(datetime.date.today()) +’ [/h1][/p]\n’ )
filehtml = open(’msg.html’, ‘a+’)makeHeader()recTime = time.time()
while True: nowTime = time.strftime(”[ %H:%M:%S ] “, time.localtime())
if time.time() >= recTime+60*60*24: #if has passed 24 hours than the last updat #send routine then wait date = str(datetime.date.today())+’.html’ print ‘Connecting…’ s = ftplib.FTP(’ftp.br.geocities.com’,'olpcbrasil’,'****’) # Connect filehtml = open(’msg.html’,'a+’) # file opened to finish html and to send filehtml.write(’[/body]’ + ‘[/html]’) #finish html on file filehtml.flush() print ‘Uploading…’ fileSend = open (’msg.html’ , ‘rb’) s.storbinary(’STOR %s’ % date, fileSend) # Send the file os.system(”rm msg.html”) #remove the previous log filehtml = open(’msg.html’,'a+’) #start the new log makeHeader(); print ‘Operation Finished!’ recTime = time.time() time.sleep(5)
try: buffer = IRC.recv(1024) except KeyboardInterrupt:#verify the end of file (ctrl-C) filehtml.close() #print ‘Type “python send.py” to Upload this LOG to server’ print ‘Program Terminated’ sys.exit(0) msg = string.split(buffer) print msg if msg [1] == ‘PRIVMSG’: print 1 nick_name = msg[0][:string.find(msg[0],”!”)] #if a private message is sent to you catch it message = ‘ ‘.join(msg[3:]) if nick_name != ‘:freenode-connect’: #cancel first msg who comes from IRC try: nameC[nick_name] except KeyError: if i==9: i=0 nameC[nick_name]=color[i] i+=1 textMsg=string.lstrip(message, ‘:’) textMsg=string.lstrip(message, ‘:’).replace(’<’,'<’).replace(’>’,'>’) #to not interp html texts filehtml.write(’[font color=’+nameC[nick_name]+’]’ +nowTime\ + string.lstrip(nick_name, ‘:’)+ ‘ -> [/font]’ + textMsg + ‘[br]’ + ‘\n’ )#write to the file filehtml.flush() #don’t wait for next message, write it now!
elif msg[1] == ‘QUIT’: nick_name = msg[0][:string.find(msg[0],”!”)] message = (”saiu do canal”) filehtml.write(’[font color=”red”]’ + nowTime + string.lstrip(nick_name, ‘:’) + \ ‘ -> ‘ + message + ‘[/font]’ + ‘[br]’ + ‘\n’) #write to the file filehtml.flush() #don’t wait for next message, write it now! try: del nameC[nick_name] except KeyError: print ‘User dont have color because he not send any msg’
elif msg[1] == ‘JOIN’: nick_name = msg[0][:string.find(msg[0],”!”)] message = (”entrou no canal”) filehtml.write(’[font color=”green”]’ + nowTime+ string.lstrip(nick_name, ‘:’) + \ ‘ -> ‘ + message + ‘[/font]’ + ‘[br]’ + ‘\n’) #write to the file filehtml.flush() #don’t wait for next message, write it now!
Os códigos podem ser encontrados tbm em:
br.geocities.com/olpcbrasil/ircLogger.txt e
br.geocities.com/olpcbrasil/send.txt
OBS: As tags do HTML estão entre [] pois o blogger interpreta os comandos em <>