Wednesday, March 28, 2007

XChat script for Lauren chatterbot





Works on XChat, a popular IRC client for Unix-like systems also available for Microsoft Windows.
If you want to use in instant messaging, there is BitlBee, and IRC Instant messaging gateway.
If you don't want to run your own BitlBee server, here are some of the free public servers.
im.bitlbee.org:6667
testing.bitlbee.org:6667
im.starkast.net:6667


import xchat
import os.path
import urllib2
import urllib
import cookielib
import os
import os.path
import re
from threading import Thread

__module_name__ = "LaurenBot"
__module_version__ = "0.1.1"
__module_description__ = "Lauren says"

# irc bot.


class Lauren(Thread):

      def __init__(self, strtalk, contactname , savefile=None):
              Thread.__init__(self)
              if not os.path.isdir('/tmp/LBot'):
                      os.mkdir('/tmp/LBot')
              self.cookiefile = '/tmp/LBot/'+contactname+'.lwp'
              self.cookiejar = cookielib.LWPCookieJar()
              self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookiejar))
              urllib2.install_opener(self.opener)
              self.url = 'http://lauren.vhost.pandorabots.com/pandora/talk?botid=f6d4afd83e34564d&skin=input&speak=true'
              self.strtalk = strtalk
              self.savefile = savefile
              self.laurenreply =''


      def run(self):
              data = None
              iscookiefile = os.path.isfile(self.cookiefile)

              if iscookiefile:
                      self.cookiejar.load(self.cookiefile)
                      self.strtalk = str(self.strtalk).strip('[]').replace(',','').replace('\'','')
                      data = urllib.urlencode({'message':self.strtalk, 'botcust2':'ea5b96438fa868b7'})

              request = urllib2.Request(self.url, data)

              try:
                      urlobj = urllib2.urlopen(request)
                      self.cookiejar.save(self.cookiefile)
                      if not iscookiefile:
                              self.run()


                      message = urlobj.read()
                      if self.savefile:
                              fileobj = open('/home/vishah/Lauren.html', 'w+')
                              fileobj.write(message)
                              fileobj.close()

                      cmpobj = re.compile(r"LaurenBot:<\/b><\/i>([^>]+?)<br> <br>[\n][\n]<form", re.DOTALL|re.MULTILINE)
                      self.laurenreply = cmpobj.search(message).group(1)

              except IOError, e:
                      if hasattr(e, 'reason'):
                              xchat.prnt("Request not fullfilled by the server. Reason:"+e.reason)
                      elif hasattr(e, 'code'):
                              xchat.prnt("failed to reach server. ErrorCode:"+e.code)



print "loaded %s" % __module_name__


class botte(Thread):
      def __init__(self, word, word_eol, userdata):
              Thread.__init__(self)
              self.word = word
              self.word_eol = word_eol
              self.userdata = userdata

      def run(self):
              he_says = self.word_eol[3][1:]
              contact_name = str(self.word_eol[0]).split('!')[0].strip(':')
              current = Lauren(he_says, contact_name)
              current.start()
              current.join()
              whatlaurensays = current.laurenreply


              if whatlaurensays:
                      xchat.command("MSG %s %s" % (contact_name, str(whatlaurensays)))
              return xchat.EAT_NONE

def priv_cb(word, word_eol, userdata):
      botte = duck(word, word_eol, userdata)
      botte.start()

xchat.hook_server("PRIVMSG", priv_cb)

Friday, March 09, 2007

dhiraagu edirectory lister




# ____________________________________
#/ dhiraagu edirectory lister initial \
#\ script...                          /
# ------------------------------------
#
import pygtk
pygtk.require("2.0")
import gtk
import urllib2
import urllib
import sgmllib
import re

class MyParser(sgmllib.SGMLParser):
   def __init__(self, verbose=0):
       sgmllib.SGMLParser.__init__(self, verbose)
       self.numbers = []
       self.bumbers = []
       self.inside_td_element = 0
       self.inside_tdno_element = 0

   def parse(self, data):
       self.feed(data)
       self.close()

   def start_td(self, attributes):
       if attributes == []:
           self.inside_td_element = 1
       else:
           self.inside_tdno_element = 1

   def end_td(self):
       self.inside_td_element = 0
       self.inside_tdno_element = 0

   def handle_data(self, data):
       if self.inside_td_element == 1:
           self.numbers.append(data)
       if self.inside_tdno_element == 1:
           self.bumbers.append(data)

   def get_numbers(self):
       return self.numbers

   def get_bumbers(self):
       return self.bumbers


class duck:
   def clicky(self, widget, textbuffer, entry):
       urlobj = urllib2.urlopen('http://www.dhivehinet.net.mv/index.php?action=edir', urllib.urlencode({'query':entry.get_text(), 'searchtype':'number'}))
       urlobjdata = urlobj.read()
       diParser = MyParser()
       cmpobj = re.compile(r"id=\"edir\".+?table>", re.DOTALL)
       feeddata = cmpobj.search(urlobjdata).group()
       diParser.feed(feeddata)
       asw = diParser.get_numbers()
       asd = diParser.get_bumbers()
       prrr = ''
       for i, j in zip(asw, asd):
           prrr = '%s %-80s %-10s\n' % (prrr, i ,j)
       textbuffer.set_text(prrr)

   def closethingy(self, widget, event, data=None):
       gtk.main_quit()
   def __init__(self):
       self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
       self.window.set_title("Alamaari")
       self.window.connect("delete_event", self.closethingy)
       vbox = gtk.VBox(False, 0)
       entry = gtk.Entry(50)
       vbox.pack_start(entry, False, False, 0)
       button = gtk.Button("Get List")
       textview = gtk.TextView()
       textbuffer = textview.get_buffer()
       button.connect("clicked", self.clicky, textbuffer, entry)
       scrolledwindow = gtk.ScrolledWindow()
       scrolledwindow.set_policy(gtk.POLICY_ALWAYS,gtk.POLICY_ALWAYS)
       scrolledwindow.add_with_viewport(textview)
       vbox.pack_start(button, False, False, 0)
       vbox.pack_start(scrolledwindow, True, True, 10)
       self.window.add(vbox)
       self.window.set_border_width(3)
       self.window.resize(700, 500)
       self.window.show_all()


if __name__ == "__main__":
   duck()
   gtk.main()