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()

1 comment:

Typos said...

thnx man...cool one