Sunday, September 23, 2007

Dhiraagu e-directory lister update.






# ____________________________________
#/ dhiraagu edirectory lister 2 nd    \
#\ version                            /
# ------------------------------------
#
import pygtk
pygtk.require("2.0")
import gtk
import urllib2
import urllib
import sgmllib
import re
import cookielib

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
      self
  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, next=None,first = None):

      urlobj = None
      urlobjdata = ""
      cmpobjres = None
      cmpobj = None

      if first != None:
              urlobj = urllib2.urlopen('http://www.dhivehinet.net.mv/index.php', urllib.urlencode({'action':'edir', 'query':entry.get_text(), 'searchtype':'number'}))
              for i in range(0,len(self.button)):
                      self.button[i].destroy()
              self.button=[]

      else:

              self.cj.load('/tmp/edircookie')
              urlobj = urllib2.urlopen('http://www.dhivehinet.net.mv/index.php', urllib.urlencode({'action':'edir','page':next}))

      self.cj.save('/tmp/edircookie')

      urlobjdata = urlobj.read()
      if (int(self.nextpage) == int(next)) or ( first != None ):
              cmpobj = re.compile(r'<a id=\"nextPage\" href=\".+?page=([0-9]{1,})\"', re.DOTALL)
              cmpobjres = cmpobj.search(urlobjdata)

              if cmpobjres is not None:
                      print "sssssssssssss"
                      self.nextpage = cmpobjres.group(1)
                      self.button.append(gtk.Button(self.nextpage))
                      buttonindex = int(self.nextpage)
                      print buttonindex
                      self.hbox.pack_start(self.button[buttonindex-1],True,True,0)
                      self.button[buttonindex-1].connect("clicked",self.clicky,textbuffer, entry, buttonindex )
                      self.button[buttonindex-1].show()

      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 = prrr + "\n" + j.ljust(12,' ') + i.ljust(20, ' ')
      textbuffer.set_text(prrr)

  def closethingy(self, widget, event, data=None):
      gtk.main_quit()
  def __init__(self):
      self.button = []
      self.cj = cookielib.LWPCookieJar()
      opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
      urllib2.install_opener(opener)
      self.nextpage=0
      self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
      self.window.set_title("Dhiraagu edirectory lister")
      self.window.connect("delete_event", self.closethingy)
      self.vbox = gtk.VBox(False, 0)
      self.hbox = gtk.HBox(True,0)
      entry = gtk.Entry(50)
      self.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,0,"yes")
      scrolledwindow = gtk.ScrolledWindow()
      scrolledwindow.set_policy(gtk.POLICY_ALWAYS,gtk.POLICY_ALWAYS)
      scrolledwindow.add_with_viewport(textview)
      self.vbox.pack_start(button, False, False, 0)
      self.vbox.pack_start(scrolledwindow, True, True, 10)
      self.vbox.pack_start(self.hbox,False,False,0)
      self.window.add(self.vbox)
      self.window.set_border_width(3)
      self.window.resize(700, 500)
      self.window.show_all()


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



4 comments:

Typos said...

looks like we both really do love messing with the Edirectory...

Ameer said...

Wow thats quite tricky. But, its gonna take up a hell of memory. I wanna see a working sample. Can u...?

HaLku said...

You can copy the code to a text file "edir.py"
and run it by typing:
python edir.py

HaLku said...

Typos, maybe we can combine these small Dhivehi oriented tools into one package.